Give me a simple explanation for rates_total and prev_calculated in OnCalculate function.
You might want to phrase your requests so that they don't seem like a demand.
rates_total is the same as the bars on a chart.
prev_calculated are the number of bars that the indicator has already calculated for.
Print( "prev = ", prev_calculated , "rates = " ,rates_total );
2018.12.24 14:21:35.508 prev = 16508 rates = 16508
//------------------------------------------------------
The program register the number of bars, and store it as prev caculated
Then at the end, the program return the numbers of bars, and compare it to the prev calculated
If it's still the same bar rates-total - prev_calculated = 0;
if a new bar has come meanwhile, then rates-total - prev_calculated = 1;
.
At the start of an indicator prev_calculated = 0;
so the program read all the bars until rates-total - prev_calculated = 0;
.
I think .. :-)
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:
// Bars = UnchangedBars + ChangedBars int UnchangedBars = IndicatorCounted(); int ChangedBars = Bars - UnchangedBars; for (int bar=ChangedBars-1; bar >= 0; bar--) { // update (i.e. recalculate) only the changed bars }Most of the examples in documentation, forum and code base on how to correctly resolve the amount of changed bars are wrong.
Agree with alphatrading that the best description of the prev_calcualated value is whatever was returned from OnCalculate on its last execution.
I made a utility to show the values of the OnCalculate parameters - easy to play with the return value in OnCalculate and see that: https://www.mql5.com/en/code/23841.
There is also an MQ4 version: https://www.mql5.com/en/forum/158115.
- www.mql5.com
prev_calculated are the number of bars that the indicator has already calculated for.
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.
OK, you are correct. But I have never had any reason to change the returned value and so I always have prev_calculated as the previous rates_total.
these 2 inner variable are mantince by MT it self, used to avoid duplicate caluction lots of datas. see others' answers above.
while you are reading and figure out the codes ,you just need to consider prev_calculated is equle to 0 and rates_total means all the bars (equle to the variable 'bars')
I understand that prev_calculated = 0 is passed to the first call of OnCalculate then it is incremented each time by one?
So inside OnCalculate, how would I handle all bars that are less than prev_calculated? If prev_calculated starts at, 0, then there's nothing to do as in:
for (int i=0; i < prev_calculated; i++) { }
我知道prev_calculated = 0传递给OnCalculate的第一次调用然后每次增加一次?
所以在OnCalculate中,我如何处理所有小于prev_calculated的柱? 如果prev_calculated从0开始,则无需执行任何操作,如:
rates_total-prev_calculated=1?
No. Prev_calculated is only what the indicator returned on the last call. Could be zero and rates_total-prev_calculated =rates_total! Could be rates_total and rates_total-prev_calculated=0!
If you return rates_total-1, you don't have to test prev_calculated for zero.
How
to do your lookbacks correctly.
Para MQL4 y MQL5
datetime L_TM = 0;
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 limit = ! L_TM ? Bars : iBarShift( Symbol(), Period(), L_TM ); /**/ L_TM = TimeCurrent();
for( int i = limit; ( i >= 0 ); i-- ) {
}
}
}
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Give me a simple explanation for rates_total and prev_calculated in OnCalculate function.
https://docs.mql4.com/basis/function/events