双重MACD价格行为突破追踪策略

MACD ATR
创建日期: 2024-11-25 11:15:50 最后修改: 2024-11-25 11:15:50
复制: 0 点击次数: 91
1
关注
1106
关注者

双重MACD价格行为突破追踪策略

概述

这是一个结合了双重MACD指标和价格行为分析的交易策略。策略通过观察15分钟周期上的双重MACD直方图颜色变化来确定市场趋势,同时在5分钟周期上寻找强势蜡烛形态,并在1分钟周期上确认突破信号。策略采用基于ATR的动态止损和追踪止盈机制,有效管理风险的同时最大化盈利空间。

策略原理

策略使用两组不同参数的MACD指标(34/144/9和100/200/50)来确认市场趋势。当两个MACD直方图都显示相同的颜色趋势时,系统会在5分钟图上寻找强势蜡烛形态,这种形态的特征是实体大于影线的1.5倍。一旦找到强势蜡烛,系统会在1分钟图上监控是否出现突破。在上升趋势中突破高点或下降趋势中突破低点时,系统会开仓。止损基于ATR指标设置,同时使用ATR的1.5倍作为动态追踪止盈。

策略优势

  1. 多周期分析:结合15分钟、5分钟和1分钟三个时间周期,提高信号可靠性
  2. 趋势确认:使用双重MACD交叉验证,降低假信号
  3. 价格行为分析:通过强势蜡烛形态识别关键价格水平
  4. 动态风险管理:基于ATR的自适应止损和追踪止盈机制
  5. 信号过滤:严格的入场条件减少误操作
  6. 自动化程度高:全程自动化交易,减少人为干预

策略风险

  1. 趋势反转风险:在剧烈波动市场中可能出现虚假突破
  2. 滑点风险:1分钟周期的高频交易可能面临滑点影响
  3. 过度交易风险:频繁的信号可能导致过度交易
  4. 市场环境依赖:在震荡市场中表现可能不佳 缓解措施:
  • 增加趋势过滤器
  • 设置最小波动阈值
  • 添加交易次数限制
  • 引入市场环境识别机制

策略优化方向

  1. MACD参数优化:可根据不同市场特征调整MACD参数
  2. 止损优化:考虑添加基于波动率的动态止损
  3. 交易时间过滤:加入交易时间窗口限制
  4. 位置管理:实现分批建仓和出场机制
  5. 市场环境过滤:添加趋势强度指标
  6. 回撤控制:引入基于权益曲线的风险控制机制

总结

这是一个综合运用技术分析和风险管理的策略系统。通过多周期分析和严格的信号过滤确保交易质量,同时利用动态止损和追踪止盈机制有效管理风险。策略具有较强的适应性,但仍需要根据市场环境进行持续优化。在实盘应用时,建议先进行充分的回测和参数优化,并结合市场特征进行针对性调整。

策略源码
/*backtest
start: 2024-01-01 00:00:00
end: 2024-11-24 00:00:00
period: 1h
basePeriod: 1h
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT"}]
*/

// @version=5
strategy("Price Action + Double MACD Strategy with ATR Trailing", overlay=true)

// Inputs for MACD
fastLength1 = input.int(34, title="First MACD Fast Length")
slowLength1 = input.int(144, title="First MACD Slow Length")
signalLength1 = input.int(9, title="First MACD Signal Length")

fastLength2 = input.int(100, title="Second MACD Fast Length")
slowLength2 = input.int(200, title="Second MACD Slow Length")
signalLength2 = input.int(50, title="Second MACD Signal Length")

// Input for ATR Trailing
atrMultiplier = input.float(1.5, title="ATR Multiplier for Trailing")

// Inputs for Stop Loss
atrStopMultiplier = input.float(1.0, title="ATR Multiplier for Stop Loss")

// MACD Calculations
[macdLine1, signalLine1, macdHist1] = ta.macd(close, fastLength1, slowLength1, signalLength1)
[macdLine2, signalLine2, macdHist2] = ta.macd(close, fastLength2, slowLength2, signalLength2)

