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 NullPointStrategies, Today, 05:17 AM
    0 responses
    44 views
    0 likes
    Last Post NullPointStrategies  
    Started by argusthome, 03-08-2026, 10:06 AM
    0 responses
    124 views
    0 likes
    Last Post argusthome  
    Started by NabilKhattabi, 03-06-2026, 11:18 AM
    0 responses
    65 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    42 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