OPEN-SOURCE SCRIPT

Trend Pullback Guard

436
Trend Pullback Guard

A trend has started. Does its next pullback stabilize — or lose its structure?

Trend Pullback Guard follows pullbacks through a defined sequence: detection, stabilization, continuation confirmation, or invalidation. It combines an EMA trend regime with frozen structure levels, volatility-normalized pullback depth and candle-overlap measurements.

The same rules work in both directions. Everything is included in one overlay indicator.

QUICK VISUAL GUIDE

• Blue line: Fast EMA; gray line: Slow EMA.
• Small blue dot: A pullback has been detected.
• Subtle green/red zone: The developing long/short pullback range.
• Red level: Frozen structure boundary.
• Amber level: Confirmation threshold after stabilization.
• Green upward/red downward triangle: Long/short continuation conditions confirmed.
• Red cross: A close broke the frozen structure boundary.
• Gray square: The trend regime was lost or the observation window ended.

The dashboard distinguishes an active episode's LAST CLOSE from frozen LAST EPISODE values. The structure and confirmation levels describe the rules; they are not suggested order prices.

1 — ESTABLISH THE TREND

Default settings use a 21-period EMA and a 55-period EMA.

For a long regime, the fast EMA must be above the slow EMA and the slow EMA must be above its value three bars earlier. Short conditions are reversed.

Before a pullback can begin, a confirmed impulse bar must lie entirely beyond the fast EMA's 0.25 ATR touch band and close in the trend direction relative to the previous close. This arms the monitor. A new episode requires a new qualifying impulse after the previous episode ends.

2 — DETECT AND FREEZE THE REFERENCE

A long pullback begins when a later bar reaches the fast EMA's upper touch band and closes below the previous close. Short detection uses the lower band and a higher close.

At detection, the script freezes:

• ATR from the previous bar, using a 14-period ATR by default.
• The lowest low of the preceding 10 bars minus 0.1 frozen ATR for long structure; the highest high plus that buffer for short structure.
• The preceding 10-bar high for long pullback depth, or low for short depth.

The detection bar is excluded from these historical reference windows. No pivot that needs future bars is used. The structure boundary remains fixed throughout the episode.

3 — WAIT FOR STABILIZATION

By default, two consecutive bars must avoid lowering the previous bar's low for a long setup, or raising its high for a short setup.

The confirmation level is then frozen at the highest high of those two stabilization bars plus 0.1 frozen ATR for longs. Shorts use their lowest low minus the buffer.

A new adverse extreme of the monitored pullback resets stabilization and removes the old trigger. The script must establish a new one. A bar that makes a new adverse extreme cannot also confirm continuation.

4 — APPLY THE GUARD

Confirmation requires a LATER bar to close strictly beyond the frozen trigger and on the trend side of the fast EMA. The original trend regime must still hold, and both guard limits must pass:

• Maximum pullback depth: 3.0 frozen ATR by default. Depth measures the move from the pre-detection reference extreme to the worst pullback extreme observed so far.
• Maximum mean candle overlap: 70% by default. Each sample is the intersection of two consecutive high-low ranges divided by the smaller range. Samples are averaged from detection through the current bar; a zero-range candle counts as 100% overlap.

TOO DEEP and HIGH OVERLAP explain why confirmation is blocked. These are rule thresholds, not estimated probabilities. High overlap can later decline; the maximum observed depth cannot shrink within an episode.

HOW AN EPISODE ENDS

• STRUCTURE BROKEN: The close moves strictly beyond the frozen structure boundary against the setup. A wick alone is insufficient.
• TREND LOST: The EMA regime no longer supports the monitored direction.
• CONTINUATION CONFIRMED: A later close passes the trigger, trend and guard requirements.
• WINDOW ENDED: No confirmation within 12 bars after detection by default.

Structure failure takes priority over other outcomes, followed by loss of the trend regime. An otherwise valid confirmation on the final allowed bar takes priority over timeout. Only one episode is active at a time.

ILLUSTRATIVE LONG EXAMPLE

Suppose the pre-pullback high is 104 and the frozen ATR is 2. A pullback low at 100 represents a depth of 2 ATR.

After two qualifying stabilization bars, their highest high is 102.50. With the default buffer, the confirmation level is 102.70.

A later close above 102.70 confirms only if the EMA regime remains bullish, the close is above the fast EMA, and the depth and overlap limits still pass. A wick above 102.70 does not confirm. A new pullback low on that bar resets the trigger instead.

This example illustrates the rules; it is not a historical trade or a return claim.

DASHBOARD AND ALERTS

The compact dashboard shows status, direction, bars since detection, depth in frozen ATR units, mean overlap, guard status, structure and confirmation levels.

The Confirmed / broken / other counters describe events in the loaded chart history. Other combines trend loss and timeout. These counts are not trading win rates; confirmed events are not followed to a profit target or stop.

Seven alert conditions are provided: pullback detected, stabilization, long confirmation, short confirmation, structure broken, trend lost and observation window ended. Stabilization can alert again if a new adverse extreme resets the setup. Create the desired alerts separately in TradingView.

USAGE AND LIMITATIONS

Use standard candles with a fast EMA length smaller than the slow EMA length. Long-only, short-only and both-direction modes are available. All calculations use the chart's symbol and timeframe; there is no external indicator dependency or higher-timeframe data request.

Event states and markers update only at confirmed bar closes and are never shifted backward. EMA lines can move while a live bar is forming. Changes to settings, available history or historical prices can change the resulting events.

This is a descriptive technical-analysis tool, not an automated trading strategy. Confirmation describes the specified conditions at that close; it does not guarantee that the trend will continue. No profitability or superior performance has been established.

Clause de non-responsabilité

Les informations et publications ne sont pas destinées à être, et ne constituent pas, des conseils ou recommandations financiers, d'investissement, de trading ou autres fournis ou approuvés par TradingView. Pour en savoir plus, consultez les Conditions d'utilisation.