// Get 15M MACD histogram colors
macdHist1Color = request.security(syminfo.tickerid, "15", (macdHist1 >= 0 ? (macdHist1[1] < macdHist1 ? #26A69A : #B2DFDB) : (macdHist1[1] < macdHist1 ? #FFCDD2 : #FF5252)))
macdHist2Color = request.security(syminfo.tickerid, "15", (macdHist2 >= 0 ? (macdHist2[1] < macdHist2 ? #26A69A : #B2DFDB) : (macdHist2[1] < macdHist2 ? #FFCDD2 : #FF5252)))

// Check MACD color conditions
isMacdUptrend = macdHist1Color == #26A69A and macdHist2Color == #26A69A
isMacdDowntrend = macdHist1Color == #FF5252 and macdHist2Color == #FF5252

// Function to detect strong 5M candles
isStrongCandle(open, close, high, low) =>
    body = math.abs(close - open)
    tail = math.abs(high - low) - body
    body > tail * 1.5  // Ensure body is larger than the tail

// Variables to track state
var float fiveMinuteHigh = na
var float fiveMinuteLow = na
var bool tradeExecuted = false
var bool breakoutDetected = false
var float entryPrice = na
var float stopLossPrice = na
var float longTakeProfit = na
var float shortTakeProfit = na

// Check for new 15M candle and reset flags
if ta.change(time("15"))
    tradeExecuted := false      // Reset trade execution flag
    breakoutDetected := false  // Reset breakout detection
    if isStrongCandle(open[1], close[1], high[1], low[1])
        fiveMinuteHigh := high[1]
        fiveMinuteLow := low[1]
    else
        fiveMinuteHigh := na
        fiveMinuteLow := na

// Get 1-minute close prices
close1m = request.security(syminfo.tickerid, "5", close)

// Ensure valid breakout direction and avoid double breakouts
if not na(fiveMinuteHigh) and not breakoutDetected
    for i = 1 to 3
        if close1m[i] > fiveMinuteHigh and not tradeExecuted  // 1M breakout check with close
            breakoutDetected := true
            if isMacdUptrend 
                // Open Long trade
                entryPrice := close
                stopLossPrice := close - (atrStopMultiplier * ta.atr(14))  // ATR-based stop loss
                longTakeProfit := close + (atrMultiplier * ta.atr(14)) // Initialize take profit

                strategy.entry("Long", strategy.long)
                tradeExecuted := true
            break // Exit the loop after detecting a breakout

        else if close1m[i] < fiveMinuteLow and not tradeExecuted  // 1M breakout check with close
            breakoutDetected := true
            if isMacdDowntrend
                // Open Short trade
                entryPrice := close
                stopLossPrice := close + (atrStopMultiplier * ta.atr(14))  // ATR-based stop loss
                shortTakeProfit := close - (atrMultiplier * ta.atr(14)) // Initialize take profit

                strategy.entry("Short", strategy.short)
                tradeExecuted := true
            break // Exit the loop after detecting a breakout

// Update trailing take-profit dynamically
if tradeExecuted and strategy.position_size > 0  // Long trade
    longTakeProfit := math.max(longTakeProfit, close + (atrMultiplier * ta.atr(14)))
    strategy.exit("Long TP/SL", "Long", stop=stopLossPrice, limit=longTakeProfit)

else if tradeExecuted and strategy.position_size < 0  // Short trade
    shortTakeProfit := math.min(shortTakeProfit, close - (atrMultiplier * ta.atr(14)))
    strategy.exit("Short TP/SL", "Short", stop=stopLossPrice, limit=shortTakeProfit)

// Reset trade state when position is closed
if strategy.position_size == 0
    tradeExecuted := false
    entryPrice := na
    longTakeProfit := na
    shortTakeProfit := na
相关推荐
更多内容