Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to update StopLoss order

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

    How to update StopLoss order

    Hello,

    I'm in learning phase and putting down some code with a basic strategy. I want to learn how to individualy and programaticaly manage evey trade.
    The folowing code just make a EnterLong order at every closed bar whene the closed bar close above the EMA(9).
    After the order is filled also automaticali create a StopLoss order and a TakeProfit order.
    Oll orders (EnterLong, SetStopLoss and SetProfitTarget) create during an OnBarUpdate cicle are "referenced" by the same "SignalName".


    Code:
           protected override void OnBarUpdate()
            {
                //Add your custom strategy logic here.
                if (CurrentBar < BarsRequiredToTrade)
                    return;
    
                if (Close[0] > ema_009[0]){
                    orderSeq++;
                    String entrySignal = "GB1_" + orderSeq;
    
                    EnterLong(1000, entrySignal);
    
                    SetStopLoss(entrySignal, CalculationMode.Price, ema_015[0], false);
                    SetProfitTarget(entrySignal, CalculationMode.Pips, 10, 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.OrderState == OrderState.Submitted
                    || order.OrderState == OrderState.Working
                    || order.OrderState == OrderState.Filled
                ) {
                    Print("Orfer: " + order);
                    }
            }​


    Since my code can produce many Long position before the oldest (fist oppened) are closed (see script output leter down), I have the need to manage/update a specific StopLoss or TakeProfit order.

    Orfer: orderId='NT-00000-357' account='Backtest' name='GB1_1' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00000-357' account='Backtest' name='GB1_1' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00001-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15065 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00001-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15065 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00002-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15229 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00002-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15229 quantity=1.000 tif=Gtc oco='NT-00000-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00000-357' account='Backtest' name='GB1_1' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15129 onBehalfOf='' id=-1 time='2022-09-09 00:45:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00004-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15072 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00004-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15072 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00005-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15228 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00005-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15228 quantity=1.000 tif=Gtc oco='NT-00001-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00003-357' account='Backtest' name='GB1_2' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15128 onBehalfOf='' id=-1 time='2022-09-09 00:50:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00007-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15077 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00007-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15077 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00006-357' account='Backtest' name='GB1_3' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15109 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00010-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15082 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00010-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15082 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00009-357' account='Backtest' name='GB1_4' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15114 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Submitted instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Working instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00013-357' account='Backtest' name='Stop loss' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15086 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00013-357' account='Backtest' name='Stop loss' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='Stop Market' limitPrice=0 stopPrice=1.15086 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Submitted instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Working instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=0 averageFillPrice=0 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00012-357' account='Backtest' name='GB1_5' orderState=Filled instrument='GBPUSD' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1.000 tif=Gtc oco='' filled=1000 averageFillPrice=1.15119 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00008-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15209 quantity=1.000 tif=Gtc oco='NT-00002-357' filled=1000 averageFillPrice=1.15209 onBehalfOf='' id=-1 time='2022-09-09 00:55:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00011-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15214 quantity=1.000 tif=Gtc oco='NT-00003-357' filled=1000 averageFillPrice=1.15214 onBehalfOf='' id=-1 time='2022-09-09 01:00:00' gtd='2099-12-01' statementDate='2022-12-18'
    Orfer: orderId='NT-00014-357' account='Backtest' name='Profit target' orderState=Filled instrument='GBPUSD' orderAction=Sell orderType='MIT' limitPrice=0 stopPrice=1.15219 quantity=1.000 tif=Gtc oco='NT-00004-357' filled=1000 averageFillPrice=1.15219 onBehalfOf='' id=-1 time='2022-09-09 01:05:00' gtd='2099-12-01' statementDate='2022-12-18'

    ATM Startegyes are not taking in consideration except in case it is possible to have full programmable control over it for each open position separately.

    My questions are:
    1. How I can programaticaly manage/update a specific StopLoss or TakeProfit order for each current open position?
    2. How I can track / manage open positions?

    Sorry for my spammi post caused by huge log export.

    Thank you in advance for any advice / suggestions / code snipets.
    Best regards,
    Valter




    #2
    Hello morfeo360,

    Thanks for your post.

    To track and manage orders that are placed by the strategy the orders would need to be tracked as Order objects. Then, you could get information about those Order objects or manage orders using OnExecutionUpdate() and OnOrderUpdate().

    See this reference sample that demonstrates tracking and managing Order objects as well as an example of using OnExecutionUpdate() and OnOrderUpdate(): https://ninjatrader.com/support/help...and_onexec.htm

    See this reference sample for information about monitoring stop loss and profit target orders: https://ninjatrader.com/support/help..._and_profi.htm

    And, see this reference sample about modifying stop loss and profit target orders: https://ninjatrader.com/support/help...of_stop_lo.htm

    Note that when you dynamically change the stop loss price or profit target price in the strategy, you should always reset the stop loss or profit target price / offset value when your strategy is flat. Otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position. This is noted on the Set method help guide pages linked below.

    SetStopLoss(): https://ninjatrader.com/support/help...etstoploss.htm
    SetProfitTarget(): https://ninjatrader.com/support/help...ofittarget.htm

    Let me know if I may assist you further.
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hello BrandonH,

      Its bean a while from when I ask for help and for that I apologize, I was unable to check end try to make some tests. Also want to than you for your help. I think that you helped me enough, and now is my turn to do something with the new knowledge

      Thank you for your help.
      Best regards,
      Valter

      Comment


        #4
        Is there a way to modify the stop loss of an OCO. I keep getting an error. OCO ID cannot be reused.

        Comment


          #5
          Hello,

          Thank you for your post.

          OCO (One-Cancels-Others) IDs need to be always unique for each grouping and cannot be re-used. When an OCO ID has been used with an order that is filled, rejected, or cancelled, any future orders using the same OCO ID will result in this error.

          This often occurs with SetStopLoss() and SetProfitTarget() as well as with Atm Strategies, should one of the exit orders be rejected. For example, the stop is submitted with OCO ID ‘12345’ at an invalid price and is rejected. The target is attempted to be submitted with the same OCO ID ‘12345’, however errors the OCO cannot be re-used as this was used with the stop order, which was rejected.

          To prevent the strategy from being deactivated, you can set the strategy's RealtimeErrorHandling property to IgnoreAllErrors or StopCancelCloseIgnoreRejects. With this property set, you can then trap order rejections in OnOrderUpdate if you would like to have the strategy attempt a different action. For example, you could call ExitLong() to exit the position, or you could use ExitLongStopMarket/ExitLongLimit to place other protective orders.

          RealtimeErrorHandling - https://ninjatrader.com/support/help...orhandling.htm

          OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
          0 responses
          571 views
          0 likes
          Last Post Geovanny Suaza  
          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
          0 responses
          330 views
          1 like
          Last Post Geovanny Suaza  
          Started by Mindset, 02-09-2026, 11:44 AM
          0 responses
          101 views
          0 likes
          Last Post Mindset
          by Mindset
           
          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
          0 responses
          549 views
          1 like
          Last Post Geovanny Suaza  
          Started by RFrosty, 01-28-2026, 06:49 PM
          0 responses
          549 views
          1 like
          Last Post RFrosty
          by RFrosty
           
          Working...
          X