How to do your lookbacks correctly #9 … #14 & #19
You have to add one because of what your returned.
How to do your lookbacks correctly #9 … #14 & #19
I know it has to do with the return value of rates_total, actually the thread you posted is the one that got me to stop using IndicatorCounted(), what I'm struggling to understand is the why it works, here's how I understand it currently:
-We have 100 bars in history (for example), all are new at first run.
-Limit then calculates to 99 (100 in rates total, 0 in prev_calculated, and -1 for the indexing)
-Since it's the first time, limit updates to 98 because of my lookback, and prev_calculated > 0 evaluates to false, that branch is ignored
-I iterate from bar 98 to bar 1 as for the loop.
The above I manage to understand, this is where I start struggling:
-I return rates_total, which in this case is 100, and I'm assuming the next tick that comes, will make prev_calculated to be 100 as well.
Next call comes in for OnCalculated within the same bar but on a new tick:
-Limit calculates to -1 (100 on rates total and prev_calculated, minus 1 for the indexing
-Since prev_calculated it's not 0, the next if doesn't execute anymore
-Since prev_calculated is now a positive value, limit recalculates to 0
-As for the for loop set-up, it won't execute as i starts to 0 and limit calculates to 0, so the condition its not validated. Let's briefly assume it's evaluating bar 0, so the condition i >= 0 will be true and indeed bar 0 will recalculate the value.
-I return 100 again as no new bars are in.
Let's assume the next tick comes and it belongs to a new bar
-Limit calculates to -1 (100 on rates total and prev_calculated, minus 1 for the indexing
-Since prev_calculated it's not 0, the next if doesn't execute anymore
-Since prev_calculated is now a positive value, limit recalculates to 0
-As for the for loop set-up, it won't execute as i starts to 0 and limit calculates to 0, so the condition its not validated. Let's briefly assume it's evaluating bar 0, so the condition i >= 0 will be true and indeed bar 0 will recalculate the value.
(At this stage I believe I partially answered my own question, but I want to confirm my understanding it's correct. In this case, even though the first few evaluations remain the same, bar 0 is now a new bar, so if I was in the loop indeed processing bar 0, this is the point where a new value gets plotted, but as for the example I posted, the new bar 0 wouldn't be processed at this stage)
-I return 101 again as we had 1 new bar.
Next tick within the same bar comes in:
-Limit calculates to -1 (101 on rates total and prev_calculated, minus 1 for the indexing
-Since prev_calculated it's not 0, the next if doesn't execute anymore
-Since prev_calculated is now a positive value, limit recalculates to 0
And here is the part where I'm not understanding why it works, in my loop I'm ignoring bar 0, and as for what I understand, the loop condition shouldn't meet as limit constantly evaluates to 0, which would work for i >= 0 (processing bar 0) but shouldn't for i > 0. I believe my struggle is coming around the how prev_calculated works.
-As for the for loop set-up, it won't execute as i starts to 0 and limit calculates to 0, so the condition its not validated. Let's briefly assume it's evaluating bar 0, so the condition i >= 0 will be true and indeed bar 0 will recalculate the value.
-I return 101 again as we don't have any new bars.
Any help to better understand this is highly appreciated.
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hey all!
I've been playing again with indicators, specially using the new events handler method with OnCalculate() instead of using IndicatorCounted().
I'm having some difficulties to fully understand why it works, here's the code:
Once again, I'm sure what I'm not fully understanding is very basic and simple, but well, as the saying goes, I can't know what I don't know, so any help to understand this properly is highly appreciated!