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

{NinjaScript - NT8} - Change from StopLoss to Trailing Stop Loss

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

    {NinjaScript - NT8} - Change from StopLoss to Trailing Stop Loss

    Hello,
    In my strategy I create trades with StopLoss Orders, but if some conditions happend, I want to delete StopLoss order and create a Traling Stop instead for that trade.
    Please, could you give me some light on this? Some sample code idea?
    Thank you so much

    #2
    Hello fscabrera03,

    Thanks for your post.

    If I am understanding correctly, you are currently using SetStopLoss and you are asking about using SetTrailStop. Is this correct?

    Are you using the Strategy Builder or working directly in Ninjascript?

    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Yes, that is!
      Im using ninjascript.

      Comment


        #4
        Hello fscabrera03,

        Thanks for your reply.

        On any given order you can use a SetStopLoss OR a SetTrailingStop but you cannot use both on the same order so what you want to do would not work.

        What you can do is to create a trailing stop in your script using the SetStopLoss() that you are already using.

        For example, lest say you want a 20 tick stop loss and then want to move the stop to breakeven when the profit is 20 ticks and then begin trailing at that point on a tick by tick basis. This example would be for a long entry.

        if (your conditions to enter a trade && Position.MarketPosition == MarketPosition.Flat)
        {
        SetStopLoss(CalculationMode.Ticks, 20);
        // Long entry order here
        }

        if (Position.MarketPosition == MarketPosition.Long && some bool is true && Close[0] >= Position.AveragePrice + 20 * TickSize)
        {
        SetStopLoss(CalculationMode.Price, Position.AveragePrice); // move the stop to breakeven
        set some bool to false to prevent moving it to B/E again
        tickCount =1; // this is an integer variable you would need to create for the tick by tick adjustment
        }

        if (Position.MarketPosition == MarketPosition.Long && some bool is false)
        {

        if (Close[0] > Position.AveragePrice + (20 +tickCount) * TickSize) // adjust higher each time by tickCount
        {
        SetStopLoss(CalculationMode.Price, Position.AveragePrice+(tickCount * TickSize));
        tickCount ++; // increment to next tick
        }

        .
        The example above is not exactly code but is provided to give you some idea that you can work with and test. A bool variable is used to switch from adjusting to breakeven to then trailing tick by tick. You would have to create the bool and you would have to "reset" it before each entry.

        Also, note that before the entry, the stop is set back to 20 ticks, this is important because the set methods retain the last value used
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          If I change the Step Frequency to 5 how will this work as it will only move every 5 Ticks? How will I configure that?

          Comment


            #6
            Hello councilsoft,

            Thanks for your note.

            Are you referring to moving the stop loss for every 5 ticks in your favor instead of every 20 ticks as seen in the example my colleague Paul shared?

            If so, you would need to change the SetStopLoss() method to use 5 ticks instead of 20 ticks, SetStopLoss(CalculationMode.Ticks, 5). Then you could check if Close[0] >= Position.AveragePrice + 5 * TickSize in your conditions instead of Close[0] >= Position.AveragePrice + 20 * TickSize.

            See this reference sample which demonstrates modifying the price of stop loss and profit target orders: https://ninjatrader.com/support/help...of_stop_lo.htm

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_BrandonH View Post
              Hello councilsoft,

              Thanks for your note.

              Are you referring to moving the stop loss for every 5 ticks in your favor instead of every 20 ticks as seen in the example my colleague Paul shared?

              If so, you would need to change the SetStopLoss() method to use 5 ticks instead of 20 ticks, SetStopLoss(CalculationMode.Ticks, 5). Then you could check if Close[0] >= Position.AveragePrice + 5 * TickSize in your conditions instead of Close[0] >= Position.AveragePrice + 20 * TickSize.

              See this reference sample which demonstrates modifying the price of stop loss and profit target orders: https://ninjatrader.com/support/help...of_stop_lo.htm

              Let us know if we may assist further.
              No, i am not referring to the actual Stop Loss Value but the Frequency in the step. See attached. This determines how often the stops will be moved as we get more in the money. But i dont see it in the SetStopLoss or SetTrailStop methods as an argument to pass.

              Comment


                #8
                Hello councilsoft,

                Thanks for your note.

                NinjaScript strategies are separate from ATM strategy templates and the Set methods in NinjaScript do not work the same as ATM strategy templates.

                You would need to program the logic to move your stop when the Close price reaches a certain number of ticks in profit. This could be seen in Paul's sample code in post #4. In Paul's example, he checks to see if the price is 20 ticks in his favor and moves the stop loss.

                Something you could consider is programming your NinjaScript strategy to use an ATM strategy template for your stop losses and trailing stops. Note this would require manually coding your logic in an unlocked script and cannot be accomplished with the Strategy Builder.

                We have an example of using an ATM strategy template in a NinjaScript strategy that you could reference when developing your own NinjaScript strategy. To view the script, open a New > NinjaScript Editor window, open the Strategies folder, and select the SampleAtmStrategy file to see the code to accomplish this.

                Also, see the help guide documentation below for detailed information and sample code.

                Using ATM Strategies in NinjaScript: https://ninjatrader.com/support/help...strategies.htm
                ATM Strategy Methods: https://ninjatrader.com/support/help...gy_methods.htm

                Let us know if we may assist further.
                Brandon H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by mmenigma, Yesterday, 03:25 PM
                1 response
                11 views
                0 likes
                Last Post NinjaTrader_Gaby  
                Started by kujista, Today, 05:44 AM
                0 responses
                7 views
                0 likes
                Last Post kujista
                by kujista
                 
                Started by ZenCortexCLICK, Today, 04:58 AM
                0 responses
                9 views
                0 likes
                Last Post ZenCortexCLICK  
                Started by sidlercom80, 10-28-2023, 08:49 AM
                172 responses
                2,281 views
                0 likes
                Last Post sidlercom80  
                Started by Irukandji, Yesterday, 02:53 AM
                2 responses
                18 views
                0 likes
                Last Post Irukandji  
                Working...
                X