Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy is being disabled when backtesting although order reference is transitioned

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • NinjaTrader_Emily
    replied
    Originally posted by Padan View Post
    Hello Emily,

    thank you for your time.

    Yes, this is occurring in a backtest in the Strategy Analyzer (please also see attached image).

    I have exported the needed scripts in the RandomEntryError.zip archive. In order to reproduce the error, you would need to make a backtest with the TitanEdgeStrategyRandomEntryV1 strategy. For you convenience I have attached a template for the used parameters (TitanEdgeStrategyRandomEntryV1.zip), as well.

    In order for the error to pop up, you need to run the backtest several times (like 5 times), as it is a random entry strategy and the error only seems to occur when there is an active order when the backtest ends. I hope this is reproducable on your side.

    If you need something else, I'd be happy to help.
    Thank you for your reply.

    I do not see the RandomEntryError.zip archive - please try to attach it to your reply again so I may download the strategy and test it on my end.

    Leave a comment:


  • Padan
    replied
    Hello Emily,

    thank you for your time.

    Yes, this is occurring in a backtest in the Strategy Analyzer (please also see attached image).

    I have exported the needed scripts in the RandomEntryError.zip archive. In order to reproduce the error, you would need to make a backtest with the TitanEdgeStrategyRandomEntryV1 strategy. For you convenience I have attached a template for the used parameters (TitanEdgeStrategyRandomEntryV1.zip), as well.

    In order for the error to pop up, you need to run the backtest several times (like 5 times), as it is a random entry strategy and the error only seems to occur when there is an active order when the backtest ends. I hope this is reproducable on your side.

    If you need something else, I'd be happy to help.
    Attached Files

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Originally posted by Padan View Post
    Hello Emily,

    thank you for your support. I have added TradeOrders = true in State.SetDefaults. Please find attached an exported text file of the output.
    Hello Padan,

    Thank you for your reply.

    This is occurring in a backtest in the Strategy Analyzer, correct? In that case, it is certainly expected that the state would not reach State.Realtime so that behavior you have observed is expected. That said, however, I would not expect any historical orders to have transitioned to realtime either. If you are not testing in the Strategy Analyzer and instead are testing the script a different way, please advise how the test is being performed.

    Please provide a reduced sample of your strategy that I may test on my end along with the steps and parameters you are using to run this test so I may observe this behavior on my end and see if I can reproduce it.
    The reduced sample may be exported via the Control Center > Tools > Export > NinjaScript AddOn function, then the generated .zip file may be attached to your reply here.

    I appreciate your patience and look forward to further investigating this item.

    Leave a comment:


  • Padan
    replied
    Hello Emily,

    thank you for your support. I have added TradeOrders = true in State.SetDefaults. Please find attached an exported text file of the output.

    Attached Files

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Hello Padan,

    Thank you for your reply.

    Please enable TraceOrders in your strategy in order to get additional output regarding which order and action is resulting in the error. TraceOrders may be enabled in State.SetDefaults. For more information about using TraceOrders and prints to debug a strategy's behavior:


    If you are unsure of the meaning of the output, please right-click the NinjaScript Output window and select Save As to save the output to a text file. Then, attach the text file to your reply here. That is easier to work with than screenshots, since I can use Ctrl + F to find specific information in the text file.

    Thank you for your time and patience.

    Leave a comment:


  • Padan
    replied
    Hello Emily,

    thank you for your fast response. I have done as you advised and included a print statement to the OnStateChange event:

    Code:
    protected override void OnStateChange()
    {​
    Print("OnStateChange() state: " + State);
    // the rest of my OnStateChange() logic below
    }​​
    Furthermore I have changed the order in the OnOrderUpdate to first assign entryOrder before calling GetRealTimeOrder() as in your snippet. Please find attached two new log screenshots with these changes. I have also attached a screeshot right after the backtest has started because I found it interesting to see the print statement OnStateChange() state: Historical. I also read the pages you linked, that is where I got the code for transitioning the order reference from in the first place, thank you.

    The problem still exists. Even with the print statement in OnStateChange I can not find anything related to Realtime. Shouldn't be there a change to state Realtime somewhere? I mean the whole transitioning with GetRealTimeOrder can only work, if the strategy has a Realtime state at some point, right? Any help will be greatly appreciated.
    Attached Files

    Leave a comment:


  • NinjaTrader_Emily
    replied
    Hello Padan,

    Thank you for your post.

    What are the results if you simply add a print (not calling DebugMessage()) to the beginning of OnStateChange() that prints the state? For example:
    Code:
    protected override void OnStateChange()
    {​
    Print("OnStateChange() state: " + State);
    // the rest of your OnStateChange() logic below
    }
    What are the results if you assign entryOrder before calling GetRealTimeOrder()?

    Code:
    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled,
    double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    // 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);
    }​
    
    // 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);
    }
    
    // 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;
    }
    }​​


    It seems that in your output, there is a message that the order is placed but not a print that the order reference has been set yet before the error is thrown. The order must be assigned to the order object before attempting to call GetRealTimeOrder(), otherwise the strategy would wait for the next call of OnOrderUpdate() to call GetRealTimeOrder() and that could be the cause of the issues you are experiencing. More information about transitioning order references from historical to live may be found at the links below:



    Please let us know if we may be of further assistance.

    Leave a comment:


  • Strategy is being disabled when backtesting although order reference is transitioned

    I am developing a strategy, that uses the following entry, in order to enable the strategy to cancel an entry order after 10 bars:

    Code:
    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
    Before going into the issue, let me explain the "context": I did an old algorithm, that uses "unmanaged approach" from NT7 and never had any issues regarding orders, live or simulated. After transitioning to NT8 and rewriting it accordingly, I decided to do some tests, first under simulation, which has been

    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:


    Code:
    private Order entryOrder = null;​


    Code:
    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.

    Code:
    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.
    Click image for larger version  Name:	error_transitioning.png Views:	0 Size:	177.2 KB ID:	1297525

    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.


    Last edited by Padan; 03-28-2024, 02:49 AM.

Latest Posts

Collapse

Topics Statistics Last Post
Started by forgebench, Today, 02:22 PM
2 responses
9 views
0 likes
Last Post forgebench  
Started by MamaKB, 02-14-2025, 03:38 PM
10 responses
83 views
0 likes
Last Post Fertryd2  
Started by Irukandji, Today, 03:46 PM
0 responses
5 views
0 likes
Last Post Irukandji  
Started by ybforex, 03-11-2025, 05:30 PM
10 responses
95 views
0 likes
Last Post rockmanx00  
Started by HappyTrader76, Yesterday, 04:14 PM
8 responses
48 views
0 likes
Last Post HappyTrader76  
Working...
X