Last X candles worth of values missing

 

Hi,

This is driving me nuts and most likely a very noob questions (I apologise in advance).  Im trying to write an oscillator that shows the percentage difference between the close price of the candle vs the close price of 5 candles prior.  It seems to work however the last 5 values before the current price is missing (or more if I increase th number of bars in history I want to compare), what am i doing wrong?

   int Counted_bars=IndicatorCounted(); // Number of counted bars
   int i=Bars-Counted_bars-1;           // Index of the first uncounted

   
   // DRAW
   while(i>=0)                      
     {  
      double t=0;  
      
      M5    = ((Close[i]-Close[i-5])/Close[i-5])*100;     
      BLUE[i] = NormalizeDouble(t,2);
      
      i--;
     }

 

 

mt4 counts up!!

[0] is the actual bar, [1] the previous, [2] is...:

 M5    = ((Close[i]-Close[i+5])/Close[i+5])*100; 

It is that simple and btw it's all in the editor's reference, just press F1.

 
gooly:

mt4 counts up!!

[0] is the actual bar, [1] the previous, [2] is...:

It is that simple and btw it's all in the editor's reference, just press F1.

Thats what I thought however if I change it to + to count up then I get nothing outputted to screen?

#property indicator_separate_window          // Draw in separate chart
#property indicator_buffers 1                // Number of buffers
#property indicator_color1 clrBlue           // Color of the 1st line 


// Indicator Declarations
double BLUE[];                               // Array


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping

      SetIndexBuffer(0,BLUE);                      // Assigning an array to a buffer
      SetIndexStyle (0,DRAW_LINE,STYLE_SOLID,1);   // Solid  
 
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---   

   double   M5 = 0;                      
   int      Counted_bars = 0;            // Number of counted bars
   int      i = 0;                       // Count


   //--------------------------------------------------------------------
   Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted

   // DRAW
   while(i>=0)                      // Loop for uncounted bars
     {    
      M5  = ((Close[i]-Close[i+5])/Close[i+5])*100;
      BLUE[i]=NormalizeDouble(M5,2);
      
      i--;
     }
 
   //--------------------------------------------------------------------
  

 
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

My crystal ball just has told me that you probably have an error message in the experts tab:

4002

ERR_ARRAY_INDEX_OUT_OF_RANGE

Array index is out of range


Can you imagine why?

This part is wrong:

 Counted_bars=IndicatorCounted(); // Number of counted bars
   i=Bars-Counted_bars-1;           // Index of the first uncounted

At its first pass Counted_bars==0 => Close[i+5] => Close[Bars-0-1+5]! ok?

 
i+5 is a look back of 5
Counted_bars=IndicatorCounted(); // Number of counted bars
i=Bars-Counted_bars-1;           // Index of the first uncounted
:
  M5  = ((Close[i]-Close[i+5])/Close[i+5])*100;
Handle your look backs
#define LB 5 // Close[i+5]
i=Bars-MathMax(LB, Counted_bars)-1; // Index of the first uncounted
 
WHRoeder:
i+5 is a look back of 5
Handle your look backs

Arghhh!

thats great, thank you for your help guys. 

Reason: