该策略是一个结合了多个技术指标的趋势跟踪交易系统。它主要利用抛物线转向指标(SAR)、移动平均线(SMA)和方向动量指标(DMI)来确定市场趋势和入场时机,并通过百分比止盈和MACD背离来优化出场。策略设计的核心思想是在确认强势趋势后入场,并在达到预设盈利目标或出现趋势转折信号时及时出场。
策略使用了多层过滤机制来确认交易信号: 1. 通过SAR指标的交叉来捕捉初始交易信号 2. 使用50周期SMA判断整体趋势方向 3. 运用DMI指标确认趋势强度和方向 4. 入场条件需同时满足:价格上穿SAR、价格在SMA之上且DMI显示多头趋势 5. 出场采用双重机制:达到3%的目标利润或MACD死叉信号出现 6. ATR指标用于市场波动性参考
该策略通过多重技术指标的协同配合,构建了一个相对完整的趋势跟踪交易系统。其优势在于信号确认的可靠性和风险控制的灵活性。虽然存在一定的滞后性风险,但通过参数优化和加入动态管理机制,策略的整体表现仍具有较好的应用价值。通过持续优化和改进,该策略可以成为一个稳健的交易工具。
/*backtest
start: 2019-12-23 08:00:00
end: 2024-12-09 08:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/
//@version=5
strategy("Swing Trading Strategy with DMI", overlay=true)
// Define parameters
sarStart = input.float(0.02, title="SAR Start")
sarIncrement = input.float(0.02, title="SAR Increment")
sarMax = input.float(0.2, title="SAR Max")
atrLength = input.int(10, title="ATR Length")
macdShort = input.int(12, title="MACD Short Length")
macdLong = input.int(26, title="MACD Long Length")
macdSignal = input.int(9, title="MACD Signal Length")
smaLength = input.int(50, title="SMA Length")
dmiLength = input.int(14, title="DMI Length")
adxSmoothing = input.int(14, title="ADX Smoothing") // Smoothing period for ADX
targetProfitPercentage = input.float(3.0, title="Target Profit Percentage")
// Calculate SAR
sar = ta.sar(sarStart, sarIncrement, sarMax)
// Calculate ATR
atr = ta.atr(atrLength)
// Calculate MACD
[macdLine, macdSignalLine, _] = ta.macd(close, macdShort, macdLong, macdSignal)
// Calculate SMA
sma = ta.sma(close, smaLength)
bullishTrend = close > sma
// Calculate DMI
[plusDI, minusDI, adx] = ta.dmi(dmiLength, adxSmoothing) // Specify ADX smoothing period
// Determine if DMI is bullish
dmiBullish = plusDI > minusDI
// Define buy signal
buySignal = ta.crossover(close, sar) and bullishTrend and dmiBullish
// Track buy price and position state
var float buyPrice = na
var bool inPosition = false
// Enter position
if (buySignal and not inPosition)
buyPrice := close
inPosition := true
strategy.entry("Buy", strategy.long)
// Define target price (3% above the buy price)
targetPrice = na(buyPrice) ? na : buyPrice * (1 + targetProfitPercentage / 100)
// Define MACD sell signal
macdSellSignal = ta.crossunder(macdLine, macdSignalLine)
// Define sell signal
sellSignal = inPosition and (close >= targetPrice or macdSellSignal)
// Exit position
if (sellSignal)
inPosition := false
strategy.exit("Sell", "Buy", limit=targetPrice)
// Plot SAR on the chart
plot(sar, color=color.red, style=plot.style_cross, linewidth=2)
// Plot SMA (optional, for visualizing the trend)
plot(sma, color=color.blue, title="SMA")
// Plot DMI +DI and -DI
plot(plusDI, color=color.green, title="+DI")
plot(minusDI, color=color.red, title="-DI")
// Plot buy signal on the chart
//plotshape(series=buySignal, location=location.belowbar, color=color.green, style=shape.labelup, text="BUY")
// Plot sell signal on the chart
//plotshape(series=sellSignal, location=location.abovebar, color=color.red, style=shape.labeldown, text="SELL")
// Optional: Plot background color for buy and sell signals
bgcolor(buySignal ? color.new(color.green, 90) : na, title="Buy Signal Background")
bgcolor(sellSignal ? color.new(color.red, 90) : na, title="Sell Signal Background")