Features & Settings

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

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.

Tom Hartman

Marketing

1 Min Read Reviewed by Mike Christensen Fact-checked by Mike Christensen
BluSky — The Future of Trading. Prop firm futures trading. Sign up at BluSky.pro.
Update history
  1. Correction

    Traders ask whether Pine Script has a built-in trailing stop, so the guidance now covers strategy.exit()'s trail parameters as an alternative to a stop loss variable.

To code a trailing stop that only activates once a certain profit target is reached, you can 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.

Alternatively, strategy.exit() has a built-in trailing stop: trail_price or trail_points sets the activation level, and trail_offset sets how many ticks the stop trails behind price1.

‍

References

1 tradingview.com, Concepts / Strategies

  • Post-Only Order: How It Works

    Post-only orders guarantee maker-side execution or cancellation. Learn how they work, why exchanges cancel or reprice them, and when to use one.

  • Difference Between Mark And Last In TOS

    Mark price and last price mean different things on trading platforms. Learn how each is calculated, why they diverge, and which to use for stops.

  • Broker Rules Now Follow the Asset Class

    Learn why a setting disappeared in TradersPost: broker rules for order types, shorting, and time in force now apply per asset class, not per broker.

  • Walk-Forward Optimization Trading Guide

    Master walk-forward optimization trading to test strategies across market regimes, reduce curve fitting, and prepare alerts for disciplined automation.

Ready to put what you read into practice?

Free 7-day trial

Set-up in 3 minutes

Paper account for testing

By continuing, you agree to the TradersPost Terms of Service and Privacy Policy. This site is protected by reCAPTCHA.