Coding for a Trailing Stop That Activates After Reaching a Profit Target

Fact checked by
Mike Christensen, CFOA
July 18, 2024
To set up a trailing stop that activates after reaching a profit target in Pine Script, create a stop loss variable and update it once the price hits the profit level, using strategy.exit to dynamically adjust the stop loss.

To code a trailing stop that only activates once a certain profit target is reached, you need to set up a variable that monitors the stop loss, and update it once the price crosses your predefined profit level. This type of setup is best done using Pine Script in TradingView, as most brokers do not directly support such a feature.

Basic Steps:

1. Create a Stop Loss Variable: Set a variable to track the stop loss level. Initially, this will be set to the value of your regular stop loss.

2. Monitor Profit Target: Add a condition to check when the price crosses your desired profit target.

3. Update the Stop Loss: Once the profit target is reached, update the stop loss variable to start trailing the price.

4. Run strategy.exit() on Every Bar: Ensure the strategy.exit function is running continuously on every bar to dynamically update the stop loss as the price moves.

Ready to automate your trading? Try a free 7-day account:
Try it for free ->