Trend Filter

A condition that restricts entries to markets trending in your favour. One of the highest-return improvements you can make to most strategies — it doesn't improve the entry signal, it improves which environment it fires in.

A trend filter is an additional condition layered on top of an entry signal that asks a single question: is the market environment right for this trade direction? It does not generate an entry by itself. It gates the entries that your other logic produces.

The most common version is checking whether price is above or below a long-period moving average. If close is above the 200-period EMA, you only take longs. If close is below it, you only take shorts (or you stay flat). That one rule does not improve the entry signal — it improves which market conditions the signal is allowed to fire in.

Why it matters

Oscillator-based entries — RSI crosses, Stochastic reversals, Bollinger Band® touches — work well in ranging markets and produce losses in strong trends. When a market is trending hard to the downside, an RSI reading below 30 is not a mean-reversion opportunity. It is just confirmation that the trend is strong. Fading it is the wrong move.

Adding a trend filter stops you from taking those trades. The cost is fewer setups. The benefit is that the setups you do take are in an environment where the underlying logic has an edge.

ADX is a useful alternative — rather than asking which direction the trend is in, it asks how strong the trend is. A low ADX reading can trigger a mean-reversion filter even when a moving average would suggest a trend exists.

//@version=6
strategy("Trend Filter Demo", overlay=false)

rsi       = ta.rsi(close, 14)
ema200    = ta.ema(close, 200)

// The filter: only allow longs when price is in an uptrend
trendIsUp = close > ema200

// Without the filter: every RSI oversold cross triggers a long
// With the filter: only when price is above the 200 EMA
longCondition = ta.crossover(rsi, 30) and trendIsUp

plot(rsi, "RSI", color=color.purple, linewidth=2)
hline(30, "Oversold", color=color.green, linestyle=hline.style_dashed)
hline(70, "Overbought", color=color.red, linestyle=hline.style_dashed)

// Green background when filter is active (trend is up), red when blocked
bgcolor(trendIsUp ? color.new(color.green, 94) : color.new(color.red, 94))

plotshape(longCondition, style=shape.triangleup, location=location.bottom, color=color.green, size=size.small)

if longCondition
    strategy.entry("Long", strategy.long)

Key takeaway: A trend filter improves environment selection, not the entry signal itself. The same RSI crossover that loses money in a trending market can be profitable when restricted to the regime where mean reversion actually works. Fewer trades is not a problem — better trades is the goal.