
Chiến lược này là một hệ thống giao dịch định lượng kết hợp các chỉ số di chuyển trung bình (EMA) và xu hướng di chuyển trung bình (MACD). Bằng cách tích hợp các tín hiệu chéo của EMA ngắn hạn và dài hạn, và xác nhận động lực MACD, nó cung cấp cho các nhà giao dịch một giải pháp theo dõi xu hướng toàn diện. Chiến lược này cũng bao gồm các cơ chế dừng và dừng động, kiểm soát rủi ro một cách hiệu quả trong khi theo đuổi tối đa hóa lợi nhuận.
Lập luận cốt lõi của chiến lược dựa trên sự phối hợp của hai chỉ số kỹ thuật. Đầu tiên, sử dụng EMA 12 chu kỳ và 26 chu kỳ để xác định xu hướng thị trường, tạo ra tín hiệu làm nhiều khi vượt qua EMA dài hạn trên EMA ngắn hạn và tạo ra tín hiệu làm trống khi đi xuống. Tiếp theo, sử dụng chỉ số MACD (cài đặt 12 , 26 , 9) để xác nhận động lực xu hướng, yêu cầu mối quan hệ vị trí của đường MACD với đường tín hiệu hỗ trợ tín hiệu giao dịch do EMA tạo ra.
Đây là một chiến lược theo dõi xu hướng được thiết kế hợp lý, logic rõ ràng. Bằng cách kết hợp các lợi thế của EMA và MACD, trong khi duy trì chiến lược đơn giản và dễ hiểu, tạo ra tín hiệu giao dịch đáng tin cậy hơn.
/*backtest
start: 2025-01-21 00:00:00
end: 2025-02-03 15:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"ETH_USDT"}]
*/
//@version=5
strategy("EMA + MACD Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=10)
// === Inputs ===
shortEmaLength = input.int(12, title="Short EMA Period", minval=1)
longEmaLength = input.int(26, title="Long EMA Period", minval=1)
macdFastLength = input.int(12, title="MACD Fast EMA Period", minval=1)
macdSlowLength = input.int(26, title="MACD Slow EMA Period", minval=1)
macdSignalLength = input.int(9, title="MACD Signal Period", minval=1)
stopLossPerc = input.float(2.0, title="Stop-Loss (%)", minval=0.1, step=0.1)
takeProfitPerc = input.float(5.0, title="Take-Profit (%)", minval=0.1, step=0.1)
// === Indicator Calculations ===
// Exponential Moving Averages (EMA)
shortEMA = ta.ema(close, shortEmaLength)
longEMA = ta.ema(close, longEmaLength)
// MACD
[macdLine, signalLine, _] = ta.macd(close, macdFastLength, macdSlowLength, macdSignalLength)
// === Entry Conditions ===
// Buy signal: Short EMA crosses above Long EMA and MACD > Signal Line
longCondition = ta.crossover(shortEMA, longEMA) and (macdLine > signalLine)
// Sell signal: Short EMA crosses below Long EMA and MACD < Signal Line
shortCondition = ta.crossunder(shortEMA, longEMA) and (macdLine < signalLine)
// === Entry Signals with Stop-Loss and Take-Profit ===
if (longCondition)
strategy.entry("Long", strategy.long)
// Calculate Stop-Loss and Take-Profit
stopPrice = close * (1 - stopLossPerc / 100)
takePrice = close * (1 + takeProfitPerc / 100)
strategy.exit("Long Exit", from_entry="Long", stop=stopPrice, limit=takePrice)
if (shortCondition)
strategy.entry("Short", strategy.short)
// Calculate Stop-Loss and Take-Profit
stopPrice = close * (1 + stopLossPerc / 100)
takePrice = close * (1 - takeProfitPerc / 100)
strategy.exit("Short Exit", from_entry="Short", stop=stopPrice, limit=takePrice)
// === Exit Conditions ===
// Alternative exit conditions based on crossovers
exitLongCondition = ta.crossunder(shortEMA, longEMA) or (macdLine < signalLine)
exitShortCondition = ta.crossover(shortEMA, longEMA) or (macdLine > signalLine)
if (exitLongCondition)
strategy.close("Long")
if (exitShortCondition)
strategy.close("Short")
// === Indicator Plotting ===
// EMA
plot(shortEMA, color=color.blue, title="Short EMA")
plot(longEMA, color=color.red, title="Long EMA")
// MACD Indicator in separate window
hline(0, "Zero Line", color=color.gray, linestyle=hline.style_dotted)
plot(macdLine - signalLine, color=(macdLine - signalLine) >= 0 ? color.green : color.red, title="MACD Histogram", style=plot.style_histogram)
plot(macdLine, color=color.blue, title="MACD Line")
plot(signalLine, color=color.orange, title="Signal Line")
// === Signal Visualization ===
// Markers for Long and Short entries
plotshape(series=longCondition, title="Long Entry", location=location.belowbar, color=color.green, style=shape.labelup, text="Long")
plotshape(series=shortCondition, title="Short Entry", location=location.abovebar, color=color.red, style=shape.labeldown, text="Short")
// Markers for Long and Short exits
plotshape(series=exitLongCondition, title="Long Exit", location=location.abovebar, color=color.red, style=shape.labeldown, text="Exit Long")
plotshape(series=exitShortCondition, title="Short Exit", location=location.belowbar, color=color.green, style=shape.labelup, text="Exit Short")