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' 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.
    Kreyenhagen
    NinjaTrader Ecosystem Vendor - Tyche Trading

    #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.
    Kate W.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by funk10101, Today, 12:02 AM
    0 responses
    3 views
    0 likes
    Last Post funk10101  
    Started by gravdigaz6, Yesterday, 11:40 PM
    1 response
    7 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by MarianApalaghiei, Yesterday, 10:49 PM
    3 responses
    10 views
    0 likes
    Last Post NinjaTrader_Manfred  
    Started by XXtrader, Yesterday, 11:30 PM
    0 responses
    4 views
    0 likes
    Last Post XXtrader  
    Started by love2code2trade, 04-17-2024, 01:45 PM
    4 responses
    28 views
    0 likes
    Last Post love2code2trade  
    Working...
    X