Trailing Take Profit in Pine Script: Activation, Ticks and Fill Limits
A trailing exit can follow favorable price movement after an activation level is reached. In Pine Script, activation and trailing distance are separate parameters. Neither a simulated trailing order nor an activation above entry guarantees a profitable live fill.

Understand the order units
trail_price specifies an absolute activation price. Alternatively, trail_points specifies the activation distance from entry in ticks. trail_offset specifies the distance behind the favorable extreme, also in ticks. It is not an activation switch.
The earlier example passed a price distance as trail_points and used a zero offset. That did not express the stated 3% activation and 2% trailing distance. Convert a price distance into ticks using the instrument’s syminfo.mintick and use a positive offset.
Complete long-only demonstration
This example uses the filled average entry price as its basis. Pyramiding is disabled, so the basis remains fixed for the position. The 2% distance is measured from entry and converted to a fixed tick distance; it is not recalculated as 2% of each new high.
//@version=6
strategy("Trailing activation and tick distance", overlay=true, pyramiding=0)
activationPercent = input.float(3.0, "Activation above entry (%)", minval=0.1) / 100.0
trailPercent = input.float(2.0, "Trail distance as % of entry", minval=0.1) / 100.0
fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 20)
longCondition = ta.crossover(fastMA, slowMA)
if longCondition and strategy.position_size == 0
strategy.entry("Long", strategy.long)
if strategy.position_size > 0
entryBasis = strategy.position_avg_price
activationPrice = entryBasis * (1.0 + activationPercent)
offsetTicks = math.max(1, int(math.round(entryBasis * trailPercent / syminfo.mintick)))
strategy.exit("Trail", from_entry="Long", trail_price=activationPrice, trail_offset=offsetTicks)
plot(fastMA, "Fast MA", color=color.blue)
plot(slowMA, "Slow MA", color=color.orange)
An entry at 100 with the default inputs sets activation at 103 and a trail distance of approximately 2 price units, subject to tick rounding. After activation, the built-in trailing order tracks favorable highs and can trigger on a reversal. The code submits the trailing order when a calculation first observes an open position, rather than waiting for the chart’s high to cross activation before placing it.
This remains a limited teaching example: there is no protective exit before activation. With default bar-close calculations, the initial trailing order is not submitted immediately at the entry fill. A position that never reaches activation can remain open. Design and test initial protection, maximum holding time and position sizing before considering any live execution.
Why historical fills need inspection
The broker emulator uses historical bar data and assumptions about intrabar movement. Bar Magnifier can provide lower-timeframe detail when available, but it does not establish real fills or eliminate gaps, slippage and liquidity constraints. A lower chart timeframe alone does not solve higher-timeframe repainting or order-routing delays.
Compare order creation time, activation time and fill time in the trade list. Include commission and an instrument-appropriate slippage assumption. If a stop is crossed by a gap, the intended stop price may differ from the fill.
ATR and stepped variants need explicit state
An ATR-based distance must also be converted into ticks for a built-in trail. Decide whether to freeze ATR at entry or update it later; those rules differ. A custom stepped stop must reset when a position closes and must never loosen inadvertently if tightening is the intended rule.
The old stepped fragment did not reset its persistent level. Another fragment never set its activation flag to true. Both have been removed rather than presented as working strategies. For positive prices, close cannot be less than 98% of itself, so the former “always triggers immediately” explanation was also incorrect.
A close-based strategy.close() condition creates a market exit; it is different from a resting stop order that can trigger intrabar. Choose the mechanism deliberately.
The former SPY 68% capture, 1.8% drawdown and AAPL 37% improvement claims had no reproducible records. No trading-performance claim is made for this syntax-checked demonstration.
Source checked October 20, 2026: TradingView strategy and trailing-stop documentation.

