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

Assign Order in OnOrderUpdate and OnExecutionUpdate - What's the Best Approach

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

    Assign Order in OnOrderUpdate and OnExecutionUpdate - What's the Best Approach

    I am looking through the strategy samples and the NinjaTrader help guide and would like advice on the best way to assign an Order in OnOrderUpdate, OnExecutionUpdate, and check when to enter / edit an order.

    In SampleOnOrderUpdate, Order is assigned like:
    Code:
    OnOrderUpdate
    if (order.Name == "MyEntry")
    {	
            entryOrder = order;
    	if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    		entryOrder = null;
    }
    
    OnExecutionUpdate
    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))
    	{
    		if (execution.Order.OrderState != OrderState.PartFilled)
    			entryOrder = null;
    	}
    }
    In Advance Order Handling, https://ninjatrader.com/support/help...er_methods.htm, Order is assigned like:
    Code:
    if (order.Name == "myEntryOrder" && orderState != OrderState.Filled)
          entryOrder = order;
    
    if (entryOrder != null && entryOrder == order)
    {
            if (order.OrderState == OrderState.Filled)
                  entryOrder = null;
    }
    In the examples from the above, Order is also checked for null before entering an order (if (entryOrder != null) enter logic). The above examples only take into consideration a portion of the OrderState values, so what is the best way in NinjaTrader 8 to assign Order, keep the status up-to-date, and then be able to check that order for an entry (like the example in this paragraph) and to change the order (example: if you have an OrderStatus of ChangeSubmitted [or a similar change status], but your entryOrder != null you could keep trying to submit an order that is pending a change.

    #2
    Hello,

    Thank you for the post.

    We have a short example that demonstrates assigning orders from these overrides in the reference samples here: https://ninjatrader.com/support/foru...ead.php?t=7499

    We also have other various strategy related concepts excepted in the reference samples: https://ninjatrader.com/support/foru...ead.php?t=3220

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello,

      Thank you for the post.

      We have a short example that demonstrates assigning orders from these overrides in the reference samples here: https://ninjatrader.com/support/foru...ead.php?t=7499

      We also have other various strategy related concepts excepted in the reference samples: https://ninjatrader.com/support/foru...ead.php?t=3220

      I look forward to being of further assistance.
      I referenced the example you provided along with the NinjaTrader help guides example, which appear to be different, so I am looking for additional guidance on the best way to handle all the different OrderStatus since it is not clear.

      Comment


        #4
        Hello,

        Yes, I do see a difference in syntax between the two samples as they were likely developed by different authors.

        Both are correct in assigning the order, and both do check for null and also check for canceled they just check this in different ways. I can see that both samples should achieve the same goal although they are slightly different. I don't see a case where one could be null where the other would not be, and both seem to work for the intended purpose. Really it would be up to you to choose in this case which you like better and go with that. The forum sample is a more complete sample so that may be good as a starting point if you wanted to have a whole script to start with.

        In your original concern, the orderstates used would be determined on what your logic needs to do. If you are just concerned with assigning orders and later accessing them, the forum sample shows this off in a simple and correct way and would be a good place to start. The help guide is still correct as well, this is just shown in a different form for that specific example.

        If you have specific cases like the one you mentioned in post one, you would need to test for these cases using Prints and if that affects your overall logic, you could account for it in a similar way that is shown with the samples orderstates.

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello,

          Do you have an example of how to create and manage multiple entries using the OnOrderUpdate and OnOrderExecution Methods? If not, can you please give an explanation of how to do it?

          I've coded it like this but only one order gets submitted.
          HTML Code:
                              if(dualTarget == false)
                              {    
                                  EnterLongStopMarket(0,true, positionSize, WizSellPrice,"myEntrylong");
                              }
                              else if(dualTarget)
                              {
                                  EnterLongStopMarket(0,true, firstTargetPosition, WizSellPrice,"myEntrylong");
                                  EnterLongStopMarket(0,true, secondTargetPosition, WizSellPrice,"myEntrylong1");
                              }    
          ​
          OnOrderUpdate:

          HTML Code:
                      if (entryOrder_Long == null &&  order.Name == "myEntrylong" && orderState != OrderState.Filled)
                      {
                          // 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
                          entryOrder_Long = order;
                          entryOrder_Short = null;
          
                                    }
                      else if (order.OrderState == OrderState.Cancelled && order.Filled == 0 &&  order.Name == "myEntrylong")
                      {
                              entryOrder_Long = null;
          
          
                      }
                      #endregion
          
                      #region sets 2nd entry orderlong if DUAL target
                      if(dualTarget) // Assigns entryorder here if more than 1 order
                      {
          
                          // Checks for all updates to entryOrder.
                          if (entryOrder_Long1 == null &&  order.Name == "myEntrylong1" && orderState != OrderState.Filled )
                          {
                              // 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
                              entryOrder_Long1 = order;
                              entryOrder_Short1 = null;
          
                                              }
                          else if (order.OrderState == OrderState.Cancelled && order.Filled == 0 &&  order.Name == "myEntrylong1")
                          {
                                  entryOrder_Long1 = null;
          
          
                          }
                      }
          ​
          OnExecutionUpdate:
          HTML Code:
                      if (dualTarget && entryOrder_Long != null && entryOrder_Long == execution.Order && execution.Name == "myEntrylong")
                      {
                          tradeCounter++;
          
                          // This second if-statement is meant to only let fills and cancellations filter through.
                          if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          {
                              // Simple stop and target
                              stopOrder_Long = ExitLongStopMarket(0, true, firstTargetPosition, execution.Order.AverageFillPrice - (Stop * TickSize), "stopLong", "myEntrylong");
                              longtargetOrder = ExitLongLimit(0, true, firstTargetPosition, execution.Order.AverageFillPrice + (Target2 * TickSize), "targetLong", "myEntrylong");
          
                              CurrentLongTriggerPrice = execution.Order.AverageFillPrice + BreakEvenTicks * TickSize;
                              //CurrentLongStopPrice = execution.Order.AverageFillPrice - (Stop * TickSize);
          
          
                              // Resets the entryOrder object to null after the order has been filled
                              if (execution.Order.OrderState != OrderState.PartFilled)
                              {
                                  entryOrder_Long = null;
          
                              }
                          }
          
                      }
                      #endregion first entry order LONG
          
                      #region// second entry order long (Sale de primro con target 1)
          
                      if (dualTarget && entryOrder_Long1 != null && entryOrder_Long1 == execution.Order && execution.Name == "myEntrylong1")
                      {
          
          
                          // This second if-statement is meant to only let fills and cancellations filter through.
                          if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                          {
                              // Simple stop and target
                              stopOrder_Long1 = ExitLongStopMarket(0, true, secondTargetPosition, execution.Order.AverageFillPrice - (Stop * TickSize), "stopLong1", "myEntrylong1");
                              longtargetOrder1 = ExitLongLimit(0, true, secondTargetPosition, execution.Order.AverageFillPrice + (Target1 * TickSize), "targetLong1", "myEntrylong1");
          
                              CurrentLongTriggerPrice = execution.Order.AverageFillPrice + BreakEvenTicks * TickSize;
          
          
          
                              // Resets the entryOrder object to null after the order has been filled
                              if (execution.Order.OrderState != OrderState.PartFilled)
                              {
                                  entryOrder_Long1 = null;
          
                              }
                          }
          
                      }
          ​
          Thanks,

          BobPerez
          Last edited by bobperez; 04-27-2023, 07:29 PM.

          Comment


            #6
            Before you get too deep just a verification - do you have EntriesPerDirection set to more than 1?
            Bruce DeVault
            QuantKey Trading Vendor Services
            NinjaTrader Ecosystem Vendor - QuantKey

            Comment


              #7
              You probably want to do this in OnExecutionUpdate not OnOrderUpdate - see the extensive commentary at https://ninjatrader.com/support/help...tionupdate.htm

              Also read the bottom section at https://ninjatrader.com/support/help...-threading.htm
              Bruce DeVault
              QuantKey Trading Vendor Services
              NinjaTrader Ecosystem Vendor - QuantKey

              Comment


                #8
                Originally posted by QuantKey_Bruce View Post
                Before you get too deep just a verification - do you have EntriesPerDirection set to more than 1?
                Hi Bruce,

                Amazing, "Entries per Direction" was set to 1. I changed it and it worked.

                Thanks

                Bob Perez

                Comment


                  #9
                  Originally posted by bobperez View Post

                  Hi Bruce,

                  Amazing, "Entries per Direction" was set to 1. I changed it and it worked.

                  Thanks

                  Bob Perez
                  That is what I was hoping for you - just a simple oversight.
                  Bruce DeVault
                  QuantKey Trading Vendor Services
                  NinjaTrader Ecosystem Vendor - QuantKey

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by geddyisodin, Yesterday, 05:20 AM
                  7 responses
                  45 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Started by gbourque, Today, 06:39 AM
                  2 responses
                  5 views
                  0 likes
                  Last Post gbourque  
                  Started by cre8able, Yesterday, 07:24 PM
                  1 response
                  13 views
                  0 likes
                  Last Post NinjaTrader_ChelseaB  
                  Started by cocoescala, 10-12-2018, 11:02 PM
                  6 responses
                  939 views
                  0 likes
                  Last Post Jquiroz1975  
                  Started by cmtjoancolmenero, Yesterday, 03:58 PM
                  1 response
                  17 views
                  0 likes
                  Last Post NinjaTrader_Gaby  
                  Working...
                  X