Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Stop Loss Order Mysteriously Canceled

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

    Stop Loss Order Mysteriously Canceled

    I'm having an order management problem. The strategy I'm writing has the strage behavior of canceling the stop loss of an open order. The stategy has two sets of orders, one with a signal name 'pioneer', the first one, that has a stop loss and a target, and another order with signal name 'runner' that has a stop loss but no target. Here are the details. See that I have taken from NinjaTrader examples.

    I have the following Defaults:
    Calculate = Calculate.OnBarClose;
    EntriesPerDirection = 3;
    EntryHandling = EntryHandling.AllEntries;



    In `OnBarUpdate` I have something like:
    EnterLong(NumberOfContracts, entrySignal);
    if (NumberOfRunners > 0)
    {
    EnterLong(NumberOfRunners, runnerSignal);
    }


    In `OnOrderUpdate` I have:
    if (order.Name.StartsWith(pioneerPrefix))
    {
    entryOrder = order;
    }
    else if (order.Name.StartsWith(runnerPrefix))
    {
    runnerOrder = order;
    }


    In `OnExecutionUpdate`:
    // Update our exit order quantities once orderstate turns to filled and we have seen execution quantities match order quantities
    else if (order.OrderState == OrderState.Filled && sumFilled == order.Filled)
    {
    // Stop-Loss order for OrderState.Filled
    stoploss = order.IsLong ? Low[LowestBar(Lows[0], 5)] : High[HighestBar(Highs[0], 5)];
    stopOrder = exitOrderStopMarket(order.Filled, stoploss, stopLossSignalName + order.Name, order.Name);
    if (order == entryOrder) // this is the pioneer order
    {
    targetOrder = exitOrderLimit(Math.Min(order.Filled, NumberOfContracts), (upperSeries[0] + lowerSeries[0]) / 2, targetSignalName + order.Name, order.Name);
    }
    }


    Scenario
    Here is the behavior (assume 5-min bars):
    1. both orders are filled successfully, let's say, 2 contracts for pioneer, and 1 contract for the runner.
    2. pioneer target is filled, the 2 stop loss orders for pioneer are canceled successfully.
    3. the runner order and runner stop loss order is still open.
    4. on the next bar, after about 5-mins, the runner stop loss is canceled and the runner order is left open (without the stop loss).

    It seems after the pioneer target is filled and the pioneer stop loss orders are canceled, on the next bar the runner stop loss is also canceled, it's not canceled immediately, but, for some reason, it mysteriously cancels on the next bar open, in this case, around 5-mins.

    Here is the stack trace, see the call to `DoCancel` from `OnBarUpdate`.
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Strat egyBase.Process(object sender, NinjaTrader.Cbi.OrderEventArgs orderUpdate)
    NinjaTrader.Core.dll!NinjaTrader.Cbi.Account.Raise OrderUpdate(NinjaTrader.Cbi.OrderEventArgs e)
    NinjaTrader.Core.dll!NinjaTrader.Cbi.Account.Order UpdateCallback(NinjaTrader.Cbi.Order order, string orderId, double limitPrice, double stopPrice, int quantity, double averageFillPrice, int filled, NinjaTrader.Cbi.OrderState orderState, System.DateTime time, NinjaTrader.Cbi.ErrorCode error, string comment, System.DateTime statementDate)
    NinjaTrader.Core.dll!NinjaTrader.Cbi.Account.Cance l(System.Collections.Generic.IEnumerable<NinjaT rad er.Cbi.Order> orders)
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Strat egyBase.DoOrder(NinjaTrader.Cbi.Order order, NinjaTrader.Cbi.OrderState[] orderStates)
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Strat egyBase.DoCancel(NinjaTrader.Cbi.Order order)
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Strat egyBase.OnOnBarUpdate(bool[] toProcess)
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Ninja ScriptBase.Update(int idx, int bip)
    NinjaTrader.Core.dll!NinjaTrader.NinjaScript.Ninja ScriptBase.SubscribeBarsUpdate.__BarsUpdate|1(obje ct _, NinjaTrader.Data.BarsUpdateEventArgs o)
    NinjaTrader.Core.dll!NinjaTrader.Data.BarsSeries.A dd(NinjaTrader.Data.Bars bars, double open, double high, double low, double close, System.DateTime time, long volume, double tickSize, bool isBar, double bid, double ask)
    NinjaTrader.Core.dll!NinjaTrader.Data.BarsSeries.A ddMarketData(NinjaTrader.Data.Bars bars, double open, double high, double low, double close, System.DateTime time, long volume, double bid, double ask, long tickId, bool isDailyFakeBar)
    NinjaTrader.Core.dll!NinjaTrader.Data.BarsSeries.O nMarketData(NinjaTrader.Data.MarketDataEventArgs obj)
    NinjaTrader.Core.dll!NinjaTrader.Data.MarketDataEv entWrapper.OnMarketData(object sender, NinjaTrader.Data.MarketDataEventArgs e)
    NinjaTrader.Core.dll!NinjaTrader.Cbi.Instrument.On RealtimeDataTimerElapsed.__Action|1(NinjaTrader.Cb i.Instrument.SubscribedThread[] threads, int i, NinjaTrader.Cbi.Instrument.RealtimeEvents evts)


    See attached Log of the above Scenario.

    Question:
    Why is the stop loss being canceled on the next bar after the target is filled and no action is happening in the strategy and how can I keep the stop loss around?

    Thanks so much for any help with this.
    Attached Files
    Last edited by bfalls; 06-26-2024, 07:34 PM.

    #2
    Check my thread. I believe the answer is the same;

    Comment


      #3
      Hello bfalls,

      From the given information it would be difficult to tell what may be happening. If you are not using live until cancelled orders that may be why the order is being cancelled, orders expire after 1 bar. We would otherwise need to know what code is being used for the order that is being cancelled to get a better idea of what may be happening.

      Comment


        #4
        lucyb is the hero here. It turned out using the ExitLongStopMarket(int barsInProgressIndex, bool isLiveUntilCancelled, int quantity, double stopPrice, string signalName, string fromEntrySignal); overloaded method did the trick, specifying true for the isLiveUntilCancelled​ argument. Thanks so much for your help!

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Today, 05:17 AM
        0 responses
        52 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        130 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        70 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        43 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        47 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X