Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel order if not filled after 12 bars

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

    Cancel order if not filled after 12 bars

    Hello,

    I am writing a code for an strategy. I have not been able to create a command for placing an order that expires/cancels if it is not filled after 12 bars. Any advise is greatly appreciated

    #2
    Hello Jorge.andres.o,

    Thanks for your post and welcome to the Ninjatrader forums!

    As far as logic, you can use an int counter that you set to 0 when you place the order and as long as your market position is still flat to increment the counter once per bar, once the counter meets 12 then issue the cancel order: https://ninjatrader.com/support/help...ancelorder.htm

    Here is a link to an educational example of using CancelOrder(): https://ninjatrader.com/support/help...thod_to_ca.htm

    Comment


      #3
      Thanks Paul,

      I have not been able to make it work. I want to cancel the order if not filled after 5 bars. However, the algorithm is cancelling the order after 1 bar. Could you help me find the error on this example?

      protected override void OnBarUpdate()
      {
      if (BarsInProgress != 0)
      return;

      if (CurrentBars[0] < 1)
      return;

      // Set 1
      if (CrossAbove(EMA1, EMA2, 1))

      {
      EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (23 * TickSize)) , @"11111111");
      EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (15 * TickSize)) , @"222222");
      EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (10 * TickSize)) , @"3333333");
      EnterShortLimit(Convert.ToInt32(DefaultQuantity), (GetCurrentAsk(0) + (8 * TickSize)) , @"444444444");
      barNumberOfOrder = CurrentBar;
      Draw.VerticalLine(this, "tag1"+CurrentBar, 0, Brushes.Red, DashStyleHelper.Solid, 10);
      Print("orders submmitted");
      }
      //if more than 5 bar has elapsed, cancel entry order
      if (myEntryOrder == null && CurrentBar > barNumberOfOrder + 5)
      CancelOrder(myEntryOrder);
      Print("order cancelled");


      }

      Comment


        #4
        Hello Jorge.andres.o,

        Thanks for your reply.

        It looks like you are using a managed approach strategy which means that in the case of a limit order, the limit order will be automatically canceled if not filled in the bar it is submitted.

        I would suggest reviewing the requirements of the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

        As is, you would need to continue resubmitting the orders on the next five bars unless filled. This may be the easier way for you rather than canceling with the extra code needed.

        Alternately you can use the Advanced order method which allows you to specify GTC (Good till cancel) which means once placed you orders would remain until filled or cancelled. You can read about that here: https://ninjatrader.com/support/help...r_handling.htm

        NOTE Based on your code example and assuming you used th advanced Order Handling, in order to be able to cancel the multiple orders, you would have to use signal names for the orders and then assign (In OnOrderUpdate), each entry order to its own order object. Please review the example in the help guide (which only shows 1 order) https://ninjatrader.com/support/help...ancelorder.htm

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        25 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        121 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        64 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        41 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        46 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X