Below is an optimized technical strategy script in Pine Script (TradingView) that combines the best elements of the "Composite Trend + Volatility Guard" strategy and incorporates alert functionality for buy and sell signals. This script also visualizes entry and exit points on the chart for easy interpretation.
Pine Script: Composite "Trend + Volatility Guard" Strategy with Alerts
//@version=5
strategy("Leveraged KOSPI200 ETN Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// === INPUTS ===
atrLen = input.int(14, title="ATR Length")
donchianHighLen = input.int(20, title="Donchian High Length")
donchianLowLen = input.int(10, title="Donchian Low Length")
volatilityThreshold = input.float(8.0, title="Volatility Threshold (%)", step=0.1)
riskPerTrade = input.float(0.5, title="Risk Per Trade (%)", step=0.1)
// === INDICATORS ===
atr = ta.atr(atrLen)
donhi = ta.highest(high, donchianHighLen)
donlo = ta.lowest(low, donchianLowLen)
// Volatility as a percentage of price
volatilityPct = (atr / close) * 100
// Position sizing based on ATR
positionSize = (riskPerTrade / 100) * strategy.equity / atr
// === ENTRY CONDITIONS ===
longCondition = close > donhi[1] and volatilityPct < volatilityThreshold
shortCondition = close < donlo[1]
// === EXIT CONDITIONS ===
exitLongCondition = close < ta.sma(close, 5) or volatilityPct > volatilityThreshold
exitShortCondition = close > ta.sma(close, 5)
// === STRATEGY LOGIC ===
if (longCondition)
strategy.entry("Long", strategy.long, qty=positionSize)
if (shortCondition)
strategy.entry("Short", strategy.short, qty=positionSize)
if (exitLongCondition)
strategy.close("Long")
if (exitShortCondition)
strategy.close("Short")
// === ALERTS ===
alertcondition(longCondition, title="Buy Signal", message="BUY: Enter Long Position")
alertcondition(shortCondition, title="Sell Signal", message="SELL: Enter Short Position")
alertcondition(exitLongCondition, title="Exit Long Signal", message="EXIT LONG: Close Long Position")
alertcondition(exitShortCondition, title="Exit Short Signal", message="EXIT SHORT: Close Short Position")
// === VISUALIZATIONS ===
plot(donhi, color=color.green, linewidth=2, title="Donchian High")
plot(donlo, color=color.red, linewidth=2, title="Donchian Low")
bgcolor(volatilityPct > volatilityThreshold ? color.new(color.red, 90) : na, title="High Volatility Warning")
// Entry/Exit Markers
plotshape(series=longCondition, title="Buy Marker", location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
plotshape(series=shortCondition, title="Sell Marker", location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
plotshape(series=exitLongCondition, title="Exit Long Marker", location=location.abovebar, color=color.orange, style=shape.labeldown, text="EXIT LONG")
plotshape(series=exitShortCondition, title="Exit Short Marker", location=location.belowbar, color=color.orange, style=shape.labelup, text="EXIT SHORT")