Freeze volatility at the entry signal

ATR-based exits adapt to market volatility, but recalculating a stop distance on every bar can unintentionally widen risk after entry. This TradingView Pine Script v6 strategy captures the ATR distance when a confirmed 10-period EMA crosses above the 30-period EMA. It submits a long entry and a fixed relative stop and target together. It is a teaching example, not a claim that the crossover is profitable.

Convert price distance into ticks

ATR is expressed in price units, while the loss and profit parameters of strategy.exit are expressed in ticks. Divide 1.5 times the signal ATR by syminfo.mintick, round to the nearest integer and enforce a minimum of one tick. Set the target to twice that tick count. If ATR is 2.00 and tick size is 0.25, the stop distance is 12 ticks and the target is 24 ticks. For a hypothetical fill at 100, the stop is 97 and the target is 106. If the fill is 101 instead, the levels become 98 and 107.

Fixed ATR Bracket Exits in Pine Script

Hypothetical fill at 100 with fixed distances of 3 and 6 price units. The plotted path is illustrative, not historical data.

Pine Script implementation

//@version=6
strategy("Fixed ATR Bracket", overlay=true, pyramiding=0)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
atr = ta.atr(14)
signal = ta.crossover(fast, slow)
if barstate.isconfirmed and signal and strategy.position_size == 0 and not na(atr)
    riskTicks = math.max(1, int(math.round(1.5 * atr / syminfo.mintick)))
    strategy.entry("L", strategy.long, qty=1)
    strategy.exit("Bracket", "L", loss=riskTicks, profit=2 * riskTicks)
plot(fast, color=color.blue)
plot(slow, color=color.orange)

The exit uses relative tick distances tied to entry ID L, so it does not require knowing the eventual fill price when the entry is submitted. The exit is not reissued on every bar; the distances remain fixed for that trade. The example uses one unit and no pyramiding. Position sizing is intentionally separate: monetary risk depends on tick value, quantity, fees and execution.

Understand simulated execution

With default strategy settings, a market order created at bar close generally fills on the next available tick, normally the next bar open in historical testing. TradingView's broker emulator uses assumptions about intrabar price movement; when both exits are touched in one bar, the simulated sequence may differ from actual execution. Bar Magnifier can improve historical granularity where available but cannot guarantee live fills. Alerts and external execution systems require their own order reconciliation and protective order handling. See the official strategy documentation for details.

Testing and risk

Compare fixed ATR brackets with a fixed-tick baseline using identical entries, realistic commissions and slippage. Test different volatility regimes, gaps through stops, and bars touching both exits. Evaluate out-of-sample drawdown, average trade and sensitivity to ATR multipliers rather than selecting the best historical result. A stop order cannot guarantee the intended loss amount. This example has not been compiled or backtested here. Trading involves risk of loss.

For custom volatility exits and alert integration, see Custom TradingView strategy Development Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.