该策略是一个结合了指数移动平均线(EMA)和平均趋向指数(ADX)的趋势跟踪交易系统。策略通过EMA50与价格的交叉来确定交易方向,并使用ADX指标过滤市场趋势强度,同时采用基于连续获利K线的动态止损方法来保护利润。这种方法既能捕捉市场的主要趋势,又能在趋势减弱时及时退出。
策略的核心逻辑基于以下几个关键要素: 1. 使用50周期指数移动平均线(EMA50)作为趋势方向的判断依据 2. 通过ADX指标(默认参数为20)过滤市场趋势强度,只在趋势明显时入场 3. 入场条件: - 多头:价格收盘价上穿EMA50且ADX大于阈值 - 空头:价格收盘价下穿EMA50且ADX大于阈值 4. 独特的止损机制: - 统计连续获利K线数量 - 当出现4根连续获利K线时激活动态追踪止损 - 止损价位会随着新高/新低动态调整
这是一个设计合理的趋势跟踪策略,通过结合EMA和ADX的优势,既能有效捕捉趋势,又能控制风险。策略的动态止损机制特别创新,能够很好地平衡盈利保护和趋势捕捉。虽然存在一些优化空间,但整体框架完整,逻辑清晰,是一个值得在实盘中验证的策略系统。
/*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")