Indicators: Need help understanding lookback set-up

 

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:

// Here, limit is equal to rates_total - prev_calculated since rates_total equals how many bars there is on the chart
// and prev_calculated equals to how many bars the indicator already calculated. The -1 just adjusts for the indexing
// from 0 general approach. (1000 total bars mean index goes from 0 to 999).   
int limit = rates_total - prev_calculated - 1;

// If all the bars are new, I need to adjust limit for my lookback, pretty simple in this case as my lookback its just 1.   
if (prev_calculated == 0)
   limit--;
   
// This is the part I'm struggling to understand. If we have unseen bars, I need to add 1 to my lookback.
// The reason that I understand for this is because prev_calculated accounts bar 0 to be processed on the
// first tick, so even though a new tick arrives, prev_calculated assumes that bar to be already processed,
// therefore, to recalculate it, I need to add 1 bar to my limit for some reason (I know this is probably
// a very basic concept but I can't wrap my head around why doing this helps, as to my understanding, 
// prev_calculated would either way be 0 at a new tick from bar 0 as it'd be counted as processed
// if my assumption is correct, which, I think it's not). 
// Also, I know my assumption must not be correct as, even though my loop is set to ignore bar 0 (i > 0
// instead of i >= 0), without these lines it for some reason, it plots properly when attached to the chart
// but it won't update when a new bar is generated.
else if (prev_calculated > 0)
   limit++;
         
// Check the signal foreach bar
for (int i = limit; i > 0; i--)
{
...
}

// I'm aware that the line of prev_calculated > 0 can be deleted if I return rates_total - 1, which I find easier
// to understand about assuming there's one less bar on the chart, but I really want to understand how prev_calculated
// works as even though I read the documentation and some threads, I still can't get why it doesn't work without this
// even when ignoring bar 0 in the loop.
return rates_total;

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! 

 
You have to add one because of what your returned.
          How to do your lookbacks correctly #9#14 & #19
 
William Roeder:
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.