Struggling with "Array out of range" message - page 2

 
If you want help post the code.
 
input int Slippage = 30;        // Tolerated slippage in pips
input double Lots = 0.1;
input int TakeProfit = 200; // In your broker's pips
input int StopLoss = 1000;
input int Magic = 123123123;

// Global variables


//+------------------------------------------------------------------+
//| Expert Every Tick Function                                       |
//+------------------------------------------------------------------+
void OnTick()
{
        
double AVG = iCustom(NULL,0,"AVG USD",0,1);
   
   Comment (  AVG );   
   
}


Here's the code, not much to show at the moment as the indicator shows the wrong value as I said.

Best regards Steve 

 
Can you give an example of a 'wrong' value? Is it something like 2147483647?
 

No, nothing like that ( which is the way a null value is represented I think? ).

The values are just not the same value as I can see on the indicator for the chart. Here's an example.

This is the indicator on a GbpUsd Daily chart, the value for the 14/1/2013 seems to be 1.6471 

http://c2n.me/jgYmtF.png 

 

Here's the EA on the GbpUsd daily chart. The value being reported on the 15/1 for the previous days bar is 1.674

 

http://c2n.me/jgYrJk.png

 

Best regards Steve 

 
When you place the cross hairs over the bar in question (14/1/2013), what does the data window show the value of the indicator to be?
 

1.6471

Interestingly when I stop the EA and the indicator paints. It doesn't paint the indicator at 1.674 so I have no idea where it is getting this value from. Also while the painted indicator is broadly similar to the indicator placed directly on the chart it doesn't appear to be exactly the same. I'm wondering whether there is an issue with the final indicator. I'll attach the version I'm currently using below.

 http://c2n.me/jgZamC.png

 

#property indicator_chart_window    // Indicator is drawn in the main window
#property indicator_buffers 2       // Number of buffers
#property indicator_color1 Blue     // Color of the 1st line
#property indicator_color2 Red      // Color of the 2nd line

double Buf_0[],Buf_1[];             // Declaring arrays (for indicator buffers)
double CloseEurUsd[],CloseGbpUsd[],CloseAudUsd[],CloseAgUsd[],CloseAuUsd[];
double CloseEurUsd2[], CloseGbpUsd2[] , CloseAudUsd2[] , CloseAgUsd2[], CloseAuUsd2[] ;

double VarEurUsd[],VarGbpUsd[],VarAudUsd[],VarAgUsd[],VarAuUsd[];
double Average[], Adj[], AdjPrice[];
//--------------------------------------------------------------------
int init() // Special function init()
  {
   SetIndexBuffer(0,Buf_0);         // Assigning an array to a buffer
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);// Line style
   SetIndexBuffer(1,Buf_1);         // Assigning an array to a buffer
   SetIndexStyle(1,DRAW_LINE,STYLE_DOT,1);// Line style
   return(0);                          // Exit the special funct. init()
  }
//--------------------------------------------------------------------
int start() // Special function start()
  {
   int Counted_bars;                // Number of counted bars
//--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
//   i=Bars-Counted_bars-2;           // Index of the first uncounted
   ArrayResize(CloseEurUsd, Bars);
   ArrayResize(CloseGbpUsd, Bars);
   ArrayResize(CloseAudUsd, Bars);
   ArrayResize(CloseAgUsd, Bars);
   ArrayResize(CloseAuUsd, Bars);

   ArrayResize(VarEurUsd, Bars);
   ArrayResize(VarGbpUsd, Bars);
   ArrayResize(VarAudUsd, Bars);
   ArrayResize(VarAgUsd, Bars);
   ArrayResize(VarAuUsd, Bars);
   ArrayResize(Average, Bars);
   ArrayResize(Adj, Bars); 
   ArrayResize(AdjPrice, Bars);
 
  for(int i = Bars - 1 - Counted_bars; i >= 0; --i){ // No look backs
      CloseEurUsd[i] = iClose("EURUSD",PERIOD_D1,i);
 //     CloseGbpUsd[i] = iClose("GBPUSD",PERIOD_D1,i);
      CloseAudUsd[i] = iClose("AUDUSD",PERIOD_D1,i);
      CloseAgUsd[i] = iClose("XAGUSD",PERIOD_D1,i);
      CloseAuUsd[i] = iClose("XAUUSD",PERIOD_D1,i);

}
for(int i2 = Bars - 1 - MathMax(1,Counted_bars); i2 >= 0; --i2){ // max lookback
      VarEurUsd[i2] = ((CloseEurUsd[i2] - CloseEurUsd[i2+1])/CloseEurUsd[i2+1]);
  //    VarGbpUsd[i2] = ((CloseGbpUsd[i2] - CloseGbpUsd[i2+1])/CloseGbpUsd[i2+1]);
      VarAudUsd[i2] = ((CloseAudUsd[i2] - CloseAudUsd[i2+1])/CloseAudUsd[i2+1]);
      VarAgUsd[i2] = ((CloseAgUsd[i2] - CloseAgUsd[i2+1])/CloseAgUsd[i2+1]);
      VarAuUsd[i2] = ((CloseAuUsd[i2] - CloseAuUsd[i2+1])/CloseAuUsd[i2+1]);


      Average[i2]=(VarEurUsd[i2]+/*VarGbpUsd[i2]+*/VarAudUsd[i2]+VarAgUsd[i2]+VarAuUsd[i2])/4;

      Adj[i2]=iClose(NULL,0,i2+1)*Average[i2];
      AdjPrice[i2]=Adj[i2]+iClose(NULL,0,i2);

      Buf_0[i2]=AdjPrice[i2];             // Value of 0 buffer on i bar

   //   Buf_1[i2]=Low[i2];              // Value of 1st buffer on i bar
//      i--;                          // Calculating index of the next bar
     }
//--------------------------------------------------------------------
   return(0);                          // Exit the special funct. start()
  }
//--------------------------------------------------------------------
 
All the quote-arrays of mt4 are set 'the other way round' - therefore mt4 provide ArraySetAsSeries(), read about it in your mt4 reference and follow the link behind timeseries to "Access to Timeseries and Indicator Data"
 

Thanks, okay I've read that. I understand the concept that the bars are numbered in the other direction. Unfortunately I'm not sure how to apply this to the indicator and why the indicator seems to be calculating correctly when placed directly onto a chart but is not calculating correctly when referenced from within iCustom.

Best regards Steve 

 
gooly:
All the quote-arrays of mt4 are set 'the other way round' - therefore mt4 provide ArraySetAsSeries(), read about it in your mt4 reference and follow the link behind timeseries to "Access to Timeseries and IndicatorData"

You are absolutely right.

Stevetrade should also consider using additional buffers instead of arrays as the resizing and set as series is done automatically. 

 

Okay, so if I'm understanding correctly, and apologies for my denseness.

I should not use arrays. I should declare a buffer for each pair I wish to take the values for and then use the buffers for the calculation, outputting to the final buffer which I wish to show on the chart?

Best regards Steve 

Reason: