rates_total and prev_calculated ? - page 2

 
Yuniesky Carralero Cuellar:

Please edit your post and use the code button (Alt+S) when pasting code.

EDIT your original post, please do not just post the code correctly in a new post.

Also, what point are you trying to make with your post?

 
alphatrading #:

Actually that's not correct.

prev_calculated will not say something about what the indicator has or has not done. It will contain whatever you returned from the last call to OnCalculate(), i.e. during processing of the last tick. The documented name is a mis-nomer, instead of "prev_calculated" it should be "prevReturnValue". For example it will happily return any negative value you return from OnCalculate(). Or anything else completely unrelated to the indicator, e.g. tomorrows temperature of your local weather forecast (if you wish so).

You can use it for communicating a value to the next call (the intended functionality). But be aware that its usefulness is limited as the terminal will overwrite your return value with 0 (zero) if IndicatorCounted() returns 0 (zero). So, it's better to store values intended for processing by the next function call elsewhere, e.g. in a global variable.

General (and better understandable) approach:

Most of the examples in documentation, forum and code base on how to correctly resolve the amount of changed bars are wrong.

Thank you @alphatrading for this UNDERSTANDABLE explanation. 

 
alphatrading #:

Actually that's not correct.

prev_calculated will not say something about what the indicator has or has not done. It will contain whatever you returned from the last call to OnCalculate(), i.e. during processing of the last tick. The documented name is a mis-nomer, instead of "prev_calculated" it should be "prevReturnValue". For example it will happily return any negative value you return from OnCalculate(). Or anything else completely unrelated to the indicator, e.g. tomorrows temperature of your local weather forecast (if you wish so).

Actually that's not correct.

While you can return any value in OnCalculate(), prev_calculated will NOT be set to whatever value you returned on the last call. It will be bound between 0 and rates_total-1. You just can't use it for whatever you want.

You can use it for communicating a value to the next call (the intended functionality). But be aware that its usefulness is limited as the terminal will overwrite your return value with 0 (zero) if IndicatorCounted() returns 0 (zero). So, it's better to store values intended for processing by the next function call elsewhere, e.g. in a global variable.

General (and better understandable) approach:

Most of the examples in documentation, forum and code base on how to correctly resolve the amount of changed bars are wrong.
I am not interested to argue, but in my opinion this is a bad advice. If prev_calculated is 0 (set by the platform), there is a good reason and you need to take it into account.
 
Overweight:

Give me a simple explanation for rates_total and prev_calculated in OnCalculate function.

https://docs.mql4.com/basis/function/events

Recommended video explaining rates total, prev calculated indicator counted.



 
Print out rates_total and prev_calculated, then it will become much clearer that it is quite a large number.

All of the bars from zero to rates_total would be calculated at every Tick if we don't limit it. 

Most of them calculations do not need to be calculated over and over except the last bar which has to be recalculated at every Tick.

But if we say 'calculate only the bars in the difference between prev_calculated and rates_total' then the current bar gets calculated only once at formation when there is a difference of one bar between them. At next tick they will be the same number.

So we say if(rates_total==prev_calculated) go one index back and recalculate only the current bar for the new tick.
Reason: