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

Problem With Cancelling Order Through Strategy

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

    Problem With Cancelling Order Through Strategy

    Hello Ninjatrader staff,

    I'm having some issues with cancelling an order through the script of a strategy, which brings me to this question.

    Let's say I have a 100 buy orders in a .Working or .Accepted state called "Buy". Then, based on certain conditions being met, my strategy is sending the CancelOrder(Buy) command and attempts to cancel all of the 100 working buy orders.

    Now, in the OnOrderUpdate() override, when you check for "if (Buy.OrderState == OrderState.Cancelled)", will this condition trigger if only one or a few of the 100 "Buy" orders are cancelled? Or, will the condition trigger if all of the 100 "Buy" orders are cancelled completely?

    I'm asking, because when "if (Buy.OrderState == OrderState.Cancelled)" is triggered, I am setting the "Buy" order back to a null state, and it might cause issues to attempt this while the "Buy" order hasn't been fully cancelled yet.

    Thank you in advance for your time, I look forward to your response.

    GLFX005
    Last edited by GLFX005; 07-01-2022, 05:02 AM.

    #2
    Hello GLFX005,

    OnOrderUpdate is called for every order and every event for that order. For a single order you will see OnOrderUpdate called quite a few times through the orders life. Cancelling an order also produces a OnOrderUpdate event. For 100 orders you would see many more events because there are many more orders. Each order has its own set of events.

    With the code you are showing and the use case of setting the variable to null, that would generally mean you have 100 Order variables and 100 conditions in OnOrderUpdate, one for each order to control that individual orders variable. If the order state was cancelled then you can set that orders variable to null at that time becuse OnOrderUpdate received the canceled event meaning the order is cancelled. You should still have 99 other orders which could be cancelled at that point and each would have its own OnOrderUpdate event for its individual cancellation.



    JesseNinjaTrader Customer Service

    Comment


      #3
      Hello Jesse,

      Thank you for your response.

      To clarify a bit more, the 100 "Buy" orders are all the same order. So it's basically 1 single order submission, called "Buy", with an order quantity of a 100 shares, instead of a 100 unique, individual orders. Does that change your response?

      Comment


        #4
        Hello GLFX005,

        There is not a concept of "100 "Buy" orders are all the same order ", you can have one order with a Quantity of 100 or you can have 100 individual orders with whatever quantities you wanted for each order.

        For this type of question you need to be careful on wording, when you say buy orders plural that means more than 1 order submission.

        A single order with 1 or 100 quantity has the same cancel event when you cancel that order, the quantity is not relevant for that type of question. When you cancel an order the broker sends a response when its cancelled, that's what you see in OnOrderUpdate. They don't have to go cancel individual quantities from a single order, it just stops that order with whatever quantity it has currently.

        and it might cause issues to attempt this while the "Buy" order hasn't been fully cancelled yet.
        When you get that cancelled order response you can set your variable to null at that point, the order is cancelled and there is no more cancelling to be done after that.

        You would still have to take a look at the current situation at that point in time, for example you may be in a position if you cancelled a partially filled order. Cancelling the order and setting the variable to null would be a part of the process, the full process depends on your goal and what your script is doing specifically. You may need to account for more situations if setting the variable to null at that point presents a problem later in your logic. That is the correct place to set a variable to null for a cancelled order.
        Last edited by NinjaTrader_Jesse; 07-01-2022, 08:04 AM.
        JesseNinjaTrader Customer Service

        Comment


          #5
          Hello Jesse,

          Thank you for the detailed response, and my apologies for the incorrect wording in my previous post.
          When using CancelOrder(), what are some scenarios that could cause this command to only cancel 1 out of 2 submitted quantities for a single buy order? I am often experiencing that scenario, but not all the time.
          Last edited by GLFX005; 07-01-2022, 09:01 AM.

          Comment


            #6
            Hello GLFX005,

            I am not certain I understand the situation from this description.

            If you cancel an order then nothing quantity wise should be changing, you just cancelled an order. Are you saying that when cancelling an order it cancels the order but leaves an open position?


            JesseNinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Kaledus, Today, 01:29 PM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Started by frankthearm, Yesterday, 09:08 AM
            13 responses
            45 views
            0 likes
            Last Post frankthearm  
            Started by PaulMohn, Today, 12:36 PM
            2 responses
            16 views
            0 likes
            Last Post PaulMohn  
            Started by Conceptzx, 10-11-2022, 06:38 AM
            2 responses
            55 views
            0 likes
            Last Post PhillT
            by PhillT
             
            Started by yertle, Yesterday, 08:38 AM
            8 responses
            37 views
            0 likes
            Last Post ryjoga
            by ryjoga
             
            Working...
            X