English

## [Solved] Please explain how to handle prev_calculated, somethings not making sense to me...

 22 enjoysmath 2015.07.22 00:23   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++) { } ``` So the code inside the loop won't get called, yet that's the logic I need for subsequent calls, so how is my perception wrong?
 Moderator3416 GumRai 2015.07.22 02:06 #   enjoysmath: 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:  So the code inside the loop won't get called, yet that's the logic I need for subsequent calls, so how is my perception wrong? prev_calculated returns the amount of bars (rates_total) from the previous tickSo if there are 1000 bars on the chart1st call it =02nd call etc =1000 When a new bar forms, the first tick received for that new bar, it returns 1000next tick etc it returns 1001 until another new bar opens
 22 enjoysmath 2015.07.22 02:10 #   GumRai: prev_calculated returns the amount of bars (rates_total) from the previous tickSo if there are 1000 bars on the chart1st call it =02nd call etc =1000 When a new bar forms, the first tick received for that new bar, it returns 1000next tick etc it returns 1001 until another new bar opens Let me see if I understand you correctly: First call of OnCalculate:       prev_calculated = 0 Second call of OnCalculate:      prev_calculated = 1000 ??????That would mean I would need to calculate the first 1000 on the first call?    Could you provide some if / else conditionals that demonstrate typical handling of prev_calculated - say all I want to do is duplicate high[] for example.Thank you for your response!!!
 22 enjoysmath 2015.07.22 02:42 #   enjoysmath: Let me see if I understand you correctly: First call of OnCalculate:       prev_calculated = 0 Second call of OnCalculate:      prev_calculated = 1000 ??????That would mean I would need to calculate the first 1000 on the first call?    Could you provide some if / else conditionals that demonstrate typical handling of prev_calculated - say all I want to do is duplicate high[] for example.Thank you for your response!!!  Okay, I've got it finally!  ```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 iBound = rates_total;       if (prev_calculated != 0) {       iBound = rates_total - prev_calculated;    }       int i;    for (i = 0; i < iBound; i++) {       smoothnessBuffer[i] = high[i];    }       return(rates_total); }```
 Moderator3416 GumRai 2015.07.22 03:12 #   Not quite```int i;    for (i = 0; i < iBound; i++) {       smoothnessBuffer[i] = high[i];```may not get the final tick value of high[1] (thats [one] not [i], easy to misread - one of the reasons I avoid using i).```int i;    for (i = 0; i <= iBound; i++) {       smoothnessBuffer[i] = high[i];```will make sure that you catch the final tick value. Also, if you use high[], you will need to set the array as series. If you use High[] instead of high[], then no need
 12995 WHRoeder 2015.07.22 14:00 #   There is the parameters high[] and the The predefined Variables - MQL4 Documentation High[]. Don't get them confused. high[] can be ordered either direction; High[0] is the current bar always. Ignore the parameters and just use ```int counted = IndicatorCounted(); int lookback = ... // iMA(period) has look back of period.                    // buffer[i+2] has look back of 2                    // use maximum of all. for(int iBar = Bars - MathMax(lookback, counted); iBar >= 0; --iBar) ... ```the buffers and The predefined Variables - MQL4 Documentation
 4 fridayda13_2016 2016.08.05 10:43 #   GumRai: Not quitemay not get the final tick value of high[1] (thats [one] not [i], easy to misread - one of the reasons I avoid using i).will make sure that you catch the final tick value. Also, if you use high[], you will need to set the array as series. If you use High[] instead of high[], then no needNot quite true either.If rates_total=1, then the loop should be only from 0 to 0, without the 1.What you suggest, adding equality sign (=) will overflow beyond rates_total number of bars in case prev_calculated=0;
 Moderator3416 GumRai 2016.08.05 14:37 #   fridayda13_2016: Not quite true either.If rates_total=1, then the loop should be only from 0 to 0, without the 1.What you suggest, adding equality sign (=) will overflow beyond rates_total number of bars in case prev_calculated=0; Yes, of course, silly me :)I should also have said that`   int iBound = rates_total;`should be `   int iBound = rates_total-1;`.
 12995 WHRoeder 2016.08.05 15:23 #   rates_total-1 doesn't handle your lookbacks correctly. My previous post should be (obviously) `for(int iBar = Bars -1 - MathMax(lookback, counted); iBar >= 0; --iBar) ...`