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 Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        633 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        364 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        105 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        567 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        568 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X