Why prev_calculated jump between 0 and Bars ?

 

Why  prev_calculated jump between 0 and Bars ?

  prev_calculated  means last calculated bars, but now only  is 0 or Bars

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[])
  {
  Print(prev_calculated," ",Bars(Symbol(),PERIOD_CURRENT));

............ 

2011.10.20 12:27:29 123forEA15 (EURUSD,M5) 5015 5015
2011.10.20 12:27:29 123forEA15 (EURUSD,M5) 0 5015
2011.10.20 12:27:28 123forEA15 (EURUSD,M5) 5015 5015
2011.10.20 12:27:28 123forEA15 (EURUSD,M5) 0 5015
2011.10.20 12:27:26 123forEA15 (EURUSD,M5) 5015 5015
2011.10.20 12:27:26 123forEA15 (EURUSD,M5) 0 5015
2011.10.20 12:27:25 123forEA15 (EURUSD,M5) 5015 5015
2011.10.20 12:27:24 123forEA15 (EURUSD,M5) 0 5015


..................................................................... 

 

prev_calculated is what OnCalculate() returned in pprevious call. See help https://www.mql5.com/en/docs/basis/function/events#oncalculate:

We should noted the connection between the return value of OnCalculate() and the second input parameter prev_calculated. During the function call, the prev_calculated parameter contains a value returned by OnCalculate() during previous call. This allows for economical algorithms for calculating the custom indicator in order to avoid repeated calculations for those bars that haven't changed since the previous run of this function.

For this, it is usually enough to return the value of the rates_total parameter, which contains the number of bars in the current function call. If since the last call of OnCalculate() price data has changed (a deeper history downloaded or history blanks filled), the value of the input parameter prev_calculated will be set to zero by the terminal.

Note: if OnCalculate returns zero, then the indicator values are not shown in the DataWindow of the client terminal.

Documentation on MQL5: Language Basics / Functions / Event Handling Functions
  • www.mql5.com
Language Basics / Functions / Event Handling Functions - Documentation on MQL5
 

Thanks, i mixed it with mql4's IndicatorCounted();

is  rates_total equal to Bars(Symbol(),PERIOD_CURRENT)    ? 

 

 
I think it must be so. Try to check yourself.
 

Hello to all.. For what it's worth, this is the way I understand it to work at this time.

On the first run as the indicator is loaded, prev_calculated will be zero because there is no previous run from which to have a calculation. After the indicator is loaded and OnCalculate() has run one time, it will be the same as the rates_total which is the same as the number of bars on the chart. ALL the bars.. including the one in progress.

They will all be the same until a new bar starts at which time the rates_total will instantly increase by 1. For the first tick of THAT candle rates_total will be 1 more than prev_calculated because the OnCalculate() has not yet run since this new bar came into existence. After that first tick, prev_calculated will be the same as rates_total once again. 

It should be noted that IndicatorCounted() is NOT the same as prev_calculatedprev_calculated returns ALL the bars on the chart. IndicatorCounted() is "The amount of bars not changed after the indicator had been launched last." In other words... it returns all of the FINISHED bars.. NOT the one in progress.. so IndicatorCounted() will NOT count the current bar in progress. Therefore if there are 1000 indexed 0-999 bars on a chart... the rates_total is 1000.. the prev_calculated is zero on first run and then quickly becomes 1000 after the first run. The IndicatorCounted() also returns a zero BUT after the first run it will return 999. Which is the number of FINISHED bars and also the index of the oldest bar on the chart.

As to why your prev_calculated was returning to zero.. it could be that your terminal was loading more data and filling in history or the chart was being scrolled back forcing a change in the number of bars on the chart.. note this....

If since the last call of OnCalculate() price data has changed (a deeper history downloaded or history blanks filled), the value of the input parameter prev_calculated will be set to zero by the terminal." 

so to recap.. if we have 1000 bars...

before the first run of OnCalculate()...

rates_total = Bars =1000 (difference is 0)

prev_calculated =0         (difference is Bars)

IndicatorCounted() =0    (difference is Bars)

after the first run of the OnCalculate()

rates_total = Bars=1000 (difference is 0)

prev_calculated = rates_total = Bars=1000 (difference is 0)

IndicatorCounted() = rates_total-1 = Bars-1 = 999 (difference is 1)

 On the first tick of a new bar... 

rates_total = Bars=1001 (difference is 0)

prev_calculated = rates_total-1 = Bars-1 = 1000 (difference is 1)

IndicatorCounted() = rates_total-2 = Bars-2 = 999 (difference is 2)

PipPip.... Jimdandy 

 
James Hodges: On the first run as the indicator is loaded, prev_calculated will be zero because there is no previous run from which to have a calculation. After the indicator is loaded and OnCalculate() has run one time, it will be the same as the rates_total 

It will be the same as the (previous) rates_total only if On_Calculate returns rates_total.

If you look at my code you see I return Bars - 1, so on the next call it will recalculate bar zero again. See how to do your lookbacks correctly.

Reason: