When I run a back-test of a simple strategy on a stock provided by the free Kinetick data feed in Strategy Analyzer on a strategy whose Calculate property is set to Calculate.OnBarClose or Calculate.OnEachTick, OnBarUpdate() is not called on the last daily bar on the chart, even if the close time of the last daily bar on the chart is one or even multiple days in the past.
For example, if the only line of code in the OnBarUpdate method of a NinjaScript strategy is:
Print(Time[0].ToString());
it will print the End Dates of all the daily bars in the back-test up to and including the second to last daily bar of the chart, but not the end date of the last daily bar on the chart.
This occurs even if I am back-testing on daily bars whose last bar was on Friday and I run the back-test on Saturday. In the example in the previous sentence, the last End Date that the "Print(Time[0].ToString());" line will print will be for Thursday's daily bar, even though there is a daily bar on Friday and I ran the back-test on Saturday.
I noticed that the last bar is not skipped in two scenarios:
1. When back-testing in the Strategy Analyzer and the strategy's Calculate property is set to Calculate.OnPriceChange.
2. When the strategy is applied to a chart, even if it is Saturday and there is no live data coming in.
My strategy needs to do a lot of work on the last bar of the chart from a back-test or optimization from within Strategy Analyzer, for instance generate a file with a list of positions that should be entered the next day for all S&P 500 stocks via a back-test of the entire basket of S&P 500 stocks via the Strategy Analyzer.
How can I do this on the actual last bar on the chart, not the second to last bar?
If I just set the strategy's Calculate property to Calculate.OnPriceChange to gain access to the last bar, I am concerned that this may cause other issues for my strategy, which logically should only run on each bar close and should not skip bars for any reason.
I've read that this might be doable in the OnStateChange() method when (State == State.Terminate) but this seems a bit risky and less straightforward than a solution whose code could be written in OnBarUpdate() if the bar is determined to be the last bar on the chart.
Thanks in advance!
EquityTrader
Comment