Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trailing SL Moving Back Into Negative During Trade

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

    Trailing SL Moving Back Into Negative During Trade

    Hello NinjaTrader community,

    I’m working on a strategy where I have a fixed 35-tick stop loss and a trailing stop that should only move in favor of the trade as the price moves in profit. My goal is for the trailing stop to never move into negative territory, but to lock in profits as the price moves in favor of the trade.

    However, I’ve encountered an issue where the trailing stop is moving back into negative territory, even after the trade has moved in profit. The trailing stop should only move in a positive direction once it’s been set, and should not retrace back to a worse price if the trade starts moving against the position.

    I’ve tried modifying the logic to ensure that the trailing stop behaves as expected, but it continues to go negative, which isn’t what I want.

    If anyone has had a similar issue or knows how to fix this, I would appreciate any help!

    Thank you!
    Attached Files

    #2
    Hello Buttony,

    I would suggest looking at the following sample, that shows how to do a trailing stop which only moves when in profit. In regard to your attachment, I would suggest uploading the .cs file directly if you are trying to show code.

    Comment


      #3
      Hey Buttony. So question about your newStop. I noticed your Initial Stop size is 35, and your trailing stop size is 25. And you are going off the max of the Position.AveragePrice - 35 ticks or the Close[0] - the trailing 25 ticks? Are you adding to the positions when this goes back into the negative? That's the only way I can see this Price move at all. The other issue could be the fact that you are calculating Close[0] - the 25 ticks. The Close[0] will be wherever price is currently at, since you are using OnPriceChange and are only taking the higher of the two,
      So if your (Position.Average Price - 35) is say 10500 and (Close[0] - 25) is 10600, it will take the higher of the two. But Close[0] can still move down 50 ticks to 10550, and it will still be the higher of the two.

      I want to say you may need to instead of taking newStop = Math.Max(Position.AveragePrice - InitialStopSize, Close[0]-TrailingStopSize) you do newStop = Math.Max(newStop, Close[0] - TrailingStopSize) and maybe after you enterLong, in OnExecutionUpdate and the position is filled you assign newStop to Position.AveragePrice - InitialStopSize.
      So, when you get into a long position you are setting newStop to the default stop price. Once it goes back into recheck that trailing logic it will check between the current stoploss price and the Close[0]-25 and take the larger of the two. The next time it checks, it will take that number and will only taken Close[0]-25 over what it current is set to, if it is larger.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      50 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 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