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()

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

    CancelOrder()

    I'm using CancelOrder(). It works perfectly in my first strategy which uses one contract, but in my second strategy which uses two contracts the CancelOrder() is not working (not canceling the order but leaving them open until filled. When a canceled other is not canceled, as it says below:

    1. This method sends a cancel request to the broker and does not guarantee that an order is completely canceled. Most of the time you can expect your order to come back 100% canceled.

    2. An order can be completely filled or part filled in the time that you send the cancel request and the time the exchange receives the request. Check the OnOrderUpdate() method for the state of an order you attempted to cancel.

    I'm using:

    private Order myEntryOrder = null;
    private int barNumberOfOrder = 0;

    protected override void OnBarUpdate()
    {
    // Submit an entry order at the low of a bar
    if (myEntryOrder == null)
    {
    // use 'live until canceled' limit order to prevent default managed order handling which would expire at end of bar
    EnterLongLimit(0, true, 1, Low[0], "Long Entry");
    barNumberOfOrder = CurrentBar;
    }

    // If more than 5 bars has elapsed, cancel the entry order
    if (CurrentBar > barNumberOfOrder + 5)
    CancelOrder(myEntryOrder);
    }

    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 gauranteed to be complete if it is referenced immediately after submitting
    if (order.Name == "Long Entry")
    myEntryOrder = order;

    // Evaluates for all updates to myEntryOrder.
    if (myEntryOrder != null && myEntryOrder == orde r)
    {
    // Check if myEntryOrder is cancelled.
    if (myEntryOrder.OrderState == OrderState.Cancel led)
    {
    // Reset myEntryOrder back to null
    myEntryOrder = null;
    }
    }
    }

    Is there anything I'm missing in the script that causes an order not to cancel 100% of the time?
    Last edited by AdeptistJune; 05-24-2022, 06:50 AM.

    #2
    Hello AdeptistJune,

    Thank you for your note.

    You mentioned that the strategy that doesn't cancel orders uses 2 contracts, but I'm only seeing this code submitting one contract - how is the second contract submitted/cancelled? Can you provide a working example script?

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      if (entryOrder == null
      //&& (Times[0][0].TimeOfDay >= new TimeSpan(BeginHourS, BeginMinuteS, 00) && Times[0][0].TimeOfDay < new TimeSpan(EndHourS, EndMinuteS, 00))
      && Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade
      && alert_state[0] == -6 && alert_state[1] != -6
      && mfe[0] < GetCurrentBid()
      && (OkToTrade == true)) // Daily Loss and Target Step 8 - trade entry with the OKtoTrade condition
      {
      EnterShortStopMarket(0, true, 1, mfe[0], @"myEntryOrderShort1");
      EnterShortStopMarket(0, true, 1, mfe[0], @"myEntryOrderShort2");
      barNumberOfOrder = CurrentBar;
      }

      Comment


        #4
        Hello AdeptistJune,

        Thank you for your reply.

        Are you ensuring you're saving each entry to a separate Order variable in OnOrderUpdate so they are separately tracked?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello Kate,

          Yes. I am saving each entry to a separate Oder variable:

          Attached Files

          Comment


            #6
            Hello Kate,

            Are you saying I need to create this way:
            private Order entryOrder = null; // This variable holds an object representing our entry order
            private Order entryOrder2 = null; // This variable holds an object representing our entry order

            Comment


              #7
              Hello AdeptistJune,

              Thank you for your reply.

              Yes, if you're tracking more than one order you should have a order variable for each one, otherwise if you have two orders but only one entryOrder variable to hold them, the second one processed would be the one held in entryOrder and you wouldn't have a reference for the other one to be able to cancel it.

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

              Kate W.NinjaTrader Customer Service

              Comment


                #8
                Thanks, Kate,

                Is this proper syntax: entryOrder == null || entryOrder2 == null

                Or is is:

                entryOrder == null && entryOrder2 == null

                Comment


                  #9
                  Hello AdeptistJune,

                  Thank you for your reply.

                  That would depend on where you're checking if these are null. If it's before entry, and you're entering both at the same time you'd want to use "entryOrder == null && entryOrder2 == null". If you're checking what's iterating through OnOrderUpdate, depending on what you're doing you might need to check if one OR the other is null.

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

                  Comment


                    #10
                    Thanks, Kate,

                    Question: What to do when I make a post that goes unanswered? I normally would repost in a new thread but I was getting chided.

                    Comment


                      #11
                      Hello AdeptistJune,

                      Thank you for your reply.

                      You should make another post in the same thread, as that will reopen the ticket if it was accidentally closed without response, rather than create a new duplicate thread.

                      Please note we are experiencing a high volume of inquiries, so responses may take a little longer than normal.

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

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by rjbtrade1, 11-30-2023, 04:38 PM
                      2 responses
                      77 views
                      0 likes
                      Last Post DavidHP
                      by DavidHP
                       
                      Started by Stanfillirenfro, Today, 07:23 AM
                      3 responses
                      13 views
                      0 likes
                      Last Post NinjaTrader_ChelseaB  
                      Started by FitSpressoHonest, Today, 09:14 AM
                      0 responses
                      4 views
                      0 likes
                      Last Post FitSpressoHonest  
                      Started by Davide999, 05-18-2023, 03:55 AM
                      4 responses
                      557 views
                      1 like
                      Last Post kcwasher  
                      Started by rexsole, Today, 08:39 AM
                      2 responses
                      8 views
                      0 likes
                      Last Post NinjaTrader_Erick  
                      Working...
                      X