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 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 gcztraders, Yesterday, 03:58 AM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by reynoldsn, 05-10-2024, 07:04 PM
        1 response
        14 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by franciscog21, 05-10-2024, 05:27 PM
        1 response
        30 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by llanqui, Today, 11:10 AM
        0 responses
        9 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Started by llanqui, Today, 10:29 AM
        0 responses
        9 views
        0 likes
        Last Post llanqui
        by llanqui
         
        Working...
        X