Oscillator stops 10 candles early MQ5 code

 

I coded a basic price calculation for an MT5 oscillator and it calculates correctly

you can see after the second vertical line, the value is 0 to the end

the problem i have is that it stops 10 candles early and i really dont know why it does this

//+------------------------------------------------------------------+
//| 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[])
  {
//---
   int begin = 0;
   int x = 0;
   int StartCalcPosition=0;
//--- check for bars count
   if(rates_total<StartCalcPosition)
      return(0);
//--- correct draw begin
   if(begin>0) PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,StartCalcPosition);
//--- calculate position
   int pos=prev_calculated-1;
   if(pos<StartCalcPosition)
      pos=StartCalcPosition;
//--- main cycle
   for(int i=0;i<rates_total;i++)
     {
   x=0;   
   
   if(close[i]>close[i+1]){x++;}
   if(close[i]<close[i+1]){x--;}
   
   if(close[i]>close[i+2]){x++;}
   if(close[i]<close[i+2]){x--;}

   /// THERE ARE MANY MORE LINES OF CLOSE[i]>CLOSE[i+1], I REMOVED THEM TO MAKE IT SHORT

   if(close[i+7]>close[i+10]){x++;}
   if(close[i+7]<close[i+10]){x--;}

   if(close[i+8]>close[i+10]){x++;}
   if(close[i+8]<close[i+10]){x--;}
      
      
      ExtBuffer[i]=(x/0.45)*(100/(-100));
      Comment((x/0.45)*(100/(-100)), " ", close[i], " ", i);
     
     }
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
 

Nurudeen Amedu: you can see after the second vertical line, the value is 0 to the end the problem i have is that it stops 10 candles early and i really dont know why it does this

   for(int i=0;i<rates_total;i++){
      if(close[i+7]>close[i+10]){x++;}
  1. Had you looked in the Experts tab, you would know it has crashed with an array exceeded error. There are [0 .. rates_total-1] bars. When i is rates_total-10, then i+10 is rates_total which does not exist. Do your lookbacks correctly.
  2. You are also not setting close and ExtBuffer to AsSeries per OnCalculate
    To determine the indexing direction of time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[], call ArrayGetAsSeries(). In order not to depend on default values, you should unconditionally call the ArraySetAsSeries() function for those arrays, which are expected to work with.
 
whroeder1:
  1. Had you looked in the Experts tab, you would know it has crashed with an array exceeded error. There are [0 .. rates_total-1] bars. When i is rates_total-10, then i+10 is rates_total which does not exist. Do your lookbacks correctly.
  2. You are also not setting close and ExtBuffer to AsSeries per OnCalculate
Please can you give me the lines to change?

 
Nurudeen Amedu: can you give me the lines to change?
  1. Read the links provided.
  2. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.
 
whroeder1:
  1. Read the links provided.
  2. We're not going to code it for you (although it could happen if you are lucky or the problem is interesting.) We are willing to help you when you post your attempt (using SRC) and the nature of your problem.


it would have been a lot easier if you had just fixed it

but am also grateful you didn't, i just learned something new

 
Nurudeen Amedu: it would have been a lot easier if you had just fixed it but am also grateful you didn't, i just learned something new
Exactly. You would have learning nothing and would be forever asking similar question. My terse remarks have resulted in many posters putting in some effort and learning. And some have posted that they are grateful that they were forced to study.
Reason: