Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Strategy is being disabled when backtesting although order reference is transitioned

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

    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.

    #2
    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.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      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

      Comment


        #4
        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.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          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

          Comment


            #6
            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.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              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

              Comment


                #8
                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.
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Here it is. Sorry, I was sure I had attached it.
                  Attached Files

                  Comment


                    #10
                    Originally posted by Padan View Post
                    Here it is. Sorry, I was sure I had attached it.
                    No worries! I was able to download the attachment now. I will test this once I have a chance and follow up with you when I have an update, which should be early next week. In the meantime, please don't hesitate to reach out with any additional questions or concerns.

                    Thank you for your patience.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      Hello Padan,

                      Thanks again for your patience.

                      I was able to reproduce the error on my end, and I suspect it is caused by the inheritance you are attempting in the TitanEdgeStrategyRandomEntryV1 script:
                      Code:
                          public class TitanEdgeStrategyRandomEntryV1 : TitanEdgeStrategyBaseV3
                      I do see that the TitanEdgeStrategyBaseV3 script is properly inheriting from the Strategies class:
                      Code:
                          public class TitanEdgeStrategyBaseV3 : Strategy
                      Having the TitanEdgeStrategyRandomEntryV1 file inherit from TitanEdgeStrategyBaseV3 is not supported. Please consolidate your logic into one strategy file and test it out; I suspect that this will not run into the same issues and will behave as expected. Currently, since you are inheriting from a strategy, no assumptions can be made about the State of either script and likely the issue is arising based on the States of each script and OnStateChange(). Ultimately, NinjaTrader does not support inheritance with Indicators, Strategies, or other script types because this breaks the NinjaTrader generated code for method overloads and also breaks optimizations and property grids.

                      Unfortunately, NinjaTrader cannot support inheritance with Indicators, Strategies, or any of the other provided script types, other than custom addon classes that open in their own window outside of the structures of NinjaTrader windows.

                      I see you already have an AddOn with custom methods. I suggest adding all helper logic and/or custom methods you'd like into that same file, or you could use partial classes in the Indicators or Strategies namespace. For more information, please see the linkss below:
                      I've built a large framework on top in ninjatrader. Some of my indicators require many levels of inheritance. It works just fine, however I recently noticed that if there have been more then 2 levels of inheritance on an indicator then the auto generated code doesn't update properly. Specifically let's say C inherits from B



                      Thank you for your understanding. Please feel free to reach out with any additional questions or concerns.
                      Emily C.NinjaTrader Customer Service

                      Comment


                        #12
                        Hello Emily,

                        first of all, thank you for your efforts!
                        You and the whole support team are really awesome and do play a large role for me personally to select NinjaTrader as trading software.

                        I have consolidated all the logic into one strategy file so there is no longer any inheritance, but still get the error.
                        I have attached a new archive containing the consolidated version of the strategy. ExportConsolidated2.zip

                        Can you reproduce the error as well, with the consolidated-no-inheritance version of the strategy? Any help would be highly appreciated.

                        UPDATE: I have updated the archive with a version of the strategy which always generates the error. The error occurs when there is an active working limit buy order in the moment the backtesting period ends.


                        Attached Files
                        Last edited by Padan; 04-02-2024, 06:37 AM.

                        Comment


                          #13
                          Hello Padan,

                          Thank you for your reply.

                          Please advise what strategy settings you are using that always generate the error. I imported the file and used the attached settings without errors. If you have a specific template you are using, please compare it to these settings to see what might be different between the two that could result in the error.

                          I look forward to your reply.
                          Attached Files
                          Emily C.NinjaTrader Customer Service

                          Comment


                            #14
                            Hello Emily,

                            I have attached a template file with the settings for the strategy ( TitanEdgeStrategyRandomEntryV4.zip ). It is important to note, that the session template for the backtest must be Default 24x5.

                            All the individual settings for the strategy do not matter too much, as the code now only should place a limit buy order at 23:51:01. This limit buy order should then be valid till the end date of the backtest (25.03.2024)

                            Code:
                                        if(ToTime(Time[0]) >= 235100 && ToTime(Time[0]) < 235101) {
                                            Signal(TitanEdgeNamespace.TradeDirection.Long, TitanEdgeNamespace.OrderType.Limit, Close[0] - (3 * TickSize));
                                        }​
                            AND in the Signal function.
                            Code:
                            EnterLongLimit(0, true, Convert.ToInt32(DefaultQuantity), entryPrice, LongPositionTier1);
                            Using these settings above the error occurs for me all the time. If you change the Bars until cancel parameter in the Entry section of the properties to e.g. 8. the order will be cancelled before the backtest finishes and there is no error. As far as I understand the whole subject, the error occurs when there is an active buy limit with isLiveUntilCancelled = true order when the backtest ends.

                            I have created a ''fix'' for this strange behavior which cancels any active entry order in the case of a backtest one minute before the backtest ends and added it in the OnBarUpdate event. This seems to fix the issue as well, but I am sure this issue should not arise at all in the first place.
                            Code:
                            ​if(IsInStrategyAnalyzer && (State == State.Historical) && HasActiveEntry() ) {
                            if(Time[0] > To.AddMinutes(-1)) {
                            // CancelAllOrders();
                            }
                            }​



                            Comment


                              #15
                              Originally posted by Padan View Post
                              Hello Emily,

                              I have attached a template file with the settings for the strategy ( [ATTACH]n1298055[/ATTACH] ). It is important to note, that the session template for the backtest must be Default 24x5.

                              All the individual settings for the strategy do not matter too much, as the code now only should place a limit buy order at 23:51:01. This limit buy order should then be valid till the end date of the backtest (25.03.2024)

                              Code:
                              if(ToTime(Time[0]) >= 235100 && ToTime(Time[0]) < 235101) {
                              Signal(TitanEdgeNamespace.TradeDirection.Long, TitanEdgeNamespace.OrderType.Limit, Close[0] - (3 * TickSize));
                              }​
                              AND in the Signal function.
                              Code:
                              EnterLongLimit(0, true, Convert.ToInt32(DefaultQuantity), entryPrice, LongPositionTier1);
                              Using these settings above the error occurs for me all the time. If you change the Bars until cancel parameter in the Entry section of the properties to e.g. 8. the order will be cancelled before the backtest finishes and there is no error. As far as I understand the whole subject, the error occurs when there is an active buy limit with isLiveUntilCancelled = true order when the backtest ends.

                              I have created a ''fix'' for this strange behavior which cancels any active entry order in the case of a backtest one minute before the backtest ends and added it in the OnBarUpdate event. This seems to fix the issue as well, but I am sure this issue should not arise at all in the first place.
                              Code:
                              ​if(IsInStrategyAnalyzer && (State == State.Historical) && HasActiveEntry() ) {
                              if(Time[0] > To.AddMinutes(-1)) {
                              // CancelAllOrders();
                              }
                              }​


                              Hello Padan,

                              Thank you for your reply.

                              I was not able to reproduce the error with the template provided. I have attached both the settings as well as the output after running a backtest several times with the provided template.

                              One thing I noticed is that in the export file, it says you exported from version 8.0.27.0. What are the results if you update to 8.0.28.0 or even optionally upgrade to NinjaTrader 8.1?

                              To update to the latest version of NinjaTrader Desktop please follow the steps below.
                              • Login into the NinjaTrader Account Dashboard
                              • Click "Download" in the bottom left-hand corner of the sidebar
                              • Either select "Download" in the "Most Recent Release" Window in the main screen to upgrade to 8.1 or select the Download option for Version 8.0.28.0 in the "Prior Releases" section on the right-hand side
                              • Once the installation had downloaded, ensure any previous version of NinjaTrader is closed before running the installation package.
                                • Note: You may need to locate this in your "Downloads" folder in a Windows File Explorer

                              If you have updated from 8.0 to 8.1 and would prefer to remain on 8.0 please follow the steps below to migrate your version.

                              Please follow the steps below to migrate from the 8.1 platform back to the 8.0 platform:
                              • Make sure all versions of NinjaTrader are Closed
                              • Go to the Windows Control Center > Click “Add/Remove Programs” (Sometimes “Uninstall a program” > Click NinjaTrader > Click Uninstall
                              • Click Next through the prompts until it is finished uninstalling
                              • Click the link below to directly download the Installer for version 8.0.28.0
                              • After downloading, click on the file and Open or Run it > Click Next through the prompts until it is Finished installing
                                • From here you can reopen NinjaTrader and continue using the platform like before.

                              Please let me know the results.
                              Attached Files
                              Emily C.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by futtrader, 04-21-2024, 01:50 AM
                              4 responses
                              41 views
                              0 likes
                              Last Post futtrader  
                              Started by Option Whisperer, Today, 09:55 AM
                              1 response
                              11 views
                              0 likes
                              Last Post bltdavid  
                              Started by port119, Today, 02:43 PM
                              0 responses
                              1 view
                              0 likes
                              Last Post port119
                              by port119
                               
                              Started by Philippe56140, Today, 02:35 PM
                              0 responses
                              3 views
                              0 likes
                              Last Post Philippe56140  
                              Started by 00nevest, Today, 02:27 PM
                              0 responses
                              2 views
                              0 likes
                              Last Post 00nevest  
                              Working...
                              X