• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to Cancel Pending Limit Orders

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

    How to Cancel Pending Limit Orders

    Hello,

    I am using the Managed Approach in to build my strategy and Strategy Builder. However Strategy Builder does not have CancelOrder setup, so i have unlock and code my self.

    I am using the Managed Approach from the wizard with the following in NT8.
    EnterLongLimit() (isLiveUntilCancelled is set to true)
    EnterShortLimit() (isLiveUntilCancelled is set to true)
    SetStopLoss()
    SetProfitTarget()

    Problem

    1. I would like to Cancel the EnterLongLimit if 28 ticks from limit order and a fill has not been occurred. How can i accomplish this?

    Solution

    Here is my approach:

    private Order myEntryLongOrder = null; // tracking the order number
    private int MyEntryPrice; // tracking pending entry limit price to fill

    if ((EMA1[0] > EMA2[0])
    {
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentBid(0) + (-8 * TickSize)) , @"Long"); // submit limit order
    myEntryLongOrder = EnterLongLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentBid(0) + (-8* TickSize)) , @"Long"); // assign order id to track status of order
    MyEntryOrder = Convert.ToInt32((GetCurrentBid(0) + (-8 * TickSize)) ); // tracking pending entry limit price to fill
    }


    // CANCEL LONG LIMIT PENDING ORDER if 30 amount of ticks above the LIMIT LONG price has occured AND order still pending to be filled
    if (myEntryLongOrder != null && Close[0] >= MyEntryOrder + 30* TickSize) // if ORDER NUMBER is NOT blank, that means the order is PENDING
    {
    CancelOrder (myEntryLongOrder);
    }

    Questions
    1. Is my approach correct? It seems to work in back test, but I am concerned "myEntryLongOrder != null " is not really tracking the state of the order on the broker side.

    2. When I cancel the long order will the Stop and profit target order automatically cancel as well?

    Thank you kindly.
    Last edited by simple_goodoboy; 05-11-2017, 09:18 AM.

    #2
    Hello simple_goodoboy,

    You wouldn’t have to check if your entry order is not equal to null and could just ask that if the last price was 30 ticks above the price of your working order, call CancelOrder with the order object you assigned to your entry order passed in.

    I put together a sample doing what you describe.

    Please let us know if you need further assistance.
    Attached Files
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_AlanP View Post
      Hello simple_goodoboy,

      You wouldn’t have to check if your entry order is not equal to null and could just ask that if the last price was 30 ticks above the price of your working order, call CancelOrder with the order object you assigned to your entry order passed in.

      I put together a sample doing what you describe.

      Please let us know if you need further assistance.
      Thank you AlanP,

      This helps simplfy things. I understand now.

      Few questions for clarification;


      1. "if(State ==State.Historical) return;" what does this mean? I don't have this in my code. I wonder if its needed.

      2. Will CancelOrder() cancel the stop and profit target orders as well?

      3. "order.Name == "myorder" " What will "order.Name" equal to if market is flat and no open orders? Will it be null?

      4. See attachment where I added "myEntryOrder=null" after cancel to reset the variable. You left this out and I believe this is correct to add here. You agree? I added in red.

      5. In my code i was not using OnOrderUpdate() to get the status of the orders, i was just assuming that order was still open. Would this have made a difference in live market?

      Thanks for your help.
      Attached Files

      Comment


        #4
        Hello Simple simple_goodoboy,

        Checking if the current state was historical and if so, returning, would force the strategy to process real time bars only. I used this to prevent the strategy from taking historical trades and not submitting a limit order immediately upon applying the strategy.

        Your cancel order call applies to your entry order, I would encourage you to read SetProfitTarget section of our helpguide and become familiar with when your Profit Target/Stop Loss orders are submitted/working.

        http://ninjatrader.com/support/helpG...ofittarget.htm

        I would suggest using a print statement in OnBarUpdate to test what order.Name equals when no orders are working, to do this you could add the following in OnBarUpdate,

        Code:
        Print(order.Name.ToString());
        You could set the order object to null after you canceled the order, however the strategy would then submit another entry order which would be unnecessary for the purpose of the sample.

        In the code I provided you, I used OnOrderUpdate to assign an order object to the working order.
        The correct place to check the status of an order would be OnOrderUpdate.

        Please see OnOrderUpdate section of our helpguide,
        http://ninjatrader.com/support/helpG...=OnOrderUpdate

        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_AlanP View Post
          Hello simple_goodoboy,

          You wouldn’t have to check if your entry order is not equal to null and could just ask that if the last price was 30 ticks above the price of your working order, call CancelOrder with the order object you assigned to your entry order passed in.

          I put together a sample doing what you describe.

          Please let us know if you need further assistance.
          Hello Alan,

          See your cancel logic below.

          What happens if there is no (myEntryOrder is null) order pending and this condition below continues to execute if true? Will this cause an error because strategy is continuously canceling an order that is not pending or open?

          }
          if (Close[0]>xBidPrice+30*TickSize)
          CancelOrder(myEntryOrder);
          }

          Comment


            #6
            Thank you Alan ,

            I will think about this some more and respond if quesitons
            Attached Files
            Last edited by simple_goodoboy; 05-15-2017, 07:58 AM.

            Comment


              #7
              Hello simple_goodoboy,

              No error would be thrown however it could be considered good practice to check that the order is not null before trying to cancel it.

              Please let us know if you need further assistance.
              Alan P.NinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_AlanP View Post
                Hello simple_goodoboy,

                No error would be thrown however it could be considered good practice to check that the order is not null before trying to cancel it.

                Please let us know if you need further assistance.
                Problem

                1. When I back test screenshot_2, I see many trades, appears to be working.

                2. When I back test screenshot_3, I only see 2 trades as if the myEntryShortOrder is not being reset back to null.

                Questions

                1. I am using managed approach and backtesting this strategy. Lets assume, a short siganl occurs and profit target is hit and I am back to market flat. Is myEntryShortOrder status now "null"? If its not, then that could be the problem because I use myEntryShortOrder==null in my short conditions to prepare for another entry signal.

                I am assuming the answer to my question is Yes, because the managed approach should reset the order object back to null.

                Thanks
                Attached Files
                Last edited by simple_goodoboy; 05-15-2017, 08:55 AM.

                Comment


                  #9
                  Hello simple_goodoboy,

                  Order objects assigned to your entry order are not set back to null on their own. Even if you assign a limit entry order to an order object, cancel that limit order, the order object does not become null.

                  You should see the following section of our helpguide for an example of setting an order object back to null,
                  https://ninjatrader.com/support/help...rderupdate.htm

                  You may also see the attached sample which demonstrates this point.

                  Please let us know if you need further assistance.
                  Attached Files
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_AlanP View Post
                    Hello simple_goodoboy,

                    Order objects assigned to your entry order are not set back to null on their own. Even if you assign a limit entry order to an order object, cancel that limit order, the order object does not become null.

                    You should see the following section of our helpguide for an example of setting an order object back to null,
                    https://ninjatrader.com/support/help...rderupdate.htm

                    You may also see the attached sample which demonstrates this point.

                    Please let us know if you need further assistance.
                    Thank you AlanP. I appreciate your effort in helping me understand.

                    I believe I slowly understanding now. I think what is confusing me is do I have to use UnManged Approach since I am now using the CancelOrders()

                    Questions:

                    1. If I copy and backtest the code the from the sample code from the nt8 help file (https://ninjatrader.com/support/help...rderupdate.htm) or your example and use it for my entry long (EnterLimitLong) orders, will I have to do the same for the my SetProfitTarget and SetStopLoss() orders?

                    Because I used the Strategy Builder to write my code trying to stick to the Managed approach and in the Managed Approach if I don't use cancelorders(), the managed approach take care of order handling for me. Is this correct?

                    2. I would like to use the CancelOrder() using the Managed Approach just for entry orders, this way I don't have to track SetProfitTarget and SetStopLoss() orders and reset the null, etc. Is this possible?

                    3. In your sample, I notice you are using the UnManaged Approach, because you used OnStateChange(). My code was built from the Managed Approach, if I want to start using the CancelOrder(), do I need to move towards UnManaged Approach. I rather not do this and keep things simple as possible.

                    Thanks for your help.

                    Comment


                      #11
                      Hello simple_goodoboy,

                      If you copy the code from OnOrderUpdate for long entry order, whether or not you would have to do that for the Profit Target and Stop loss orders would depend on what you intend on doing. If you wanted to assign an order object to them for the purpose of canceling either of them, then yes.

                      Yes, the managed approach does utilize Internal Order Handling Rules, which are detailed at the following link.
                      https://ninjatrader.com/support/help...d_approach.htm

                      In the sample I provided a sample which uses CancelOrder, you could add SetProfitTarget and SetStopLoss calls within that strategy. You would not have to worry about canceling a Stop if the Profit Target was filled and vis versa, this being the advantage of the Managed Approach.

                      I was not using the unmanaged approach in my sample. To determine or set a strategy as unmanaged, you would have to have the following set under State.SetDefaults,
                      https://ninjatrader.com/support/help...sunmanaged.htm

                      Please let us know if you need further assistance.
                      Alan P.NinjaTrader Customer Service

                      Comment


                        #12
                        Hi Alan,

                        Question: I am running a strategy with "Calculate.OnEachTick" setting. CancelOrder(myOrder) for a EnterLongStopMarket (or EnterLongLimit) command gets executed but only physically cancels at the close of a bar, and not on the tick. Am I missing a setting?

                        Thanks,
                        Alex

                        Comment


                          #13
                          Hello Shadowfax,

                          Is this occurring in live trading, market replay, or a backtest?

                          Could you please send an email to platformsupport[at]ninjatrader[dot]com with Attn: Alan P in the Subject line. Also within the email please include a link to this thread, and attach the log and trace files for the day in subject which you can find in My Documents>NinjaTrader8>Log and My Documents>NinjaTrader8/Trace folders.

                          I look forward to your email.
                          Alan P.NinjaTrader Customer Service

                          Comment


                            #14
                            This is using the Sim account on a live feed strategy. I will send you the details shortly.

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by rexsole, Today, 11:27 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post rexsole
                            by rexsole
                             
                            Started by alfonso, Today, 10:21 AM
                            0 responses
                            2 views
                            0 likes
                            Last Post alfonso
                            by alfonso
                             
                            Started by saeed, Today, 10:20 AM
                            2 responses
                            5 views
                            0 likes
                            Last Post NinjaTrader_RyanS  
                            Started by ntbone, Yesterday, 05:21 PM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_ChelseaB  
                            Started by ntbone, Yesterday, 04:05 PM
                            1 response
                            5 views
                            0 likes
                            Last Post NinjaTrader_Jesse  
                            Working...
                            X