Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel confirmation

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

    Cancel confirmation

    To adhere to the Internal Order Handling Rules, I need to cancel orders before I submit new orders that would violate the rules. How do I wait for a CancelOrder() confirmation so that I know it's safe to submit the new orders? Is there a write-up or example somewhere about how to wait for order confirmations?

    Thanks,

    David
    dbw451

    #2
    Hi David, you would check if the orderstate is being return as OrderState.Cancelled in the OnOrderUpdate(). This reference sample will be helpful - http://www.ninjatrader-support2.com/...ead.php?t=7499

    Please also review this IOrder helppage with the different orderstates listed - http://www.ninjatrader-support.com/H...V6/IOrder.html
    BertrandNinjaTrader Customer Service

    Comment


      #3
      Perfect. Thanks Bertrand. Based on the sample code, I assume I would do something like:

      Code:
       
      protected override void OnExecution(IExecution execution)
      {
         if (targetOrder != null && targetOrder.Token == execution.Order.Token)
      	{
              // If target order cancel confirmed, enter new orders
      	if ([B]execution.Order.OrderState == OrderState.Cancelled[/B])
      	     {
      		entryOrder =  EnterLong("MyEntry");
      		stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
      		targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
      			
      	     }
      	}
      }
      Regards,

      David
      dbw451

      Comment


        #4
        dbw451,

        You might not want to submit your Exit()s that quickly. You need to first wait till you get a long position or else it may get ignored due to not being able to find a position.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Good point Josh. My actual strategy code submits my target and stop orders when I get a "Filled" order state for the opening position order. My strategy code logic is more similar to the code below than the example in my previous post:

          Code:
          protected override void OnExecution(IExecution execution)
          {
             if (targetOrder != null && targetOrder.Token == execution.Order.Token)
          	{
                  // If target order cancel confirmed, enter new orders
          	if (execution.Order.OrderState == OrderState.Cancelled)
          	     {
          		entryOrder =  EnterLong("MyEntry");
          	     }
          	}
          
              if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                 {
          	if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
          	{
          	    stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
          	    targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
          	}
                }
          
          }
          Regards,

          David
          dbw451

          Comment


            #6
            It looks fine in theory.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              I think I see what you mean by "in theory". Using the back-tester, I'm unable to trap the targetOrder cancelled event in OnExecution(). The stopOrder filled event is sent to OnExecution(), but not the targetOrder cancelled event. The reversal code is written such that the OnExecution() procedure would need to trap the targetOrder cancelled event.

              According to the help file, OnExecution() is called every time an order is filled. I see that OnOrderUpdate() is called every time an order changes state. Maybe the targetOrder cancelled state could be trapped in OnOrderUpdate()? I'll give it a try.

              Regards,

              David
              dbw451

              Comment


                #8
                Right. OnExecution() does not trigger from a cancel event. It only triggers from an execution so you will need to catch it in OnOrderUpdate() if that is what you are going for.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  I have my stop filled logic and my target canceled canceled logic in OnOrderUpdate(). When the back-tester determines that my stop has been hit, the sequence of order events is not as I would expect. In live trading, I would expect:

                  1. stop order is filled
                  2. target order is canceled.

                  The back-tester sequence is the opposite:

                  1. target order is canceled
                  2. stop order is filled.

                  Now I realize in live trading I'm not guaranteed a sequence of events from the exchanges, so I updated my stop and reverse code to not be order event sequence dependent. I only bring this up because I've banged my head against this issue for a couple of hours and thought the knowledge might benefit someone else.

                  Regards,

                  David
                  dbw451

                  Comment


                    #10
                    It generally would depend on how you have it coded. Submitting ExitLongStop/Limit() orders are not OCO in nature. They are OCO due to your code.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi Josh, I was never sure if ExitLongStop/Limit() were OCO in nature, so I always include logic that traps the stop and target order filled event. In the filled event logic, the opposite order is immediately canceled. That's why it's odd in OnOrderUpdate(), I get the cancel order event before I get the opposite filled order event.

                      Regards,

                      David
                      dbw451

                      Comment


                        #12
                        Unfortunately I cannot comment on this without seeing complete code. It may or may not be attributed to the fill algorithm required during backtesting. In either case I do not believe it should be a major detriment to your code.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Right, it's not a major detriment. I mentioned before that I've coded logic to handle it. You've already seen most of the code that can reproduce the issue earlier in this thread, but I'll add the stopOrder logic that cancels the targetOrder for completeness:

                          Code:
                          protected override void OnExecution(IExecution execution)
                          {
                             if (stopOrder != null && stopOrder.Token == execution.Order.Token)
                              {
                          [FONT=Courier New][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (order.OrderState == OrderState.Filled || order.OrderState == OrderState.PartFilled || (order.OrderState == OrderState.Cancelled && order.Filled > [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2]))[/SIZE][/FONT]
                          [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                          [FONT=Courier New][SIZE=2][COLOR=#0000ff]   if[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2] (targetOrder != [/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2])[/SIZE][/FONT]
                          [SIZE=2][FONT=Courier New]       CancelOrder(targetOrder);[/FONT][/SIZE]
                          [SIZE=2][FONT=Courier New]}[/FONT][/SIZE]
                              }
                           
                             if (targetOrder != null && targetOrder.Token == execution.Order.Token)
                              {
                                // If target order cancel confirmed, enter new orders
                              if (execution.Order.OrderState == OrderState.Cancelled)
                                   {
                                  entryOrder =  EnterLong("MyEntry");
                                   }
                              }
                           
                              if (entryOrder != null && entryOrder.Token == execution.Order.Token)
                                 {
                              if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                              {
                                  stopOrder = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 4 * TickSize, "MyStop", "MyEntry");
                                  targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 8 * TickSize, "MyTarget", "MyEntry");
                              }
                                }
                           
                          }
                          Thanks for your responses Josh. I don't need further assistance on this issue. I posted the code just to clarify.

                          Regards,

                          David
                          dbw451

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by terofs, Today, 04:18 PM
                          0 responses
                          1 view
                          0 likes
                          Last Post terofs
                          by terofs
                           
                          Started by nandhumca, Today, 03:41 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post nandhumca  
                          Started by The_Sec, Today, 03:37 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post The_Sec
                          by The_Sec
                           
                          Started by GwFutures1988, Today, 02:48 PM
                          1 response
                          5 views
                          0 likes
                          Last Post NinjaTrader_Clayton  
                          Started by ScottWalsh, 04-16-2024, 04:29 PM
                          6 responses
                          33 views
                          0 likes
                          Last Post ScottWalsh  
                          Working...
                          X