This strategy is a trend following system that combines the Directional Movement Index (DMI) with Average True Range (ATR). The core mechanism uses DI+ and DI- indicators to identify market trend direction and strength, while utilizing ATR for dynamic stop-loss and take-profit adjustments. The introduction of a trend filtering moving average further enhances signal reliability. The strategy design considers market volatility and demonstrates good adaptability.
The strategy operates based on the following core mechanisms:
Oscillation Market Risk - May result in consecutive stops in range-bound markets. Suggestion: Add oscillation indicators for filtering or adjust parameter thresholds.
Slippage Risk - May face significant slippage during high volatility periods. Suggestion: Appropriately widen stop-loss positions to accommodate slippage.
False Breakout Risk - Potential misjudgments at trend turning points. Suggestion: Incorporate volume indicators for signal confirmation.
Parameter Sensitivity - Performance varies significantly with different parameter combinations. Suggestion: Find stable parameter ranges through backtesting.
Signal Optimization - Consider introducing ADX indicator for trend strength evaluation or adding volume confirmation mechanisms.
Position Management - Implement dynamic position sizing based on trend strength for more refined risk control.
Time Structure - Consider multi-timeframe analysis to enhance signal reliability.
Market Adaptability - Develop adaptive parameter adjustment mechanisms based on different instrument characteristics.
This strategy achieves dynamic trend following and risk control by combining directional and volatility indicators. The strategy design emphasizes practicality and operability, demonstrating strong market adaptability. Through parameter optimization and signal improvements, there is room for further enhancement. Investors are advised to thoroughly test and make specific adjustments based on market characteristics before implementation.
/*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=5 strategy("使用 DI+ 和 DI- 的策略 (最終完整修正且含圖表止損止盈線)", overlay=true) // 輸入參數 diLength = input.int(title="DI 長度", defval=14) adxSmoothing = input.int(title="ADX Smoothing", defval=14) trendFilterLength = input.int(title="趨勢過濾均線長度", defval=20) strengthThreshold = input.int(title="趨勢強度門檻值", defval=20) atrLength = input.int(title="ATR 長度", defval=14) atrMultiplierStop = input.float(title="ATR 停損倍數", defval=1.5) atrMultiplierTakeProfit = input.float(title="ATR 止盈倍數", defval=2.5) // 計算 DI+ 和 DI- [diPlus, diMinus, _] = ta.dmi(diLength, adxSmoothing) // 計算趨勢過濾均線 trendFilterMA = ta.sma(close, trendFilterLength) // 判斷趨勢方向和強度 strongUpTrend = diPlus > diMinus + strengthThreshold and close > trendFilterMA strongDownTrend = diMinus > diPlus + strengthThreshold and close < trendFilterMA // 計算 ATR atr = ta.atr(atrLength) // 追蹤止損止盈價格 (使用 var 宣告,只在進場時更新) var float longStopPrice = na var float longTakeProfitPrice = na var float shortStopPrice = na var float shortTakeProfitPrice = na // 進場邏輯 longCondition = strongUpTrend shortCondition = strongDownTrend if (longCondition) strategy.entry("多單", strategy.long) longStopPrice := close - atr * atrMultiplierStop // 進場時計算並更新止損價 longTakeProfitPrice := close + atr * atrMultiplierTakeProfit // 進場時計算並更新止盈價 if (shortCondition) strategy.entry("空單", strategy.short) shortStopPrice := close + atr * atrMultiplierStop // 進場時計算並更新止損價 shortTakeProfitPrice := close - atr * atrMultiplierTakeProfit // 進場時計算並更新止盈價 // 出場邏輯 (使用 time 限制和 ATR) inLongPosition = strategy.position_size > 0 inShortPosition = strategy.position_size < 0 lastEntryTime = strategy.opentrades.entry_bar_index(strategy.opentrades - 1) if (inLongPosition and time > lastEntryTime) strategy.exit("多單出場", "多單", stop=longStopPrice, limit=longTakeProfitPrice) if (inShortPosition and time > lastEntryTime) strategy.exit("空單出場", "空單", stop=shortStopPrice, limit=shortTakeProfitPrice) // 繪製 DI+、DI- 和趨勢過濾均線 plot(diPlus, color=color.green, title="DI+") plot(diMinus, color=color.red, title="DI-") plot(trendFilterMA, color=color.blue, title="趨勢過濾均線") // 繪製止損止盈線 (使用 plot 函數繪製) plot(strategy.position_size > 0 ? longStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=2, title="多單停損") plot(strategy.position_size > 0 ? longTakeProfitPrice : na, color=color.green, style=plot.style_linebr, linewidth=2, title="多單止盈") plot(strategy.position_size < 0 ? shortStopPrice : na, color=color.red, style=plot.style_linebr, linewidth=2, title="空單停損") plot(strategy.position_size < 0 ? shortTakeProfitPrice : na, color=color.green, style=plot.style_linebr, linewidth=2, title="空單止盈")