Here is a complete, production-ready TradingView Pine Script implementing the Composite “Trend + Volatility Guard” strategy from your playbook. It includes:
- Dynamic Donchian breakout entries
- ATR-based volatility filter
- Respects risk control rules by avoiding trades in chop
- Visual buy/sell markers
- Alert conditions
- Integrated strategy performance testing for backtesting
✅ Composite Trend + Volatility Guard Strategy (with Alerts & Visuals)
//@version=5
strategy("Composite Trend + Volatility Guard (KOSPI200 ETN)", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// === Parameters ===
donchian_entry_len = input.int(20, minval=1, title="Donchian High (Entry Window)")
donchian_exit_len = input.int(10, minval=1, title="Donchian Low (Exit Window)")
atr_period = input.int(14, minval=1, title="ATR Lookback Period")
vol_thresh = input.float(0.08, minval=0.01, maxval=1, title="Min ATR/Price Volatility Threshold (e.g. 0.08 = 8%)")
plot_atr = input.bool(true, title="Show ATR on Chart?")
show_labels = input.bool(true, title="Show Entry/Exit Labels?")
enable_alerts = input.bool(true, title="Enable Strategy Alerts")
// === Calculations ===
price = close
atr = ta.atr(atr_period)
atr_pct = atr / price
donchian_high = ta.highest(high, donchian_entry_len)[1] // Avoid breakout on current bar
donchian_low = ta.lowest(low, donchian_exit_len)[1]
// === Entry Condition ===
long_entry = price > donchian_high and atr_pct > vol_thresh
long_exit = price < donchian_low
// === Position Management ===
if (long_exit)
strategy.close("Long", comment="Donchian Exit")
if (long_entry)
strategy.entry("Long", strategy.long, comment="Donchian Breakout")
// === Plotting ===
plotshape(long_entry and not strategy.position_size, title="Buy Signal", location=location.belowbar, color=color.lime, size=size.small, style=shape.labelup, text="BUY")
plotshape(long_exit and strategy.position_size > 0, title="Sell Signal", location=location.abovebar, color=color.red, size=size.small, style=shape.labeldown, text="SELL")
plot(plot_atr ? atr : na, title="ATR", color=color.orange, linewidth=1)
// === Alerts ===
alertcondition(long_entry, title="BUY Alert", message="KOSPI200 ETN BUY Signal Triggered (Breakout + Volatility Confirmed)")
alertcondition(long_exit, title="SELL Alert", message="KOSPI200 ETN SELL Signal Triggered (Breakdown)")
// === Labels (Enhanced UX) ===
var label last_buy = na
var label last_sell = na
if (long_entry and show_labels)
label.delete(last_buy)
last_buy := label.new(bar_index, low, "BUY", style=label.style_label_up, textcolor=color.white, color=color.green, size=size.small)
if (long_exit and show_labels)
label.delete(last_sell)
last_sell := label.new(bar_index, high, "SELL", style=label.style_label_down, textcolor=color.white, color=color.red, size=size.small)