Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Going from a Stop Loss to a Trailing Stop within a strategy

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

    Going from a Stop Loss to a Trailing Stop within a strategy

    Hi All,

    I'm writing my first NinjaScript strategy... I'm wanting to set a firm price level stop on an opened position initially (either long or short) and then change the stop to a trailing stop after a specified length of time. Is there a way of doing this...??

    As I understand it if I have a stop set using SetStopLoss() it will always take precedence over another instruction to then use the SetTrailStop() method...??

    So am I right in thinking that once I have called SetStopLoss() and subsequent calls to SetTrailStop() will be ignored...? If this is the case, could you point to how I can go from a firm stop to a trailing stop...?

    Many Thanks

    #2
    This is possible in NT6.5 with the use of IOrder objects and the OnOrderUpdate() method. Please see this reference sample: http://www.ninjatrader-support.com/v...ead.php?t=3917

    The idea is you don't use the Set() methods anymore and do everything manually. You can modify your stop prices whenever you decide.
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Thanks Josh.

      I've looked at the code in this example, and it looks ok. Famous last words etc.

      One question, instead of using the methods you outline here, could I not just continue to call (with a new price) the SetStopLoss() method on each OnBarUpdate()....?? Would this not simulate a trailing stop if the price I passed to it was trailing...??

      Thanks

      Comment


        #4
        Sidartha,

        FYI, I tried various ways of implementing a trailing stop.

        Problem with those other options Josh mentioned is that:

        1. NT won't let you enter another stop with the current stop still working.
        2. So you have to cancel your stop loss and THEN enter the trailing stop. DANGEROUS, in my opinion because if software/hardware or network fails, you're left without a stop.

        A safer way to handle trailing stops is simply to change your original stop loss. NT handles that very well as a change order than cancels and replaces the stop order all in one shot. So it's less risk you're left without a stop.

        If you originally call SetStopLoss(5) for a 5 pip stop loss, you can later call it SetStopLoss(-5) to raise it to a 5.

        What I do is check the Profit or Loss of the position on every tick and raise the Stop loss accordingly.

        In other words, you can implement you're on trailing stop. In fact, when I Did this, I found a far more profitable logic for the trailing stop than the simple logic built into NT.

        Hopefully this helps.

        Sincerely,
        Wayne

        Comment


          #5
          Thanks Wayne.

          So if I understand you right, you are saying that repeatedly calling the SetStopLoss() method for an open position has the same effect of trailing the stop loss...?? And in fact it's potentially better than the methods outlined by Josh because it simply amends the existing Stop...?

          Many Thanks

          Comment


            #6
            Correct. Try it. You'll see in the logs that it cancels the order and replaces it. I verify everything in the logs.

            It also seems simpler than dealing with the IOrder interface.

            Try it both ways. I was pleasantly surprise and happy that SetStopLoss() accepts negative numbers so that properly handles changing the stop higher and higher.

            Sincerely,
            Wayne

            Comment


              #7
              There should be no cancellation of the stop order in the IOrder interface methodology. The stop order is simply modified just like how you are modifying your SetStopLoss(). They should both behave the same.

              Since there are many ways to tackle a problem, by all means, take whichever approach feels more comfortable to you.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Josh, in 6.5.0.10, the SetStopLoss() method returns a void.

                So is it even possible to modify it with the IOrder interface?

                I think that's the other reason I did this way. Maybe you need to change the software to return an IOrder on the SetStopLoss() SetProfitTarget(), and SetTrailStop().

                I couldn't figure out any way to "cancel" or change them which is quite unfortunate if that's the case.

                Sincerely,
                Wayne

                Comment


                  #9
                  Set() methods can not be cancelled. If you require flexibility of cancellation, instead of using the Set() methods, use Exit() methods but trigger their placement from OnOrderStatus() monitoring for filled event from the associated entry order. This acomplishes the same thing as a Set() method. One exception is SetTrailStop() in which case you would need to self code this logic in OnBarUpdate() or OnMarketData() for real-time only adjustments.
                  RayNinjaTrader Customer Service

                  Comment


                    #10
                    Thanks. Those are exactly the reason why, it's far easier to simply use the SetStopLoss() and raise it using custom trailing stop logic. It take far fewer lines of code to write and debug.

                    Suggestion for next version. Why not return the IOrder from the Set() methods? Since they return void now, it won't impact anyone's existing code to return the IOrder so developers have more control.

                    Just a feature suggestion.

                    Sincerely,
                    Wayne

                    Comment


                      #11
                      Unfortunately it is not as simple as just passing back an IOrder object since there is internal logic in the Set() methods. Any external intervention could cause unexpected behaviour.

                      I do see the convenience for sure...
                      RayNinjaTrader Customer Service

                      Comment


                        #12
                        Understood and that makes sense. So it seems fine as it is now. Returning IOrder would only have been icing on the cake.

                        Sincerely,
                        Wayne

                        Comment


                          #13
                          Hey Guys.

                          Is it possible to start with a trailing stop, then submit a stop loss at a certain point which may be overridden by an actual exit call within an NT strategy?

                          I want to have SetTrailStop() within my initialize() method. Once a certain point is reached I can leave the trailing stop in, but I want to submit a SetStopLoss() value to always stay as a break even. If that stop is not hit, I will use an ExitLong / ExitShort call to exit the position.

                          Is this logical? Can I submit a trailing stop initially then use a stop loss that will supersede my trail?

                          Thanks
                          Anthony
                          mrlogik
                          NinjaTrader Ecosystem Vendor - Purelogik Trading

                          Comment


                            #14
                            Unfortunately not. To what you want you are best monitoring OnMarketData() and writing your own code for submission, trail etc... based off on incoming market data.
                            RayNinjaTrader Customer Service

                            Comment


                              #15
                              Ray.

                              Will the OnMarketData() method get called for every tick even if my strategy is end-of-bar?
                              mrlogik
                              NinjaTrader Ecosystem Vendor - Purelogik Trading

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              558 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 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
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X