My live strategy did something very odd today and hoping to ask a few questions to help me debug and protect against this happening again. My strategy calculates OnBarClose using a 10sec dataseries, and a 1 tick dataseries. I set off my strategy around 9.20am and it had been running for approximately 40mins before it entered a Buy trade at 9:59:20 AM. It then immediately sent an exit order for the same time. In my backtest this doesn't happen. I print to output at various points in my strategy and its showing an odd scenario outlined below that I'm trying to debug please...
What happened:
Within my strategy only the 10sec bars can trigger a trade (1 tick series can trigger an exit). When a Buy Order is submitted it's set to print like this:
Print Code:
"Buying at Time: " +Time[0] + " and Bar: " +CurrentBar + ", Ticker: " + Instrument.FullName;
Within OnExecutionUpdate I'm trying to monitor the fills using the following code (not doing anything with it yet, just trying to log fills properly):
Code inc Print code
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) { if (entryOrder != null && entryOrder == execution.Order) { if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0)) { // We sum the quantities of each execution making up the entry order sumFilled += execution.Quantity; Print(execution.Time + ", Time: " + Time[0] + ", We sum the quantities of each execution making up the entry order, " + Instrument.FullName + ", " + execution.Order.OrderState + ", " + execution.Order.Filled + ", SUMFILLED: " + sumFilled + ", " + entryOrder); ...
Todays Output: 8/25/2022 9:59:20 AM, Time: 8/25/2022 9:53:41 AM, We sum the quantities of each execution making up the entry order, DRUG, Filled, 3600, SUMFILLED: 500, orderId='XXXXXX' account='XXXXXXX' name='BuyLong' orderState=Filled instrument='DRUG' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=3,600 tif=Gtc oco='' filled=3600 averageFillPrice=2.749385 onBehalfOf='' id=XXX time='2022-08-25 09:59:20' gtd='2099-12-01' statementDate='2022-08-25'
I've highlighted in underline and bold the odd thing. execution.Time = 9:59:20 AM while Time[0] =9:53:41 AM. Earlier at 9.53.41AM the price was much higher, and so my strategy sent a take-profit exit order because it believed the Close was much higher (~$2.90 vs $2.75). I ran a backtest afterwards and this order-and-immediately-exit issue never happened, so I'm trying to figure out what may have happened so I can code to avoid this in future. Here are my best guesses, but please let me know if you have any other ideas:
1. NT8 was up to speed processing 10sec bars but was delayed by almost 6mins processing ticks, and Time[0] was showing the time on the 1 tick series - (highly unlikely but listing it anyway)
2. A delayed tick was received at 9:59:20 AM for 9.53.41AM - as my code isn't checking the timestamp of each tick it processed its Close price as current and submitted a takeprofit exit.
Questions:
A) Can you think of any other reason why execution.Time & Time[0] would differ by 6mins?
B) If it is likely scenario #2, any advice to guard against this? E.g. should I always keep a record of the latest time, and if a tick arrives from the past ignore it?
C) Is the processing of onexecutionupdate tied into a dataseries or are updates here event tracked outside of dataseries timebounds? e.g. does OnExecutionUpdate only process if a tick is received, or a 10sec bar completes, or will it update on its on timeline?
Thanks in advance for any help you can provide with this
ChainsawDR
EDIT: In case it is helpful, the strategy was processing 3 days data (but had been running for 40mins and showed as green in the strategy tab as synced, so I presume it was all up to date).
Comment