
이 전략은 트렌드 추적과 브레이크 트레이드를 결합한 다중 시간 프레임 전략으로, 트렌드 필터로 EMA 크로스, 동력 확인 지표로 RSI, ATR으로 동적 위험 관리를 수행한다. 이 전략은 분리된 경보 시스템을 통해 정확한 입출소 신호 관리를 구현하고, 비율 기반의 자금 관리 방법을 사용하여 위험을 통제한다.
이것은 구조적으로 엄격한 트렌드 추적 전략으로, 여러 기술 지표 검증을 통해 신호 신뢰성을 높이고, 과학적인 자금 관리 시스템이 하향 위험을 효과적으로 제어한다. 전략은 특히 트렌드가 명확한 시장 환경에 적합하며, 변동성이 적당한 품종에서 가장 잘 작동한다. 변수를 추가로 최적화하여 적응 메커니즘과 시장 상태 식별 모듈을 추가하면 전략의 안정성과 적응력을 크게 향상시킬 수 있다.
// @version=5
strategy("Trend Breakout Strategy with Separated Alerts", overlay=true, initial_capital=10, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
// --- Parameters ---
var float risk_per_trade = 0.02 // 2% risk per trade
var int ema_fast = 9
var int ema_slow = 21
var int rsi_length = 14
var int atr_length = 14
var float atr_multiplier_sl = 2.0 // ATR multiplier for SL
var float tp_ratio = 3.0 // TP to SL ratio = 3:1
var float trail_trigger_ratio = 0.5 // Trailing stop triggers at 50% of TP
// --- Indicators ---
ema9 = ta.ema(close, ema_fast)
ema21 = ta.ema(close, ema_slow)
rsi = ta.rsi(close, rsi_length)
atr = ta.atr(atr_length)
// --- Trend Filter ---
bull_trend = ta.crossover(ema9, ema21) or (ema9 > ema21)
bear_trend = ta.crossunder(ema9, ema21) or (ema9 < ema21)
// --- Entry Conditions ---
long_entry = bull_trend and rsi > 50 and close > high[1]
short_entry = bear_trend and rsi < 50 and close < low[1]
// --- Position Size Calculation ---
equity = strategy.equity
stop_loss_distance = atr * atr_multiplier_sl
risk_amount = equity * risk_per_trade
position_size = risk_amount / stop_loss_distance
// --- SL and TP Levels ---
long_sl = close - stop_loss_distance
long_tp = close + stop_loss_distance * tp_ratio
short_sl = close + stop_loss_distance
short_tp = close - stop_loss_distance * tp_ratio
// --- Trailing Stop (activated after 50% of TP) ---
trail_points = atr * atr_multiplier_sl * tp_ratio * trail_trigger_ratio
trail_offset = atr * atr_multiplier_sl
// --- Entries ---
if long_entry
strategy.entry("Long", strategy.long, qty=position_size)
strategy.exit("Long Exit", "Long", stop=long_sl, limit=long_tp, trail_points=trail_points, trail_offset=trail_offset)
if short_entry
strategy.entry("Short", strategy.short, qty=position_size)
strategy.exit("Short Exit", "Short", stop=short_sl, limit=short_tp, trail_points=trail_points, trail_offset=trail_offset)
// --- Alert Conditions ---
var bool long_opened = false
var bool short_opened = false
// Track position opening
long_open_alert = long_entry and not long_opened
short_open_alert = short_entry and not short_opened
// Track position closing
long_close_alert = long_opened and strategy.position_size == 0
short_close_alert = short_opened and strategy.position_size == 0
// Update position states
if long_entry
long_opened := true
if short_entry
short_opened := true
if strategy.position_size == 0
long_opened := false
short_opened := false
// --- Alerts ---
alertcondition(long_open_alert, title="Open Long", message='{"action":"buy","symbol":"{{ticker}}","price":{{close}}}')
alertcondition(long_close_alert, title="Close Long", message='{"action":"close_long","symbol":"{{ticker}}","price":{{close}}}')
alertcondition(short_open_alert, title="Open Short", message='{"action":"sell","symbol":"{{ticker}}","price":{{close}}}')
alertcondition(short_close_alert, title="Close Short", message='{"action":"close_short","symbol":"{{ticker}}","price":{{close}}}')
// --- Visualization ---
plot(ema9, color=color.blue, title="EMA9")
plot(ema21, color=color.red, title="EMA21")
plotshape(long_open_alert, title="Long Entry", location=location.belowbar, color=color.green, style=shape.triangleup, size=size.small)
plotshape(short_open_alert, title="Short Entry", location=location.abovebar, color=color.red, style=shape.triangledown, size=size.small)