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

How to prevent Order Cancellation?

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

    How to prevent Order Cancellation?

    How can I prevent Order Cancellation.
    I would like my orders to be 100% Filled at the Specific Quantity at any price.

    I had tried everything
    i.e.

    Set Slippage = 9999999999;
    bool isLiveUntilCancelled = true

    EnterLongLimit(0,true,Quantity,999999999,"");
    EnterShortLimit(1,true,Quantity2,0,"");
    ExitLongMIT(0,true,Positions[0].Quantity,0,"","");
    ExitShortMIT(1,true,Positions[1].Quantity,99999999999,"","");

    I am still getting Cancelled Order and Partial Fill.
    The attachment is from the backtesting.

    Thank you!



    Attached Files

    #2
    Hello johnnybegoode,

    With a real order submitted live to a broker there is no way to prevent part fills if there are not enough contracts on the opposing side to exchange.

    With cancelled orders, orders are cancelled if isLiveUntilCancelled is false and the submission bar closes, if CancelOrder() is called with the order object, if the entry associated with an exit is closed, and when the strategy is being disabled.

    Are you calling CancelOrder()?

    Is the order submitted with isLiveUntilCancelled as true?

    Do you have TraceOrders on to see why the order is being cancelled?
    What is the output message from TraceOrders?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      That cancellation happened during backtesting, not a live simulation.
      I'm not sure it will happen during a live simulation.

      I do not use any CancelOrder().
      LiveUntilCancelled is always true.

      Knowing that Cancellation, Partial Fill, and Rejection could happen,
      Would OnOrderUpdate() help me solve this problem?

      After placing an order, I would like to keep trying to open the order at a specific volume at any cost.
      If an order is Cancelled - keep trying until filled at any cost
      If an order is Rejected - keep trying until filled at any cost
      If an order is Partially filled - keep trying to fill the remaining at any cost


      Does TraceOrders work in backtesting?
      TraceOrders = true;
      I see nothing on the output





      Last edited by johnnybegoode; 02-13-2021, 12:37 AM.

      Comment


        #4
        Hello johnnybegoode,

        Yes, TraceOrders works in backtest and in real-time.

        Have you set TraceOrders to true in OnStateChange() when State is State.Configure?

        If this was set to true when State is State.SetDefaults, you must remove the strategy and re-add the strategy to pull the defaults.

        Please provide the output from prints and TraceOrders.

        Below is a link to a forum post that discusses using prints and TraceOrders to understand behavior.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          TraceOrders works now
          but it doesn't tell me why my EnterLong is ignored.

          All I get is
          An Enter() method to submit an entry order at '1/6/2021 11:55:00 AM' has been ignored. Please search on the term 'Internal Order Handling Rules' in the Help Guide for detailed explanation.

          The https://ninjatrader.com/support/help...d_approach.htm
          is no help too.

          Comment


            #6
            Hello johnnybegoode,

            Please provide the output from prints and TraceOrders.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              It is working well, then all a sudden Cancelled because of entry order on the opposite side of the market.

              1/6/2021 9:07:15 AM Strategy 'StatArbFutures/224128128': Cancelled pending entry order on opposite side of the market: BarsInProgress=1, orderId='287a7224be854ebfa87ce69491709245' account='Playback101' name='LONG M2K 03-21' orderState=Submitted instrument='M2K 03-21' orderAction=Buy orderType='MIT' limitPrice=0 stopPrice=9999999999 quantity=21 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=1593 time='2021-01-06 09:07:13' gtd='2099-12-01' statementDate='2021-01-06'
              I'm doing pair trading.
              I have buy orders in opposite direction but different BarsInProgress.

              Code:
              if (
              (Positions[0].MarketPosition == MarketPosition.Flat)
              && (Positions[0].Quantity == 0)
              && (Positions[1].MarketPosition == MarketPosition.Flat)
              && (Positions[1].Quantity == 0)
              )
              {
              EnterShortMIT(0,true,Quantity,0,"SHORT MES 03-21");
              EnterLongMIT(1,true,Quantity2,9999999999,"LONG M2K 03-21");
              }
              Attached Files

              Comment


                #8
                Hello johnnybegoode,

                You cannot place working orders in opposite directions with the managed approach.

                From the help guide.
                "Methods that generate orders to enter a position will be ignored if:
                •A position is open and an order submitted by a non market order exit method (ExitLongLimit() for example) is active and the order is used to open a position in the opposite direction"


                If you want to place working orders in opposite directions you must use the unmanaged approach.

                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  1)
                  On managed order, I try using Market Order to workaround opening opposite direction under
                  different barsarray.
                  It works until it decided not to.
                  Exit more than what I have to exit and turn the exit into an open position.
                  Very unstable.
                  Can NT8 do pair trading or not?

                  *************************
                  2)
                  On unmanaged order, it is like conditions do not matter anymore.

                  Unmanaged behavior is worse:
                  Buy and Sell continuously no matter what I do.

                  Can there be 1 entryperdirection like in managed?

                  And there was Exit on Session Close

                  Is there a way to do what I want to do on NinjaTrader or not?
                  Pair Trading

                  Code:
                  if (
                  (PositionsAccount[0].MarketPosition == MarketPosition.Flat)
                  && (PositionsAccount[0].Quantity == 0)
                  && (PositionsAccount[1].MarketPosition == MarketPosition.Flat)
                  && (PositionsAccount[1].Quantity == 0)
                  && (Positions[0].MarketPosition == MarketPosition.Flat)
                  && (Positions[0].Quantity == 0)
                  && (Positions[1].MarketPosition == MarketPosition.Flat)
                  && (Positions[1].Quantity == 0)
                  && (entryOrderLongM2K == null)
                  && (entryOrderShortMES == null)
                  )
                  
                  {
                  SubmitOrderUnmanaged(0, OrderAction.Sell, OrderType.MIT,
                  Quantity, 0, 0, "", "SHORT MES 03-21");
                  SubmitOrderUnmanaged(1, OrderAction.Buy, OrderType.MIT,
                  Quantity2, 9999999999, 9999999999, "", "LONG M2K 03-21");
                  }
                  Attached Files
                  Last edited by johnnybegoode; 02-18-2021, 03:53 PM.

                  Comment


                    #10
                    Hello johnnybegoode,

                    Market orders fill immediately and cannot be paired.

                    The Managed approach cannot pair orders in opposite directions, the Unmanaged approach can.

                    If new orders are placed on each bar, then your conditions are evaluating as true on every bar. Change the conditions so they are not true on every bar. (For example assign the order to a variable in OnOrderUpdate() and only allow a new order if the order variable is null)

                    Use prints if you want to know why the condition is true.


                    No, the Unmanaged approach is unmanaged. Orders will not be ignored for any reason. The EntriesPerDirection and EntryHandling will not have affect. Same for internal order handling rules. These manage orders in the for the Managed approach and do not affect Unmanaged strategies.



                    Yes, there absolutely is a way to achieve your goal when your write code correctly, as done in the example script I have provided you which works with out issue.

                    If the code is not written correctly the behavior will not be what you desire.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      what do you mean in code i.e.
                      If new orders are placed on each bar, then your conditions are evaluating as true on every bar. Change the conditions so they are not true on every bar.
                      (For example assign the order to a variable in OnOrderUpdate() and only allow a new order if the order variable is null)

                      Show me an example condition for unmanaged pair order that work.
                      and I need to get rid of Exit on Session Close
                      TY
                      Last edited by johnnybegoode; 02-18-2021, 05:01 PM.

                      Comment


                        #12
                        Hello johnnybegoode,

                        Where you have mentioned:
                        "what do you mean:
                        (For example assign the order to a variable in OnOrderUpdate() and only allow a new order if the order variable is null)"

                        I am meaning that if a variable is null and no object is assigned, to place the orders. If the variable is not null and an order is assigned do not place the orders.

                        If you are asking about the code in the UnmanagedOCOBracketExample for the AssignOrderToVariable() method, this is because the order object must be returned from the argument of OnOrderUpdate() and not from the order method.

                        From the help guide:
                        "Notes
                        •OnOrderUpdate() will run inside of order methods such as EnterLong() or SubmitOrderUnmanaged(), therefore attempting to assign an order object outside of OnOrderUpdate() may not return as soon as expected. If your strategy is dependent on tracking the order object from the very first update, you should try to match your order objects by the order.Name (signal name) from during the OnOrderUpdate() as the order is first updated."
                        https://ninjatrader.com/support/help...rderupdate.htm

                        To confirm, you are finding the UnmanagedOCOBracketExample_NT8 example I have provided you a link to in post #8 is insufficient for demonstrating how to place paired orders?
                        Can you further detail how this example is not meeting your needs?
                        The example demonstrates how to use OCO with two entry orders in opposite directions. Can you clarify what you are wanting to achieve instead?

                        Where you have mentioned:
                        "Show me an example condition for unmanaged pair order that work."
                        If the example is insufficient because the orders are entry orders, I have a second example that demonstrates this with exit orders in the unmanaged approach.
                        https://ninjatrader.com/support/foru...269#post802269

                        Where you have mentioned:
                        "and I need to get rid of Exit on Session Close"

                        Then uncheck Exit on session close in the strategy properties window.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          AssignOrderToVaiable ()
                          works for entry, but not exit

                          The Exit is continuous!

                          Code:
                          protected override void OnBarUpdate()
                          {
                          if (
                          Profit >= ProfitTarget
                          && (PositionsAccount[0].MarketPosition == MarketPosition.Short)
                          && (PositionsAccount[1].MarketPosition == MarketPosition.Long)
                          && (PositionsAccount[0].Quantity > 0)
                          && (PositionsAccount[1].Quantity > 0)
                          && (Positions[0].MarketPosition == MarketPosition.Short)
                          && (Positions[1].MarketPosition == MarketPosition.Long)
                          && (Positions[0].Quantity > 0)
                          && (Positions[1].Quantity > 0)
                          && (entryOrderExitLongM2K == null)
                          && (entryOrderExitShortMES == null)
                          )
                          {
                          SubmitOrderUnmanaged(0, OrderAction.BuyToCover, OrderType.MIT,
                          Positions[0].Quantity, 9999999999, 9999999999, "", "EXIT SHORT MES (ProfitTarget)");
                          SubmitOrderUnmanaged(1, OrderAction.SellShort, OrderType.MIT,
                          Positions[1].Quantity, 0, 0, "", "EXIT LONG M2K (ProfitTarget)");
                          }
                          }
                          Code:
                          private void AssignOrderToVariable(ref Order order)
                          {
                          
                          if (order.Name == "LONG MES 03-21" && entryOrderLongMES != order)
                          entryOrderLongMES = order;
                          
                          if (order.Name == "SHORT MES 03-21" && entryOrderShortMES != order)
                          entryOrderShortMES = order;
                          
                          if (order.Name == "LONG M2K 03-21" && entryOrderLongM2K != order)
                          entryOrderLongM2K = order;
                          
                          if (order.Name == "SHORT M2K 03-21" && entryOrderShortM2K != order)
                          entryOrderShortM2K = order;
                          
                          if (order.Name == "EXIT LONG MES (ProfitTarget)" && entryOrderExitLongMES != order)
                          entryOrderExitLongMES = order;
                          
                          if (order.Name == "EXIT SHORT MES (ProfitTarget)" && entryOrderExitShortMES != order)
                          entryOrderExitShortMES = order;
                          
                          if (order.Name == "EXIT LONG M2K (ProfitTarget)" && entryOrderExitLongM2K != order)
                          entryOrderExitLongM2K = order;
                          
                          if (order.Name == "EXIT SHORT M2K (ProfitTarget)" && entryOrderExitShortM2K != order)
                          entryOrderExitShortM2K = order;
                          }
                          Code:
                          protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                          {
                          
                          AssignOrderToVariable(ref order);
                          
                          if (order.Name == "LONG MES 03-21")
                          entryOrderLongMES = order;
                          
                          if (order.Name == "SHORT MES 03-21")
                          entryOrderShortMES = order;
                          
                          if (order.Name == "LONG M2K 03-21")
                          entryOrderLongM2K = order;
                          
                          if (order.Name == "SHORT M2K 03-21")
                          entryOrderShortM2K = order;
                          
                          if (order.Name == "EXIT LONG MES (ProfitTarget)")
                          entryOrderExitLongMES = order;
                          
                          if (order.Name == "EXIT LONG M2K (ProfitTarget)")
                          entryOrderExitLongM2K = order;
                          
                          if (order.Name == "EXIT Short MES (ProfitTarget)")
                          entryOrderExitShortMES = order;
                          
                          if (order.Name == "EXIT Short M2K (ProfitTarget)")
                          entryOrderExitShortM2K = order;
                          }
                          Attached Files
                          Last edited by johnnybegoode; 02-18-2021, 11:50 PM.

                          Comment


                            #14
                            Hello johnnybegoode,

                            This is Jim responding on behalf of Chelsea who is out of the office at this time.

                            Are the order objects being assigned, but OnBarUpdate logic is firing faster than the the orders are submitted and processed in OnBarUpdate? This could be seen if the strategy is operating off a very small data series or if it is calculating OnEachTick with high volatility. You can confirm with prints, one inside your order submission condition, and another in OnOrderUpdate where the order is assigned to confirm. A solution may be to set a bool in the condition, so the condition is only processed once, and then to reset the bool when the object is assigned which can prevent additional submissions before the Order object assignment is complete.

                            Ultimately, the orders would still be firing because the condition is allowing the strategy to do so. After looking into the above, if you are still getting the multiple submissions, please attach debug output for the condition that controls these actions, and let us know what these values are when the additional orders are submitted unexpectedly. The debug output there would be the reason why those additional submissions have taken place.

                            For example, if the following condition became true and allowed additional submissions, how did each part of the condition evaluate when additional orders were submitted?

                            Code:
                            if (
                            Profit >= ProfitTarget
                            && (PositionsAccount[0].MarketPosition == MarketPosition.Short)
                            && (PositionsAccount[1].MarketPosition == MarketPosition.Long)
                            && (PositionsAccount[0].Quantity > 0) && (PositionsAccount[1].Quantity > 0)
                            && (Positions[0].MarketPosition == MarketPosition.Short)
                            && (Positions[1].MarketPosition == MarketPosition.Long)
                            && (Positions[0].Quantity > 0) && (Positions[1].Quantity > 0)
                            && (entryOrderExitLongM2K == null)
                            && (entryOrderExitShortMES == null)
                            We look forward to assisting.
                            JimNinjaTrader Customer Service

                            Comment


                              #15
                              Yes, it is firing faster than submitting.
                              That is the exaxt code that did not work. I tried.
                              It is operating as if the conditions do not matter.
                              What else can i do?
                              How would you set the bool?
                              Last edited by johnnybegoode; 02-19-2021, 12:25 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by gentlebenthebear, Today, 01:30 AM
                              1 response
                              8 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by Aviram Y, Today, 05:29 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by cls71, Today, 04:45 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by TradeForge, Today, 02:09 AM
                              1 response
                              22 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by elirion, Today, 01:36 AM
                              2 responses
                              14 views
                              0 likes
                              Last Post elirion
                              by elirion
                               
                              Working...
                              X