Custom Indicator fails to update on new candle after intial load

 

Hi, I created a custom indicator that doesn't update or move along with new candles on the chart. I think it has something to do with the way I am using rates_total and prev_caculated but I'm not sure. Currently, my array buffers are "TRUE" for ArraySetAsSeries() and  I have tried several different methods to access them with varying result. Please see the code I am using below -

   int limit = 99;
   int min_rates_total = limit;
   int values_to_copy;
   if(prev_calculated>rates_total || prev_calculated<=0)
      values_to_copy=rates_total;
   else
     {
      values_to_copy=rates_total-prev_calculated;
      //--- last value is always copied
      values_to_copy=limit+1;
     }


//----------------

for(int i=limit; i>-1 && !IsStopped(); i--)
     {
}


return rates_total-1;

Any help form someone skilled in MQL5 would be appreciate.

Thanks
 
  1. See How to do your lookbacks correctly #9 - #14 & #19.

  2. int limit = rates_total - MathMax(LOOKBACK, prev_calculated);
    int values_to_copy = limit;
    ⋮
    for(int i=limit-1; i>-1 && !IsStopped(); i--) 
    ⋮ 
    return rates_total-1;
 

Amazing! It worked. I have been trying to fix this for a while. Thank you.


Comparing your edits to the code and your other lookback examples, I think I'm beginning to understand how it works. I have to do more research to figure out where I went wrong.


I had previously used a version of the code below when I first ran into problems. Unfortunately, it didn't work because I messed up somewhere.


   for(int iBar = Bars(_Symbol,PERIOD_CURRENT)-1-MathMax(lookback, prev_calculated); iBar >= 0; --iBar){
   }
   return rates_total-1; // Recalculate current bar next tick.


Thanks again for your help!

Reason: