Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy builder Trailing stop Logic

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

    Strategy builder Trailing stop Logic

    I have been using strategy builder along with the custom series, and variable functions to create some pretty sophisticated logic in my strategies. However order management has been a bit of a barrier to more complexity. Market orders with an ATR multiple for target and stoploss have been my go to. I may have some more questions about limit order handling in a different post in the future.

    I have been avoiding using the imbedded "stops and targets" section because you cannot optimize those through the strategy analyzer. Instead I have been using conditions and actions to manually build exit logic based on an input so that it is open for optimization.

    My question for the brain trust is, how can I using conditions and actions, custom series, or variables, create a logic for exiting on a trailing stop? probably setting an ATR multiple offset.

    I could create a custom series to track the price I would want to exit, but I dont know how I could stop it from moving away when the price turns towards the stop.

    This might be a weird question, let me know if you need clarification.

    Thanks!


    #2
    Easy to say and do in Ninjascript but basically in the strategy builder you want to compare the new stop price to the current stop price and if it less than (for a long), to not change the stop price. So if you are still in position and you have a new stop price that is more than the current stop price, you set the new stop. However keep in mind that you must issue the stop order at the current stop ( if the new is less) in order to keep the order in place (orders are auto cancelled in the strategy builder if not resubmitted).

    Comment


      #3
      Hello BoneM4nwalkin,

      Thank you for your post.

      Due to limitations in the Strategy Builder, this task would be more simply achieved through an unlocked script in the NinjaScript Editor. One of the main reasons this is difficult in the Strategy Builder is because the SetStopLoss method is only used in OnStateChange when set up in the builder yet in an unlocked script you may use SetStopLoss in OnBarUpdate. For this reason, you have to use exit orders with logic in the conditions and actions as you are already exploring. Another reason a trailing stop is difficult is because of performing mathematical calculations; the strategy builder is not able to perform math on variables or inputs, only on a custom Series<double>. As Tasker-182 mentioned, to keep the exit from moving away when the price moves toward it, you could make comparisons in the logic determining when the price should and should not be modified.

      My colleague has created examples of how to do a breakeven or an auto trail in the Strategy Builder. You could import these examples into your platform and open them in the strategy builder in order to see how the conditions and actions are configured. The examples may be found at the following link:


      Using the ideas demonstrated in the example, you should be able to test out a trailing stop using ATR multiples.

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

      Comment


        #4
        Thank you both for your replies, that really gives me some ideas for sure.

        NinjaTrader_Emily those look like great examples to review when I am around the platform later today. To add more clarity to my initial post, what I am referring to as a "stop Loss" I am really using a exit market order that triggers after price drops below(or above for short) a certain level, which I am using custom series to update the value. Although not elegant, using strategy builder for this allows me to iterate ideas quickly be able to optimize the offsets for targets and stop loss levels for a given strategy.

        Tasker-182 I was vaguely thinking about that method as I was typing up the question, create a custom series that only updates when the market position is not flat and is higher than the previous bar, and then a similar but opposite one for short.

        I have unlocked scripts before, but most of the time I just spend days and days chasing down little bugs in the structure which slows down the strategy development cycle..... Eventually I will put the time in to master ninjascript, but I am finding there is alot you can do with custom series, and creating "switching" functions within your conditions and actions.

        I will be back to ask about limit orders after some time, thanks.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by NullPointStrategies, Yesterday, 05:17 AM
        0 responses
        66 views
        0 likes
        Last Post NullPointStrategies  
        Started by argusthome, 03-08-2026, 10:06 AM
        0 responses
        141 views
        0 likes
        Last Post argusthome  
        Started by NabilKhattabi, 03-06-2026, 11:18 AM
        0 responses
        76 views
        0 likes
        Last Post NabilKhattabi  
        Started by Deep42, 03-06-2026, 12:28 AM
        0 responses
        47 views
        0 likes
        Last Post Deep42
        by Deep42
         
        Started by TheRealMorford, 03-05-2026, 06:15 PM
        0 responses
        51 views
        0 likes
        Last Post TheRealMorford  
        Working...
        X