Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unable to cancel previous open orders

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

    #16
    Thank you Jesse,
    I changed my code based on example you sent but unfortunately still doesn't work
    But I noticed one thing:
    I'm trying to make it work in playback but I saw in the example file some order parameters is made in Realtime state.

    Code:
    else if (State == State.Realtime)
    {
    // one time only, as we transition from historical
    // convert any old historical order object references
    // to the new live order submitted to the real-time account
    if (entryOrder != null)
    entryOrder = GetRealtimeOrder(entryOrder);
    if (mar****rder != null)
    mar****rder = GetRealtimeOrder(mar****rder);
    if (stopOrder != null)
    stopOrder = GetRealtimeOrder(stopOrder);
    if (targetOrder != null)
    targetOrder = GetRealtimeOrder(targetOrder);
    }
    Is the ninjatrader playback considered Realtime?
    Because I cannot see any other thing wrong with my code.
    I changed my code in OnOrderUpdate and OnExecutionUpdate with same idea from cancel example code but still doesn't work.
    The code is generating a single entry order to guarantee no mismatch but still doesn't cancel the entry order.
    Attached Files

    Comment


      #17
      I just checked in live trading.
      It still doesn't work.
      I really don't know what I'm doing wrong

      Comment


        #18
        Hello diorfo,

        Please try the sample that I had linked and confirm that works to cancel an order. If so then the problem is something you are doing in your logic in your script.

        Comment


          #19
          Hi Jesse,

          I can't still make it work.

          I made a new simple code below from zero and I couldn't find what's wrong that the canceling is not working.

          Could you please point what is missing?​

          Code:
          namespace NinjaTrader.NinjaScript.Strategies
          {
             public class ordercanceltestOMD : Strategy
             {
                  private Order Buy = null;
          
                  protected override void OnStateChange()
                  {
                         if (State == State.SetDefaults)
                         {
                               Description = @"Place order on price hits below low of last candle, cancel after price hits high of last candle.";
                               Name = "ordercanceltestOMD";
                               Calculate = Calculate.OnBarClose;
                               EntriesPerDirection = 1;
                               EntryHandling = EntryHandling.AllEntries;
                               IsExitOnSessionCloseStrategy = true;
                               ExitOnSessionCloseSeconds = 30;
                               IsFillLimitOnTouch = false;
                               MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                               OrderFillResolution = OrderFillResolution.Standard;
                               Slippage = 0;
                               StartBehavior = StartBehavior.WaitUntilFlat;
                               TimeInForce = TimeInForce.Gtc;
                               TraceOrders = false;
                               RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                               StopTargetHandling = StopTargetHandling.PerEntryExecution;
                               BarsRequiredToTrade = 2;
                               // Disable this property for performance gains in Strategy Analyzer optimizations
                               // See the Help Guide for additional information
                               IsInstantiatedOnEachOptimizationIteration = true;
                         }
                         else if (State == State.Realtime)
                         {
                               if (Buy != null)
                               {
                                     Buy = GetRealtimeOrder(Buy);
                               }
          
                         }
                   }
          
               protected override void OnBarUpdate(){ }
          
               protected override void OnMarketData(MarketDataEventArgs marketDataUpdate)
               {
                    if (State == State.Historical)
                         return;
          
                    if (Buy == null && marketDataUpdate.Price < Low[0])
                    {
                         EnterLongLimit(1, (Low[0] - (20 * TickSize)), "Buy");
                    }
          
                    if (Buy != null && marketDataUpdate.Price > High[0])
                    {
                         CancelOrder(Buy);
                         Print("Buy order canceled");
                    }
               }
          
               protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
               int quantity, int filled, double averageFillPrice,
               Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
               {
                    if (Buy == null && order.Name == "Buy")
                    {
                         Buy = order;
                    }
          
                    if (Buy != null && order.Name == "Buy" && Buy.OrderState == OrderState.Cancelled)
                    {
                         Buy = null;
                    }
          
               }
          
               protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
               {
                    if (Buy != null && Buy == execution.Order && execution.Order.OrderState == OrderState.Filled)
                    {
                         Buy = null;
                    }
                }
             }
          }
          ​
          Last edited by diorfo; 01-14-2023, 02:47 PM.

          Comment


            #20
            Hello diorfo,

            Did you try the cancel order sample and confirm that was working to cancel the order? Did you also read the notes that sample has on that page? The code you are using you did not use a Live Until Cancel Order so you shouldn't be able to cancel that order. You need to use the overload for your order that has IsLiveUntilCanceled and set that to true so the order doesn't expire and can be cancelled.

            EnterLongLimit(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double limitPrice, string signalName)


            Comment


              #21
              Hi Jesse,

              I have read the online documentation and I understood that isLiveUntilCancelled bool was facultative instead required to cancel the orders manually.

              Adding the bool, it worked as expected.

              Thank you very much for supporting

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
              0 responses
              599 views
              0 likes
              Last Post Geovanny Suaza  
              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
              0 responses
              344 views
              1 like
              Last Post Geovanny Suaza  
              Started by Mindset, 02-09-2026, 11:44 AM
              0 responses
              103 views
              0 likes
              Last Post Mindset
              by Mindset
               
              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
              0 responses
              558 views
              1 like
              Last Post Geovanny Suaza  
              Started by RFrosty, 01-28-2026, 06:49 PM
              0 responses
              557 views
              1 like
              Last Post RFrosty
              by RFrosty
               
              Working...
              X