Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Cancel a stop loss order

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

    Cancel a stop loss order

    I'm entering a new order and setting a stop loss order in the customary way. (the _positionManager is just way to clean up my strategy class).

    Code:
    EnterLong(_positionManager.OriginalPositionSize, _positionManager.EntryName);
    SetStopLoss(_positionManager.EntryName, CalculationMode.Price, _positionManager.StopPrice, false);
    When the position is in my favor, I want to scale out of the original position and move the stop into positive territory. The code to do this runs fine. The problem is, I cannot force the original stop loss order to cancel.

    Code:
    // Cancel the stop order.
    if (_positionManager.StopOrder != null && string.Equals(_positionManager.StopOrder.Name, "Stop Loss", StringComparison.CurrentCultureIgnoreCase))
    CancelOrder(_positionManager.StopOrder); // Does nothing!
    
    _positionManager.EntryName = string.Format("{0} Long: {1}", _positionManager.OrderCount, _congestionZone.GetHashCode());
    ExitLong(_closeSize, _positionManager.EntryName, "");
    When the "CancelOrder(...)" line executes, nothing happens. I've placed debugging stop points in both the "OnOrderUpdate" and "OnExecutionUpdate" methods before executing that line and neither are hit. Below is the log. You can see the 1st(only) trade has a quantity of 185,128 and the original stop loss isn't cancelled until the original order is completely closed out and you'll notice the quantity matches the original order's quantity even though that original orders quantity is systematically reduced.

    Since the "CancelOrder" event doesn't do anything and there's no indication of it in the log, how can I force a cancellation of a stop loss order?


    3/30/2011 12:00:00 PM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 3/30/2011 12:00:00 PM: BarsInProgress=0 Action=Buy OrderType=Market Quantity=185,128 LimitPrice=0 StopPrice=0 SignalName='1 Long: -687182827' FromEntrySignal=''
    3/30/2011 12:00:00 PM Strategy 'MathewsWay/202261300': Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='1 Long: -687182827' Mode=Price Value=1.40174666666667 IsSimulatedStop=False IsMarketIfTouched=False
    4/4/2011 6:00:00 AM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 4/4/2011 6:00:00 AM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=92,564 LimitPrice=0 StopPrice=0 SignalName='1 Long: -974923138' FromEntrySignal=''
    4/4/2011 6:00:00 AM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 4/4/2011 6:00:00 AM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=92,564 LimitPrice=0 StopPrice=1.4161'8 SignalName='1 Stop: -974923138' FromEntrySignal=''
    4/7/2011 2:00:00 PM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 4/7/2011 2:00:00 PM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=46,282 LimitPrice=0 StopPrice=0 SignalName='1 Long: -1379288906' FromEntrySignal=''
    4/7/2011 2:00:00 PM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 4/7/2011 2:00:00 PM: BarsInProgress=0 Action=Sell OrderType=StopMarket Quantity=46,282 LimitPrice=0 StopPrice=1.4198'1 SignalName='1 Stop: -1379288906' FromEntrySignal=''
    4/11/2011 7:00:00 AM Strategy 'MathewsWay/202261300': Entered internal SubmitOrderManaged() method at 4/11/2011 7:00:00 AM: BarsInProgress=0 Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''
    4/11/2011 7:00:00 AM Strategy 'MathewsWay/202261300: Cancelled pending exit order, since associated position is closed, orderId='NT-00001-129' account='Sim101' name='Stop loss' orderState=Working instrument='EURUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.40175 quantity=185,128 tif=Gtc oco='NT-00000-129' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2011-03-30 12:00:00' gtd='2099-12-01' statementDate='2020-09-05'
    Enabling NinjaScript strategy 'MathewsWay/202261300' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=2 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=False SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes

    #2
    Hello Herrwolf1,

    This is correct, set methods cannot be cancelled.

    It would be necessary to use exit orders or the unmanaged approach to cancel orders is code.
    Below is a link to examples.


    Also, as set methods cannot be unset, I recommend calling set methods before calling the entry method.
    Chelsea B.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by NullPointStrategies, Today, 05:17 AM
    0 responses
    41 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
    64 views
    0 likes
    Last Post NabilKhattabi  
    Started by Deep42, 03-06-2026, 12:28 AM
    0 responses
    41 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