Filtering unusually wide breakout candles in Pine Script
A breakout candle can signal momentum but also expose a trader to unusually large price movement before entry. This educational TradingView strategy tests whether rejecting unusually wide signal candles improves the consistency of a channel breakout system. It uses the prior bar's ATR as a reference so the signal candle cannot inflate its own threshold.
Rules and worked example
Calculate the highest high of the previous 20 completed bars. A long signal occurs when the close crosses above this rolling series. Require the signal candle's high minus low to be no greater than 1.5 times the previous 14-period ATR. This is candle range, not true range: overnight gaps are not directly measured by this filter. With prior ATR 2.0, the maximum permitted range is 3.0. If resistance is 102 and both candidates close at 103.5, a high of 104 and low of 101.5 passes, while a high of 106 and low of 100 fails. Assume the preceding close was at or below its own channel reference.

Two hypothetical breakout candles with identical closes but different ranges. Green denotes a passing filter, red a rejected candidate, not candle direction.
Pine Script v6 example
//@version=6
strategy("ATR Range Guard", overlay=true, pyramiding=0,
default_qty_type=strategy.fixed, default_qty_value=1)
length = input.int(20, "Channel length", minval=2)
cap = input.float(1.5, "Maximum range / ATR", minval=0.1)
priorATR = ta.atr(14)[1]
upper = ta.highest(high, length)[1]
lower = ta.lowest(low, 10)[1]
breakout = ta.crossover(close, upper)
rangeOK = not na(priorATR) and priorATR > 0 and
high - low <= cap * priorATR
if barstate.isconfirmed and strategy.position_size == 0 and breakout and rangeOK
riskTicks = math.max(1, int(math.round(2 * priorATR / syminfo.mintick)))
strategy.entry("L", strategy.long)
strategy.exit("Protect", "L", loss=riskTicks, profit=2 * riskTicks)
if barstate.isconfirmed and strategy.position_size > 0 and close < lower
strategy.close("L")
plot(upper, "Prior channel", color=color.blue)
plot(lower, "Exit channel", color=color.orange)
The code uses a one-unit position for demonstration. It submits a protective bracket at the same time as the entry instruction, with a stop distance of twice prior ATR converted to ticks and a target twice that rounded distance. For ATR 2.0 and tick size 0.25, the stop is 16 ticks and target 32 ticks. These relative distances refer to the emulator's entry fill, not the signal close. A close below the prior 10-bar low also requests a market exit. A rejected breakout is not chased on subsequent bars unless a new crossover occurs. A rolling channel can change independently of price, so crossover semantics differ from crossing a fixed horizontal level.
Testing and execution limits
Compare an unrestricted baseline with range caps of 1.0, 1.5 and 2.0, keeping entry size, stops, costs and data constant. Measure rejection rate, net returns, drawdown and missed winning trades. Use standard candles, realistic commissions and slippage, out-of-sample periods and Bar Magnifier where available to examine ambiguous intrabar bracket fills. The default strategy market order normally fills on the next available tick, typically the next bar open in historical testing. TradingView broker emulator fills differ from live execution; alerts or broker integrations require separate order reconciliation and protection. The example has not been compiled or backtested here. Trading involves risk of loss, including gaps beyond stops.
For instrument-specific filters and execution integration, see Custom TradingView strategy Development Services.