
This strategy is a quantitative trading system based on multiple Exponential Moving Average (EMA) crossovers. It constructs a complete trend-following trading framework using three EMAs: 9-day, 21-day, and 200-day. The strategy identifies market trends and executes trades by analyzing the crossovers between fast and slow EMAs and their positions relative to the long-term EMA.
The core logic revolves around triple EMA crossovers to capture market trends. Specifically: 1. Uses 9-day EMA as the fast line to reflect short-term price movements 2. Uses 21-day EMA as the medium-term line to filter short-term noise 3. Uses 200-day EMA as the long-term line to determine major trend direction The system generates long signals when the fast EMA crosses above the slow EMA while both are above the 200-day EMA, and short signals when the fast EMA crosses below the slow EMA while both are below the 200-day EMA. This design captures trend reversal points while avoiding frequent trades in ranging markets.
This is a well-designed trend-following strategy with clear logic. Through the coordination of multiple EMAs, it effectively captures market trends while maintaining good risk control. The strategy has significant optimization potential, and its stability and profitability can be further enhanced through continuous improvements.
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-25 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("EMA Cross with both MinhTuan", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// Tham số EMA
fastLength = input.int(9, title="Fast EMA Length", minval=1)
slowLength = input.int(21, title="Slow EMA Length", minval=1)
filterLength = input.int(200, title="EMA Filter Length", minval=1)
// Tùy chọn chế độ giao dịch
tradeMode = input.string("Both", options=["Long", "Short", "Both"], title="Trade Mode")
// Tính toán EMA
fastEMA = ta.ema(close, fastLength)
slowEMA = ta.ema(close, slowLength)
filterEMA = ta.ema(close, filterLength)
// Điều kiện vào lệnh Long: EMA nhanh cắt lên EMA chậm và cả hai nằm trên EMA 200
longCondition = ta.crossover(fastEMA, slowEMA) and fastEMA > filterEMA and slowEMA > filterEMA
// Điều kiện vào lệnh Short: EMA nhanh cắt xuống EMA chậm và cả hai nằm dưới EMA 200
shortCondition = ta.crossunder(fastEMA, slowEMA) and fastEMA < filterEMA and slowEMA < filterEMA
// Điều kiện thoát lệnh: EMA nhanh cắt ngược lại EMA chậm
closeLongCondition = ta.crossunder(fastEMA, slowEMA) // Thoát lệnh Long
closeShortCondition = ta.crossover(fastEMA, slowEMA) // Thoát lệnh Short
// Thực hiện lệnh Long
if (longCondition and (tradeMode == "Long" or tradeMode == "Both"))
strategy.entry("EMA_Cross_Long", strategy.long)
label.new(x=bar_index, y=low, text="Long", color=color.green, textcolor=color.white, size=size.small)
// Thực hiện lệnh Short
if (shortCondition and (tradeMode == "Short" or tradeMode == "Both"))
strategy.entry("EMA_Cross_Short", strategy.short)
label.new(x=bar_index, y=high, text="Short", color=color.red, textcolor=color.white, size=size.small)
// Thoát lệnh Long
if (closeLongCondition)
strategy.close("EMA_Cross_Long")
label.new(x=bar_index, y=high, text="Close Long", color=color.orange, textcolor=color.white, size=size.small)
// Thoát lệnh Short
if (closeShortCondition)
strategy.close("EMA_Cross_Short")
label.new(x=bar_index, y=low, text="Close Short", color=color.blue, textcolor=color.white, size=size.small)
// Vẽ đường EMA nhanh, EMA chậm, và EMA 200
plot(fastEMA, title="Fast EMA", color=color.blue, linewidth=2)
plot(slowEMA, title="Slow EMA", color=color.orange, linewidth=2)
plot(filterEMA, title="Filter EMA (200)", color=color.red, linewidth=2)
// Hiển thị nền khi đang giữ lệnh
bgcolor(strategy.position_size > 0 ? color.new(color.green, 90) : strategy.position_size < 0 ? color.new(color.red, 90) : na)