Announcement

Collapse
No announcement yet.

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.

    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.

        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.

              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.

                  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.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      71 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      143 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      76 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      47 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      51 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X