Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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
    Paul H.NinjaTrader Customer Service

    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
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by cupir2, Yesterday, 07:44 PM
        3 responses
        21 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by reynoldsn, Yesterday, 07:26 PM
        2 responses
        14 views
        0 likes
        Last Post reynoldsn  
        Started by MartinT, 05-17-2023, 06:00 AM
        18 responses
        173 views
        0 likes
        Last Post flybuzz
        by flybuzz
         
        Started by sgordet, Today, 05:24 AM
        2 responses
        20 views
        0 likes
        Last Post sgordet
        by sgordet
         
        Started by Board game geek, Today, 02:20 AM
        1 response
        15 views
        0 likes
        Last Post NinjaTrader_Erick  
        Working...
        X