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

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.
    Brandon H.NinjaTrader Customer Service

    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

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by rocketman7, Today, 02:12 AM
      5 responses
      23 views
      0 likes
      Last Post rocketman7  
      Started by trilliantrader, 04-18-2024, 08:16 AM
      7 responses
      28 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by samish18, 04-17-2024, 08:57 AM
      17 responses
      66 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Started by briansaul, Today, 05:31 AM
      1 response
      15 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by PaulMohn, Today, 03:49 AM
      1 response
      12 views
      0 likes
      Last Post NinjaTrader_BrandonH  
      Working...
      X