Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Transitioning a managed order from historical to live

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

    Transitioning a managed order from historical to live

    Hello,


    I've read the documentation here:https://ninjatrader.com/support/help...ed_order_handl ing.htm
    around order handling, but am still getting the pop up saying that the strategy has been disabled because it's trying to modify a historical order and have some questions.

    I have a strategy which uses entry signals to enter like so:

    Code:
    SetStopLoss(lf1, CalculationMode.Price, tp2 - 0.5, false);
    SetProfitTarget(lf1, CalculationMode.Price, stop - 0.5);
    EnterLongLimit(BarsInProgress, true, 2, entryPrice, lf1);
    Later on after it's entered I would call SetStopLoss/SetProfitTarget

    Code:
    SetStopLoss(lf1, CalculationMode.Price, newStop, false);

    In this case though I'm not doing anything with the order - how can I get around the error? Do I modify the Active

    Thanks!

    #2
    Hello -kman-,

    That is due to the use of an IsLiveUntilCancelled order which is your EnterLongLimit. Because you are using IsLiveUntilCancelled you need to implement the OnOrderUpdate logic as shown in the help guide to collect the order to a variable so it can be transitioned.

    Comment


      #3
      Hi Jesse,

      I think that's the question. I can get the order object...but what do I do with it? The samples I've seen just replace one private variable with another.

      But what do I do if I'm just calling setstoploss with the entry signal?

      Comment


        #4
        Hello -kman-,

        Did you take a look at the sample in the following link? That shows how to use GetRealtimeOrder inside the OnOrderUpdate event.

        Code:
        // One time only, as we transition from historical
        // 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);






        Comment


          #5
          I did read it, but my question remains. I call SetStopLoss/SetProfitTarget with just the entry signal. I don't deal with the order object at all and am not sure what I need to do.

          Concretely, suppose we have this:

          Code:
          // Historical
          SetStopLoss(lf1, CalculationMode.Price, tp2 - 0.5, false);
          SetProfitTarget(lf1, CalculationMode.Price, stop - 0.5);
          entryOrder = EnterLongLimit(BarsInProgress, true, 2, entryPrice, lf1);
          
          // Later on - State == State.Realtime
          newOrder =GetRealtimeOrder(entryOrder);
          // Ok I have newOrder and I want to call this...what do I need to change?
          SetStopLoss(lf1, CalculationMode.Price, newStop, false);

          Thx!

          Comment


            #6
            Hello -kman-

            If your entry hasnt filled the stop and target won't have been submitted yet.


            The code you have shown is incorrect, you need to use OnOrderUpdate along with the code shown in that sample, that's the sample I posted in the last reply.

            You are using a live until cancelled order so you do need to work with order objects in that case, the sample in the help guide shows how to do that. Your code would look identical to that sample to work with the order and transition it.

            Comment


              #7
              Hey Jesse,

              My code was shrunk to be terse - my actual code does what the sample asks.

              Code:
              private Order activeOrder;
              protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
              {
                 if (activeOrder != null && activeOrder.IsBacktestOrder && State == State.Realtime)
                   activeOrder = GetRealtimeOrder(activeOrder);
              }
              
              
              protected override void OnBarUpdate()
              {
                 if(entryCondition)
                    activeOrder = EnterLongLimit(BarsInProgress, true, 2, entryPrice, "myEntrySignal");
              
                 if(IsFirstTickOfBar && moveStopLossCondition)
                 {
                    SetStopLoss("myEntrySignal"...., newStop);
                  }
              }


              So again - here's the question: I'm trying to call SetStopLoss after the order has transitioned to realtime...what do I do with the new activeOrder object?

              Or are you telling me I can't call SetStopLoss with the same myEntrySignal variable...?

              Comment


                #8
                Hello -kman-

                the Set methods work based on the fill of the entry order, those won't be working while your entry order is working. The Set methods use the entries signal name to associate themselves with the entry. The activeOrder is just a reference to your entry order so it can be transitioned.

                The code you have shown is missing some parts, please ensure you are fully reviewing the sample in the help guide and ensure you code has all 3 concepts listed in OnOrderUpdate. You need to manage the variable by assigning it (your code doesn't have that part). You need to transition it when you enter realtime. You need to set it to null when the order is filled (Your code doesn't have that part). All of what is shown in that sample is a requirement for that to work.




                Last edited by NinjaTrader_Jesse; 06-22-2023, 09:51 AM.

                Comment


                  #9
                  Hi Jesse,

                  As far as I can tell, the sample allows me to keep a reference to the orders that are created, and then the transition is just replacing my private variable with the new one. But my code's reference is my code's reference. The sample doesn't actually use the order to do anything.

                  But let me see if I understand you. This code below has the 3 things that the sample asks for.

                  All that I see happening is that I keep my own reference to the order.

                  Code:
                  private Order activeOrder;
                  protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string comment)
                  {
                    if (activeOrder != null && activeOrder.IsBacktestOrder && State == State.Realtime)
                      activeOrder = GetRealtimeOrder(activeOrder);
                  
                    // Null Entry order if filled or cancelled. We do not use the Order objects after the order is filled, so we can null it here
                    if (activeOrder!= null && activeOrder == order)
                    {
                      if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                        activeOrder= null;
                  
                      if (order.OrderState == OrderState.Filled)
                        activeOrder= null;
                    }​
                  }
                  
                  protected override void OnBarUpdate()
                  {
                    if(entryCondition)
                      activeOrder = EnterLongLimit(BarsInProgress, true, 2, entryPrice, "myEntrySignal");
                  
                    if(IsFirstTickOfBar && moveStopLossCondition)
                    {
                      SetStopLoss("myEntrySignal"...., newStop);
                    }
                  }​
                  Are you telling me that just because I set my local activeOrder variable to null that SetStopLoss will suddenly start working when we transition from Historical to Realtime? I'm assuming that internally SetStopLoss keeps its own reference to the order that's bound to "myEntrySignal". Why does me keeping a reference around and then setting it to null affect anything...?

                  Thx!

                  Comment


                    #10
                    Hello -kman-

                    The sample doesn't actually use the order to do anything.
                    It does use the order reference inside of OnOrderUpdate.

                    The reason you get that error is because your order is still active when you move into realtime. A LiveUntilCancelled Order requires you to transition it in that situation. The sample shows how to get the order variable inside OnOrderUpdate, transition it inside OnOrderUpdate and also clear the variable when its done inside OnOrderUpdate. All of that is required based on you using a LiveUntilCancelled Order.

                    Assigning the order this way is incorrect:
                    Code:
                    activeOrder = EnterLongLimit(BarsInProgress, true, 2, entryPrice, "myEntrySignal");
                    You should be using OnOrderUpdate exactly like the sample shows, that's a complete sample.


                    Are you telling me that just because I set my local activeOrder variable to null that SetStopLoss will suddenly start working when we transition from Historical to Realtime?
                    No I didn't say that. SetStopLoss is controlled by the entry orders fill.

                    I'm assuming that internally SetStopLoss keeps its own reference to the order that's bound to "myEntrySignal". Why does me keeping a reference around and then setting it to null affect anything..
                    The Set methods manage themselves. The reason you need a reference to the entry is so you can transition it. If you use LiveUntilCancelled you need to manage the transition for those orders exactly as the sample in the help guide shows. You would repeat that for any LiveUntilCancelled orders you have that may be active when transitioning.

                    To transition an orderrr you have to have an instance of the order so you find the order in OnOrderUpdate and save it to a variable. You have to transition it so you check if its not null and is in the state needed for transition. Once the order is done you clear the variable.

                    You can copy and paste the sample into your script to make sure you have the correct setup.

                    Comment


                      #11
                      I think I understand -
                      Code:
                      GetRealtimeOrder
                      must have side effects aside from just retrieving the realtime order object, is that correct? The call to GetRealtimeOrder is that call that actually does the transitioning, and under the covers you guys rebind everything?

                      If so, then final question thing - just to make sure:

                      This code, which you said is incorrect:
                      Code:
                      protected override void OnBarUpdate()
                      {
                      if(entryCondition)
                        activeOrder = EnterLongLimit(BarsInProgress, true, 2, entryPrice, "myEntrySignal");​
                      is based on code from here: https://ninjatrader.com/support/help...ed_order_handl ing.htm

                      Code:
                       [B]protected override void OnBarUpdate()[/B]
                      
                      [B]{[/B]
                      
                      [B]if (entryOrder == null && Close[0] > Open[0])[/B]
                      
                      [B]entryOrder = EnterLongLimit("myEntryOrder", Low[0]);[/B]
                      
                      [B]}[/B]
                      
                      ​
                      Aside from me saying that this is GTC - I don't see any difference in how I'm entering the long...?

                      Thx!

                      Comment


                        #12
                        Hello -kman-,

                        Please use the following page for an example of transitioning orders. You can copy/paste the OnOrderUpdate override to get the right code. You need to use OnOrderUpdate for all order related tasks in this specific case.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Today, 05:17 AM
                        0 responses
                        50 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        126 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        69 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        42 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        46 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X