Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel Order After 'X' Number Of Bars

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

    Cancel Order After 'X' Number Of Bars

    I read on the ninjatrader website that when building a strategy it is better to use OnOrderUpdate method to cancel orders. I am curious though if there is a way to set it up so that the order will cancel if a certain number of bars pass before filling.

    I've tried a few things but seem to run into an issue where after it cancels an order the strategy stops taking trades all together...
    I don't seem to get this issue when using the cancel code in the OnBarUpdate Method, but then run into an issue where it will cancel and order, but leaves the order there but then never fills that order even after the price eventually reaches is.

    I currently have the following code:

    public class Strategy : Strategy
    {
    private int indexEntryBar = 0;

    protected override void OnBarUpdate()
    {
    If (myOrder == null)
    {
    indexEntryBar = CurrentBar;
    EnterLongLimit();
    longTrade = true;
    }
    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.Name == "long")
    myOrderl = order;

    if (myOrderl != null && myOrderl == order)
    {
    if (longTrade && CurrentBar - indexEntryBar >= 2)
    {
    CancelOrder(myOrderl);
    myOrderl = null;
    longTrade = false;
    }
    else if (order.OrderState == OrderState.Filled)
    myOrderl = null;
    }
    }
    }
    Last edited by Kreyenhagen; 03-12-2021, 02:05 PM.

    #2
    Hello Kreyenhagen,

    Thank you for your note.

    OnOrderUpdate() is only called when an order is updating, so that wouldn't be the recommended place to cancel an order. I'd suggest doing the cancellation in OnBarUpdate instead. I'd imagine that the issue you're running into is likely occurring when you try to cancel the order after x number of bars but it's already filling, which would cause an error and likely cause the strategy to disable.

    This example from our help guide shows properly cancelling an order:



    Please let us know if we may be of further assistance to you.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Mindset, 04-21-2026, 06:46 AM
    0 responses
    88 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by M4ndoo, 04-20-2026, 05:21 PM
    0 responses
    134 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by M4ndoo, 04-19-2026, 05:54 PM
    0 responses
    68 views
    0 likes
    Last Post M4ndoo
    by M4ndoo
     
    Started by cmoran13, 04-16-2026, 01:02 PM
    0 responses
    118 views
    0 likes
    Last Post cmoran13  
    Started by PaulMohn, 04-10-2026, 11:11 AM
    0 responses
    67 views
    0 likes
    Last Post PaulMohn  
    Working...
    X