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 cmoran13, Yesterday, 01:02 PM
        0 responses
        29 views
        0 likes
        Last Post cmoran13  
        Started by PaulMohn, 04-10-2026, 11:11 AM
        0 responses
        21 views
        0 likes
        Last Post PaulMohn  
        Started by CarlTrading, 03-31-2026, 09:41 PM
        1 response
        160 views
        1 like
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 04-01-2026, 02:41 AM
        0 responses
        95 views
        1 like
        Last Post CarlTrading  
        Started by CaptainJack, 03-31-2026, 11:44 PM
        0 responses
        148 views
        2 likes
        Last Post CaptainJack  
        Working...
        X