EnterLongLimit(0, true, Convert.ToInt32(DefaultQuantity), entryPrice, LongPositionTier1);
I understand, that if using true for the isLiveUntilCancelled parameter, the strategy has to take care of the order reference transitioning. I read all the posts in this forum I found regarding this subject.
Basic knowledge pages:
Specific user questions handling this problem
https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?using_cancelorder_me thod_to_ca.htm
etc.
I therefore have included the order reference transitioning in the strategy as follows:
private Order entryOrder = null;
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) { // One time only, as we transition from historical to realtime (State == State.Realtime) // Convert any old historical order object references to the live order submitted to the real-time account if (entryOrder != null && entryOrder.IsBacktestOrder && State == State.Realtime) { entryOrder = GetRealtimeOrder(entryOrder); DebugMessage("Transitioning to realtime: OnOrderUpdate"); Print(entryOrder); } // Assign entryOrder in OnOrderUpdate() to ensure the assignment occurs when expected. // This is more reliable than assigning Order objects in OnBarUpdate, as the assignment is not guaranteed to be complete if it is referenced immediately after submitting if (order.Name == LongPositionTier1) { DebugMessage("Setting order reference: OnOrderUpdate"); entryOrder = order; // Print(entryOrder); } // Evaluates for all updates to entryOrder if (entryOrder != null && entryOrder == order) { // Reset the entryOrder object to null if order was cancelled without any fill if (order.OrderState == OrderState.Cancelled && order.Filled == 0) entryOrder = null; if (order.OrderState == OrderState.Filled) entryOrder = null; } }
I also have included a debug print in the OnStateChange event in the case of State == State.Realtime to see when this is happening.
else if(State == State.Realtime) { DebugMessage("Transitioning to realtime: OnStateChange");
Now when backtesting my strategy on the ES 06-24 for a past period like e.g. a start date of 24/03/2024 and end date of 25/03/2024 I get the
error: Strategy has been disabled because it attempted to modify a historical order that has transitioned to a live.Please see attached NinjaScript Output screenshot.
I only use SetStopLoss and SetProfitTarget to exit positions.
Following question:
As you can see from the output screenshot neither the debug message Transitioning to realtime: OnStateChange nor the message Transitioning to realtime: OnOrderUpdate are being printed.
My assumption is therefore, that the Strategy never reaches the State == State.Realtime, therefore no transitioning of the order references will occur.
But how is it possible to get the error: Strategy has been disabled because it attempted to modify a historical order that has transitioned to a live order if the Strategy never reaches State.Realtime? What am I missing? I am grateful for any suggestion or idea. Thank you for your time.
Comment