I have some code similar to the following in a custom backtest procedure. This code was written many years ago to compute various metrics. It is important to calculate the metrics properly – respecting the bars-in-range during a walk-forward optimization. I’m wondering if this style of programming is still needed?
In custom backtest code short version should be fine, as custom backtester works on ~~~EQUITY so it has number of bars equal to range, but general code (non custom backtest) usually has MORE bars than range because MAs, Ref() and all other functions require certrain number of bars BEFORE start of the range to be able to produce results from the start of the range. Say 10-bar MA needs 10 bars before start of the range to produce average since first bar in range.
@Tomasz After dozens of tests, I observed a run where BarCount was larger than the range. It happened during the custom-backtest portion of a walk-forward optimization. QuickAfl was enabled. I observed these values in a debug trace:
0 == BarIndex of firstBarInRange
3775 = BarIndex of lastBarInRange
4026 = BarCount
BarCount was exactly 250 bars longer than range. The walk-forward test was stepping forward one year at a time. I cannot reproduce this.