
Chiến lược này là hệ thống giao dịch theo xu hướng kết hợp Đường trung bình động theo hàm mũ (EMA) và Chỉ số định hướng trung bình (ADX). Chiến lược này xác định hướng giao dịch bằng giao điểm của EMA50 và giá, đồng thời sử dụng chỉ báo ADX để lọc cường độ xu hướng thị trường, đồng thời áp dụng phương pháp dừng lỗ động dựa trên đường K có lợi nhuận liên tục để bảo vệ lợi nhuận. Phương pháp này không chỉ nắm bắt được xu hướng chính của thị trường mà còn kịp thời thoát ra khi xu hướng suy yếu.
Logic cốt lõi của chiến lược này dựa trên các yếu tố chính sau:
Đây là chiến lược theo xu hướng được thiết kế tốt, kết hợp những ưu điểm của EMA và ADX để nắm bắt xu hướng một cách hiệu quả đồng thời kiểm soát rủi ro. Cơ chế dừng lỗ năng động của chiến lược này đặc biệt sáng tạo và có thể tạo ra sự cân bằng tốt giữa bảo vệ lợi nhuận và nắm bắt xu hướng. Mặc dù có một số chỗ cần tối ưu hóa, nhưng khuôn khổ tổng thể đã hoàn chỉnh và logic rõ ràng. Đây là một hệ thống chiến lược đáng để xác minh trong giao dịch thực tế.
/*backtest
start: 2019-12-23 08:00:00
end: 2025-01-04 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=6
strategy("Simple EMA 50 Strategy with ADX Filter", overlay=true)
// Input parameters
emaLength = input.int(50, title="EMA Length")
adxThreshold = input.float(20, title="ADX Threshold", minval=0)
// Calculate EMA and ADX
ema50 = ta.ema(close, emaLength)
adxSmoothing = input.int(20, title="ADX Smoothing")
[diPlus, diMinus, adx] = ta.dmi(20, adxSmoothing)
// Conditions for long and short entries
adxCondition = adx > adxThreshold
longCondition = adxCondition and close > ema50 // Check if candle closes above EMA
shortCondition = adxCondition and close < ema50 // Check if candle closes below EMA
// Exit conditions based on 4 consecutive profitable candles
var float longSL = na
var float shortSL = na
var longCandleCounter = 0
var shortCandleCounter = 0
// Increment counters if positions are open and profitable
if (strategy.position_size > 0 and close > strategy.position_avg_price)
longCandleCounter += 1
if (longCandleCounter >= 4)
longSL := na(longSL) ? close : math.max(longSL, close) // Update SL dynamically
else
longCandleCounter := 0
longSL := na
if (strategy.position_size < 0 and close < strategy.position_avg_price)
shortCandleCounter += 1
if (shortCandleCounter >= 4)
shortSL := na(shortSL) ? close : math.min(shortSL, close) // Update SL dynamically
else
shortCandleCounter := 0
shortSL := na
// Exit based on trailing SL
if (strategy.position_size > 0 and not na(longSL) and close < longSL)
strategy.close("Buy", comment="Candle-based SL")
if (strategy.position_size < 0 and not na(shortSL) and close > shortSL)
strategy.close("Sell", comment="Candle-based SL")
// Entry logic: Check every candle for new positions
if (longCondition)
strategy.entry("Buy", strategy.long)
if (shortCondition)
strategy.entry("Sell", strategy.short)
// Plot EMA and ADX for reference
plot(ema50, color=color.blue, title="EMA 50")
plot(adx, color=color.orange, title="ADX", style=plot.style_stepline, linewidth=1)
plot(longSL, color=color.green, title="Long SL", style=plot.style_cross, linewidth=1)
plot(shortSL, color=color.red, title="Short SL", style=plot.style_cross, linewidth=1)
// Plot signals
plotshape(series=longCondition, location=location.belowbar, color=color.green, style=shape.labelup, title="Buy Signal")
plotshape(series=shortCondition, location=location.abovebar, color=color.red, style=shape.labeldown, title="Sell Signal")