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

CancelOrder() did not cancel Order

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

    CancelOrder() did not cancel Order

    Hello,

    first my english is far from perfect.

    On testing my Strategy i found a behavior which is not clear for me.

    I wrote an example which produce this behavior.

    CancelOrderSample.cs

    In the Case, the First order is fill, i will cancel the second order.
    But this second Order is still alive until end of the bar, and will be filled if the certeria catch.

    when i call CancelOrder(secondOrder) this order should be canceld immediately.

    Which settings is here false?

    with best regard
    Rossegger Robert




    #2
    Hello SpikeLGWG,

    Thank you for your post.

    First, you're running into an issue where you're not setting the orders to remain live until cancelled. The cancellation would best be done in OnOrderUpdate when the first order fills. Try this:

    Code:
    private Order firstOrder;
    private Order secondOrder;
    
    protected override void OnBarUpdate()
    {
    
    if (State == State.Historical)
    return;
    
    if (IsFirstTickOfBar)
    {
    Print(Closes[BarsInProgress].Bars.GetTime(CurrentBar).ToString("yyyy_MM_dd_HH_ mm_ss") + " " + this.Name + "-OnBarUpdate: Creating Orders");
    
    firstOrder = EnterLongLimit(0, true, 1, GetCurrentBid() - 10 , "FirstOrder_" + CurrentBar);
    secondOrder = EnterLongLimit(0, true, 1, GetCurrentBid() - 1000 , "SecondOrder_" + CurrentBar);
    }
    
    }
    
    protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
    {
    if(firstOrder != null && secondOrder != null)
    {
    if (order.Name == firstOrder.Name && order.OrderState == OrderState.Filled)
    {
    Print(" " + Closes[BarsInProgress].Bars.GetTime(CurrentBar).ToString("yyyy_MM_dd_HH_ mm_ss") + " " + this.Name + "-OnBarUpdate: FirstOrder filled");
    
    firstOrder = null;
    
    Print(" " + Closes[BarsInProgress].Bars.GetTime(CurrentBar).ToString("yyyy_MM_dd_HH_ mm_ss") + " " + this.Name + "-OnBarUpdate: Cancel Second Order");
    
    CancelOrder(secondOrder);
    
    secondOrder = null;
    }
    }
    if (orderState == OrderState.Working || orderState == OrderState.Filled || orderState == OrderState.CancelPending || orderState == OrderState.Cancelled)
    Print(" " + Closes[BarsInProgress].Bars.GetTime(CurrentBar).ToString("yyyy_MM_dd_HH_ mm_ss") + " " + this.Name + "-OnOrderUpdate: " + order.Name + " order.OrderState: " + order.OrderState);
    }
    I'm seeing this cancel the second order immediately when the first fills.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate,

      thats realy strange, i found this this EnterLongLimit with this overload in many sampels and i testet them more then once time, And didn't not work. but now it will work.

      Maybe the Problem is, i call CancelOrder in OnBarUpdate?


      thank you Kate

      Attached Files

      Comment


        #4
        Hm,
        i want to move this "new" code to my realy strategy, and i found this code allready there
        I testet them a second time and it didn't work (the open order will canceld on the next bar.)

        I try to attempt the new (CancelOrderSample) with code from my original Strategy to find what is corrupt on my usage of ninjatrade.

        Can you give me a hint?
        If this Order (firstOrder) is filled, how should i set a takeprofit order?
        I use
        ExitLongLimit(fullQuantity, current_TakeProfit);

        Is this ok?

        with best regards


        Comment


          #5
          Hello Kate,

          I hope Kate is your Name.

          Thanks to your help an i try to modify your hint, to get the reason why my code didn't work.
          In my orignal Strategy, i will also create a "TakeProfitOrder". And after the "TakeprofitOrder" is filled, i want to cancel all other Orders. Thats the plan.
          And ich modified your code and get this Result in the Output-Window:
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnBarUpdate: Creating Orders
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder_13931_0 LimitPrice: 57384,03 order.OrderState: Working
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder_13931_1 LimitPrice: 56394,03 order.OrderState: Working
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder_13931_0 LimitPrice: 57384,03 order.OrderState: Filled
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder filled
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: Create ExitLongLimit
          2021_11_30_21_ 13_00 CancelOrderStrategy-OnOrderUpdate: ExitLongLimit: is null <<<<<<<<<<<<<<<<<

          Why ist the Order from ExitLongLimit null?
          if a second Order is filled, the ExitLongLimit returns an order.

          I read realy many of your guidlines and helpfile. (I can read english, but write and talk are horror) and i have no Ideo whats going wrong.

          Please, can you help me?


          Attached Files

          Comment


            #6
            Hello SpikeLGWG,

            Thank you for your reply.

            I'm not following exactly what the intended behavior is here, can you clarify? What exactly are you wanting to occur after the order that's placed closest to the current price fills? Are you wanting the other limit order to be cancelled and a new exit limit order placed?

            Thanks in advance; I look forward to assisting you further.

            Kate W.NinjaTrader Customer Service

            Comment


              #7
              Hello Kate,

              Wenn the First "EnterLongLimit"-Order is filled, it should create a ExitLongLimit (as opposite).
              --The Price is realy near the current price, this is only for testing.
              --The ExitLongLimit returns null.

              After the "ExitLongLimit" Order is filled, all oter "EnterLongLimit"-Order should be canceld.

              This should be happen.
              In my samplecode from last post is an error. I attach the correct Version.

              Output:
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnBarUpdate: Creating Orders
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder_11991_1 LimitPrice: 57356,31 order.OrderState: Working
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder_11991_1 LimitPrice: 57356,31 order.OrderState: Filled
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnOrderUpdate: FirstOrder filled
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnOrderUpdate: Create ExitLongLimit 57376,27
              2021_12_01_05_ 02_00 CancelOrderStrategy-OnOrderUpdate: ExitLongLimit: is null <<<<<<<<<<<<<<<<<<<<<<<

              Thank you for your help!

              Spike
              Attached Files

              Comment


                #8
                Hello SpikeLGWG,

                Thank you for your reply.

                If you turn on the Trace Orders function by setting TraceOrders = true in State.SetDefaults, we can see what's occurring:

                12/1/2021 12:27:55 PM Strategy 'CancelOrderStrategy2/243910102': Ignored SubmitOrderManaged() method at 12/1/2021 12:27:55 PM: BarsInProgress=0 Action=Sell OrderType=Limit Quantity=1 LimitPrice=4611.50 StopPrice=0 SignalName='ExitLongLimit_4202' FromEntrySignal='FirstOrder_4199_0' Reason='This was an exit order but no position exists to exit'
                2021_12_01_12_ 28_00 CancelOrderStrategy2-OnOrderUpdate: ExitLongLimit: is null

                So what's happening is that the actual internal strategy position hasn't updated yet, because that occurs when it runs through OnExecutionUpdate, and the exit order is getting ignored. To avoid this you'll actually want to submit the exit order from OnExecutionUpdate.

                I'm attaching a revised version of my previous example that illustrates this. Because we'll still need our variables to track the orders, we set them to null in OnExecutionUpdate instead.

                Please let us know if I may be of further assistance to you.

                Attached Files
                Kate W.NinjaTrader Customer Service

                Comment


                  #9
                  Hello Kate,

                  your sample-code will do what i want.
                  An LongLimitOrder will be created, an ExitlongOrder is created when LongLimitOrder is filled and second LongLimitOrder is canceld wenn ExitLongOrder is filled.

                  Perfect!
                  I try to place this knowledge in my original Strategy, but i think this issue can be closed.

                  Thank you for your help!

                  with best regards
                  Spike

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by a5435754, Today, 10:35 PM
                  0 responses
                  2 views
                  0 likes
                  Last Post a5435754  
                  Started by JanickEichman, Today, 10:32 PM
                  0 responses
                  1 view
                  0 likes
                  Last Post JanickEichman  
                  Started by BurnOutTrader, Today, 07:02 PM
                  2 responses
                  12 views
                  0 likes
                  Last Post BurnOutTrader  
                  Started by dtaylor, Today, 04:36 PM
                  3 responses
                  11 views
                  0 likes
                  Last Post dtaylor
                  by dtaylor
                   
                  Started by iq200, Today, 01:35 PM
                  1 response
                  21 views
                  0 likes
                  Last Post NinjaTrader_ChrisL  
                  Working...
                  X