Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel Order after "X" Bars have printed

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

    Cancel Order after "X" Bars have printed

    Hi,
    I've searched the forum with no luck for a simple way to cancel an order after several bars have printed.
    I'm using a Ninjascript strategy that does NOT call an ATM strat.
    I've looked at BarsArray but they all seem to apply to indicators on a multi time frame chart.

    Is there any way to do this using a variable and possibly a BarsArray? For instance, if order is placed, set a variable orderplaced = 1 then if 6 bars have printed without the order being filled, reset the variable back to 0 so the order is automatically cancelled on the next bar?

    I just need some way to start the bar count when the order is placed.
    Thanks,
    Joe

    #2
    Joe,

    You can use BarsSinceEntry().

    Code:
    if (BarsSinceEntry() > 10)
         CancelOrder(someIOrder);
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh,
      I don't think that will work since there is no Entry, just a pending order that has been placed. However, I think I found the answer in Help, under Cancel Order. Here it is for anyone looking at this thread.

      private IOrder myEntryOrder = null;
      private int barNumberOfOrder = 0;

      protected override void OnBarUpdate()
      {
      // Submit an entry order at the low of a bar
      if (myEntryOrder == null)
      {
      myEntryOrder = EnterLongLimit(0, true, 1, Low[0], "Long Entry");
      barNumberOfOrder = CurrentBar;
      }

      // If more than 5 bars has elapsed, cancel the entry order
      if (myEntryOrder != null && CurrentBar > barNumberOfOrder + 5)
      CancelOrder(myEntryOrder);
      }

      I tried it and it works great.
      Thanks Anyway, sorry to bother you!
      Joe

      Comment


        #4
        Glad you figured it out...
        RayNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        52 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        142 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        160 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        96 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        276 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X