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 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 frslvr, 04-11-2024, 07:26 AM
    9 responses
    123 views
    1 like
    Last Post caryc123  
    Started by rocketman7, Today, 09:41 AM
    4 responses
    15 views
    0 likes
    Last Post rocketman7  
    Started by selu72, Today, 02:01 PM
    1 response
    9 views
    0 likes
    Last Post NinjaTrader_Zachary  
    Started by WHICKED, Today, 02:02 PM
    2 responses
    15 views
    0 likes
    Last Post WHICKED
    by WHICKED
     
    Started by f.saeidi, Today, 12:14 PM
    8 responses
    21 views
    0 likes
    Last Post f.saeidi  
    Working...
    X