Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Prevent Backwards Movement of Stop Loss in Ninjascript

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

    Prevent Backwards Movement of Stop Loss in Ninjascript

    I am working on a Strategy that has a trailing stop loss that is attached to an EMA. In the example of a short, as the EMA moves down, the stop loss would move down with it. But if the EMA retraces up, and the moves back down, the stop loss will retrace as well. I am wanting to add some type of logic that acts like the "modify toward last price only" when manually attaching an ATM to an indicator.

    Here is the code that I have currently for a short position:

    if (EMA[1] > EMA[0])
    {
    SetStopLoss(CalculationMode.Price, EMA[0] +10);
    }

    I have attached a picture for reference.


    #2
    The problem with this is if the EMA goes up and then starts to go back down, it meets the condition you have given it to set the StopLoss at 10 units above your EMA, which, if higher than its previous position, will then appear to be going up with the EMA.

    Have you tried something like:

    Code:
    // STEP 1 Place before OnStateChange
    private double ShortStop;
    
    // STEP 2 Place in OnStateChange
    else if (State == State.DataLoaded)
    {​
    
         ShortStop = double.MaxValue
    }
    
    // STEP 3 Place in OnBarUpdate
    
    if (ShortStop >= EMA[0] + 10))
    {
         ShortStop = EMA[0] + 10;
    }
    
    
    // STEP 4 Place also in OnBarUpdat​e
    
    if (EMA[1] > EMA[0])
    {
         SetStopLoss(CalculationMode.Price, ShortStop);
    }​
    Edited for clarity:

    In the above script, you are creating the variable in step 1, giving it a default value in step 2, then you are assigning it the stop value you wish to maintain every time there is a new EMA low to the new variable in step 3, and finally, essentially checking every time there is a new ema low, that you are making sure your stop loss matches the stop level value assigned to the variable in step 4.
    Last edited by smcllr; 06-25-2023, 12:47 PM.

    Comment


      #3
      Hello ScalpRule,

      Thanks for your post.

      smcllr is correct. The condition you have for placing/moving the stop loss is becoming true so the stop loss is moved when the EMA goes up and then starts to go back down.

      You could consider implementing the suggestion that smcllr shared to better handle the stop loss in your script. This would allow you to set a default value to a variable and then update that variable with a new value anytime the variable value is greater than or equal to EMA()[0] + 10. That variable would then be used to offset your stop loss order as smcllr noted.
      <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

      Comment


        #4
        Or you can calculate your trail steps every time using high-entry price to move stop.

        Comment


          #5
          Originally posted by smcllr View Post
          The problem with this is if the EMA goes up and then starts to go back down, it meets the condition you have given it to set the StopLoss at 10 units above your EMA, which, if higher than its previous position, will then appear to be going up with the EMA.

          Have you tried something like:

          Code:
          // STEP 1 Place before OnStateChange
          private double ShortStop;
          
          // STEP 2 Place in OnStateChange
          else if (State == State.DataLoaded)
          {​
          
          ShortStop = double.MaxValue
          }
          
          // STEP 3 Place in OnBarUpdate
          
          if (ShortStop >= EMA[0] + 10))
          {
          ShortStop = EMA[0] + 10;
          }
          
          
          // STEP 4 Place also in OnBarUpdat​e
          
          if (EMA[1] > EMA[0])
          {
          SetStopLoss(CalculationMode.Price, ShortStop);
          }​
          Edited for clarity:

          In the above script, you are creating the variable in step 1, giving it a default value in step 2, then you are assigning it the stop value you wish to maintain every time there is a new EMA low to the new variable in step 3, and finally, essentially checking every time there is a new ema low, that you are making sure your stop loss matches the stop level value assigned to the variable in step 4.

          Thank you! I knew I needed to add another condition, I just didn't know where to start with it. Thank you for this!

          Comment


            #6
            Originally posted by ScalpRule View Post


            Thank you! I knew I needed to add another condition, I just didn't know where to start with it. Thank you for this!


            You are very welcome.
            Last edited by smcllr; 06-25-2023, 08:10 PM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            51 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            127 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X