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

Sell your applications via the Market. It is secure!
64
2015.07.21 22: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?

Moderator
9526
2015.07.22 00: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 tick

So if there are 1000 bars on the chart

1st call it =0

2nd call etc =1000

When a new bar forms, the first tick received for that new bar, it returns 1000

next tick etc it returns 1001 until another new bar opens

64
2015.07.22 00:10
GumRai:

prev_calculated returns the amount of bars (rates_total) from the previous tick

So if there are 1000 bars on the chart

1st call it =0

2nd call etc =1000

When a new bar forms, the first tick received for that new bar, it returns 1000

next 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.

64
2015.07.22 00: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.

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[],
{
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);
}```

Moderator
9526
2015.07.22 01: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

14230
2015.07.22 12: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
6
2016.08.05 08:43
GumRai:

Not quite

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).

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

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;

Moderator
9526
2016.08.05 12: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;`

.

14230
2016.08.05 13:23
1. rates_total-1 doesn't handle your lookbacks correctly.
2. My previous post should be (obviously)
`for(int iBar = Bars -1 - MathMax(lookback, counted); iBar >= 0; --iBar) ...`
/