盈利配对相关散度扫描器 v6
发布: (2025年12月8日 GMT+8 19:48)
3 min read
原文: Dev.to
Source: Dev.to
概览
该策略通过 Z‑Score 价差分析、趋势确认、RSI 与 MACD 动量检查、相关性过滤以及基于 ATR 的止损/止盈管理,识别两只相关资产之间的背离机会。它针对正向盈亏(P&L)和实际交易执行进行了优化。
关键特性
- 配对背离检测 – 测量两只资产收益之间的价差,并使用 Z‑Score 进行标准化。
- 趋势对齐 – 使用快慢 EMA 确保交易方向与当前主趋势一致。
- 动量确认 – RSI 与 MACD 过滤器确认看涨或看跌动量。
- 相关性过滤 – 要求配对之间的相关性达到最低阈值(默认 > 0.5),以降低误报。
- 风险管理 – 基于 ATR 的止损和止盈水平,奖励/风险比大于 1。
- 退出条件 – 当 Z‑Score 重新进入紧凑的“归一化”区间时平仓。
工作原理
计算收益
ret1 = ta.roc(s1, 1)
ret2 = ta.roc(s2, 1)
Z‑Score 价差
spread = ret1 - ret2
spreadMA = ta.sma(spread, zLen)
spreadSD = ta.stdev(spread, zLen)
zScore = (spread - spreadMA) / spreadSD
趋势过滤器
emaFast = ta.ema(s1, fastEMA)
emaSlow = ta.ema(s1, slowEMA)
trendLong = emaFast > emaSlow
trendShort = emaFast 50
rsiShort = rsiVal signalLine
macdShort = macdLine 0.5
交易执行
longCond = zScore entryZ and trendShort and rsiShort and macdShort and corrFilter
if (longCond)
strategy.entry("LongSpread", strategy.long)
if (shortCond)
strategy.entry("ShortSpread", strategy.short)
退出逻辑
exitLong = math.abs(zScore) emaSlow
trendShort = emaFast 50
rsiShort = rsiVal signalLine
macdShort = macdLine 0.5
// ATR FOR SL/TP
atrVal = ta.atr(atrLen)
sl = atrVal * atrMult
tp = atrVal * atrMult * 2 // reward > risk
// ENTRY CONDITIONS
longCond = zScore entryZ and trendShort and rsiShort and macdShort and corrFilter
exitLong = math.abs(zScore) < exitZ
exitShort = math.abs(zScore) < exitZ
// EXECUTION
if (longCond)
strategy.entry("LongSpread", strategy.long)
if (shortCond)
strategy.entry("ShortSpread", strategy.short)
if (exitLong)
strategy.close("LongSpread")
if (exitShort)
strategy.close("ShortSpread")
// ATR‑BASED EXIT
strategy.exit("Exit Long", "LongSpread", stop=close - sl, limit=close + tp)
strategy.exit("Exit Short", "ShortSpread", stop=close + sl, limit=close - tp)
// PLOTTING
plot(zScore, "Z-Score", color=color.new(color.blue, 0))
hline( entryZ, "Upper Entry", color=color.red)
hline(-entryZ, "Lower Entry", color=color.green)
hline( exitZ, "Exit Zone", color=color.gray)
hline(-exitZ, "Exit Zone", color=color.gray)
plot(emaFast, "Fast EMA", color=color.new(color.purple, 0))
plot(emaSlow, "Slow EMA", color=color.new(color.orange, 0))
plot(corrVal, "Correlation", color=color.new(color.teal, 40))