This is a rather long winded enquiry, so please bear with me.
I am trying gain a fundamental understanding of why some Indicators do not plot correctly (or at all) when performing Strategy backtesting using the Strategy Analyser (e.g. the "StrategyPlot()" custom indicator which is known not to work for charts in the Strategy Analyser).
To this end, I am specifically looking to understand how the Strategy Analyser performs backtests for any given strategy for any given set of data.
I am led to understand that when performing becktests, optimisations are put in place so that indicators "OnBarUpdate" method is not called unless specifically forced to (using "Update()" to make sure that a correctly updated value is returned back to the strategy from the indicator).
To this end I set up a very simple custom strategy that calls a very simple custom indicator. The indicator just plots "Close[0]" on a line graph. It also has one exposed public variable which is updated (via "update()") each time the strategy "OnBarUpdate" method is executed. A series of print statements allow me to trace through the strategy and indicator as it is executed within the Strategy Analyser. Printed outputs consist of "Initialize Reached" and "OnBarUpdate Reached" for both Strategy and Indicator, as well as "CurrentBar", "ExposedVariable" and "Close[0]" values for the Indicator. The backtest data contents is consists of a custom dataset, whose values for this test are irrelevant, except to say that there is 1798 data bars present.
When I run the backtest, I get the following generic output (assuming that the Output Window show the Print output in the correct sequence).
Initialize Strategy - Expected
Initialize Indicator - Expected
For Each of the subsequent 1798 Data Bars:
OnBarUpdate - SATestIndicator - Expected
OnBarUpdate - SATestStrategy - Expected
Correct and consistent CurrentBar Numbers - Expected
ExposedVariable and Close[0] are showing correct values - Expected
I would have thought that would be it, but here's where it gets interesting... next is
Initialize SATestStrategy
Initialize SATestIndicator (three times)
OnBarUpdate - SATestIndicator (for CurrentBar 0 through to CurrentBar 1487 - Strategy is not includeed this time)
ExposedVariable is NOT updated.
Close[0] has correct value.
Thats It... End of output...
This last part (second loop through the data) is the part I am trying to understand, and can't seem to find it in the documentation. Could someone please let me know whether my observation below is in any way correct?
The example is a representive example., so you will see the same kind of behaviour for any dataset during a backtest. I'm just trying to understand the methodology (so that I can avoid any related pitfalls along the way for myself)
I know the initialize gets called a few times. I don't care about this and am told that it is necessary for "other" reasons. Ok.
Backtest Observation: It's almost like The first run through is where each bar is passed to the Strategy itself (which will only update/interact with any Indicators if explicitly told to via "update()". No plots are generated from this run, just strategy entries/exits etc. The second run through is where the Plot for the indicator is generated and therefore only runs through the indicator only, and it is only passed the price data (ExposedVariable is not updated in the example). So anything that is going to be plotted correctly, during the backtes needs to be generated from the price feed itself (no reliance on variables being set from the strategy). Is this observation correct? Anyone have anything further to add or any other questions relating to this topic?
Question: Why stop at Bar number 1487 out of 1798?
Thanks and regards,
Sc1entia

Comment