How to calculate an indicator for Max number of Bars - page 2

 
Muhammad Elbermawi:

Thanks a lot Petr, this code do the job. I hope you can explain this lines in more details.

My former code doesn't watch if the parameter MaxCandles is greater than number of bars. It could cause error. I've updated the code to avoid that.

The code sets all the new candles (those weren't set to close before) to close (the first loop) and all the old candles (those weren't set to zero before) to zero (they aren't drawn) (the second loop). "Old candles" are candles those are to the left from count of number of MaxCandles candles. My explanation is really strange but if you watch the code it must be clear.

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 from,limit,uncalculated=rates_total-prev_calculated;   
   limit=(uncalculated+1)>MaxCandles?MaxCandles:((uncalculated+1)>rates_total?rates_total:(uncalculated+1));
   for(int i=0;i<limit;i++) zxc[i]=close[i];
   from=MaxCandles>rates_total?rates_total:MaxCandles;
   limit=(from+uncalculated)>rates_total?rates_total:(from+uncalculated);
   for(int i=from;i<limit;i++) zxc[i]=0.0;
   return(rates_total);
  }

The rest of code is the same.

 
Petr Nosek:

My former code doesn't watch if the parameter MaxCandles is greater than number of bars. It could cause error. I've updated the code to avoid that.

The code sets all the new candles (those weren't set to close before) to close (the first loop) and all the old candles (those weren't set to zero before) to zero (they aren't drawn) (the second loop). "Old candles" are candles those are to the left from count of number of MaxCandles candles. My explanation is really strange but if you watch the code it must be clear.

The rest of code is the same.

Thanks a lot Petr, I tried the new code and  it work fine.
Reason: