Trading Strategy

Supertrend Strategy

A powerful trend-following indicator that combines ATR-based volatility bands with directional momentum to identify trend reversals with precision.

Precision Trend Reversal Detection

The Supertrend indicator uses Average True Range (ATR) to create dynamic support and resistance levels. When price crosses these levels, it generates precise entry signals. Combined with a 200 EMA trend filter, this strategy captures high-probability trend reversals while avoiding false signals in choppy markets.

Trend Reversal Type
4h Chart Timeframe
Intermediate Complexity
Trending Best Market

Signal Logic

Long (Buy)

Supertrend flips from bearish to bullish (direction crosses above 0) while price is above the 200 EMA trend filter.

Short (Sell)

Supertrend flips from bullish to bearish (direction crosses below 0) while price is below the 200 EMA trend filter.

Implementation

//@version=6
strategy("Supertrend Strategy", overlay=true, initial_capital=1000000, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// ==========================================
// 🔹 INPUTS
// ==========================================
// Group: Supertrend Settings
grp_st = "Supertrend Settings"
period   = input.int(10, "ATR Period", minval=1, group=grp_st)
mult     = input.float(3.0, "Multiplier", step=0.1, group=grp_st)

// Group: Trend Filter
grp_trend = "Trend Filter"
use_trend = input.bool(true, "Use Trend Filter (EMA 200)", group=grp_trend)
ema_len   = input.int(200, "Trend EMA Length", minval=1, group=grp_trend)

// Group: Risk Management
grp_risk  = "Risk Management"
tp_mult   = input.float(2.0, "Take Profit (ATR Multiplier)", step=0.1, group=grp_risk)

// ==========================================
// 🔹 CALCULATIONS
// ==========================================
// Supertrend calculation - returns [supertrend, direction]
[supertrend, direction] = ta.supertrend(mult, period)

// Trend EMA
ema_val = ta.ema(close, ema_len)

// Trend Conditions
is_uptrend   = use_trend ? close > ema_val : true
is_downtrend = use_trend ? close < ema_val : true

// ==========================================
// 🔹 STRATEGY LOGIC
// ==========================================
// Long Entry: Supertrend flips from bearish (-1) to bullish (1)
direction_changed_long = direction[1] < 0 and direction > 0
long_signal = is_uptrend and direction_changed_long

// Short Entry: Supertrend flips from bullish (1) to bearish (-1)
direction_changed_short = direction[1] > 0 and direction < 0
short_signal = is_downtrend and direction_changed_short

// ==========================================
// 🔹 EXECUTION & RISK MANAGEMENT
// ==========================================

if long_signal
    strategy.entry("Long", strategy.long, comment="L | Supertrend")

if short_signal
    strategy.entry("Short", strategy.short, comment="S | Supertrend")

// Exit with ATR-based stops
atr_val = ta.atr(period)
if strategy.position_size > 0
    strategy.exit("Exit Long", "Long", stop=close - (atr_val * mult), limit=close + (atr_val * tp_mult), comment_loss="SL", comment_profit="TP")

if strategy.position_size < 0
    strategy.exit("Exit Short", "Short", stop=close + (atr_val * mult), limit=close - (atr_val * tp_mult), comment_loss="SL", comment_profit="TP")

// ==========================================
// 🔹 VISUALS
// ==========================================
// Plot EMA
plot(use_trend ? ema_val : na, "Trend EMA", color=color.new(color.orange, 0), linewidth=2)

// Plot Supertrend line
plot(supertrend, "Supertrend", color=direction > 0 ? color.new(color.green, 0) : color.new(color.red, 0), linewidth=2)

// Plot Signals
plotshape(long_signal, title="Long Signal", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY", textcolor=color.white)
plotshape(short_signal, title="Short Signal", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL", textcolor=color.white)

// Background color for trend
bgcolor(direction > 0 ? color.new(color.green, 95) : color.new(color.red, 95))