int barsAgo = Calculate == Calculate.OnBarClose || State == State.Historical ? 0 : 1; if (IsFirstTickOfBar) { if (Close[barsAgo] > High[barsAgo + 1]) ExitLong(); if (Time[0].Date == new DateTime(2020,04,15).Date) EnterLong(); }
In the above example the script tries to get the right bar index to use based on how the script is being run. It runs as expected for Calculate == OnBarClose but for Calculate == OnPriceChange, barsAgo == 0 when I expect barsAgo == 1.
When running on Calculate == OnPriceChange and starting the strategy the backfll logic processes and when the OnBarUpdate is called for the last bar on the chart the State == Historical and IsFirstTickOfBar is true. But I'm starting the strategy in the middle of the formation of this last bar. I expected that IsFirstTickOfBar should be false.
THE TEST SCRIPT ATTACHED:
AtsSampleStrategy.zip
I run the script on the ES 06-20 1440 minute bars, CME US Index Futures ETH.
Set MarketReplay to 4/17/2020 midnight.
This simple script enters long on 4/15/2020 then looks for a close higher than the prior day high. The bar time stamped 4/17/2020 meets this criteria.
With Calculate == OnBarClose the exit order is submitted at the close of the 4/17 bar as expected.
With Calculate == OnPriceChange the exit order is submitted before the 4/17 bar closes. Because of the afformentioned problem the script calls ExitLong. I would have expected that the result of this unwanted call would take the strategy to flat but it does not. An order is submitted however and the a short position in the sim account while the strategy remains in the long position. This is a secondary issue .
I test with Market Replay and set the time to 4/17/2020 which begins at midnight and the 4/17 bar has already started to form.
I expect that there's some way to write a formula to create a barsAgo value that will be correct for both OnBarClose and OnEachTick/OnPriceChange. I just don't know how. Is there a way?
AtsSampleStrategy.zip
Comment