Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

When to set Stop/Profit targets?

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

    When to set Stop/Profit targets?


    When is the preferred section of code to call SetStopLoss/SetProfitTarget?

    1) I've seen older sample code which sets the stop loss/profit target within the (State == State.Configure) section
    2) Then, in new sample code, it seems like the stop loss/profit target are being set within the OnExecutionUpdate() section
    3) In other code, I've seen the stop loss/profit target call in the OnBarUpdate() section, either before or after the EnterShort()/EnterLong() is called.

    Out of these three possibilities, is any one of them preferred? Or, is it pretty much three different ways of accomplishing the same task?

    Thanks,





    #2
    Hello timmbbo,

    Thank you for your post.

    Ultimately, it depends on your needs and your preferences. If you are using a static offset, such as simply a number of ticks or percentage away from the entry price, you could call SetStopLoss() and SetProfitTarget() in State.Configure.

    If you want dynamic stops/targets, such as set to a specific price based on a variable or stops/targets that move (like a breakeven, trail stop, or profit chase) then you would need to call them dynamically such as OnBarUpdate() or OnExecutionUpdate().

    As noted in the help guide:
    • It is suggested to call this method from within the strategy OnStateChange() method if your stop loss/profit target price/offset is static
    • You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the stop loss/profit target price while in an open position
    • Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss/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/profit target order on your next open position
    Here are the help guide pages for SetStopLoss() and SetProfitTarget() for more details:​Here is an example of a strategy that modifies the price of stop loss and profit target orders:


    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by CarlTrading, 03-31-2026, 09:41 PM
    1 response
    43 views
    0 likes
    Last Post NinjaTrader_ChelseaB  
    Started by CarlTrading, 04-01-2026, 02:41 AM
    0 responses
    20 views
    0 likes
    Last Post CarlTrading  
    Started by CaptainJack, 03-31-2026, 11:44 PM
    0 responses
    30 views
    1 like
    Last Post CaptainJack  
    Started by CarlTrading, 03-30-2026, 11:51 AM
    0 responses
    48 views
    0 likes
    Last Post CarlTrading  
    Started by CarlTrading, 03-30-2026, 11:48 AM
    0 responses
    38 views
    0 likes
    Last Post CarlTrading  
    Working...
    X