208 lines
7.0 KiB
Plaintext
208 lines
7.0 KiB
Plaintext
// 高级MACD背驰检测指标
|
|
// 作者: Claude AI
|
|
// 版本: 1.0
|
|
|
|
//@version=5
|
|
indicator("高级MACD背驰检测", shorttitle="高级MACD背驰", overlay=true)
|
|
|
|
// === 输入参数 ===
|
|
|
|
// MACD参数
|
|
fast_length = input.int(12, "MACD快线长度", minval=1, group="MACD设置")
|
|
slow_length = input.int(26, "MACD慢线长度", minval=1, group="MACD设置")
|
|
signal_length = input.int(9, "MACD信号线长度", minval=1, group="MACD设置")
|
|
src = input.source(close, "价格数据源", group="MACD设置")
|
|
|
|
// 背驰检测参数
|
|
lookback = input.int(10, "回溯检测周期", minval=5, group="背驰设置")
|
|
pivot_confirmation = input.int(3, "高低点确认柱数", minval=1, maxval=5, group="背驰设置")
|
|
regular_div = input.bool(true, "检测常规背驰", group="背驰类型")
|
|
hidden_div = input.bool(true, "检测隐形背驰", group="背驰类型")
|
|
|
|
// 显示设置
|
|
show_labels = input.bool(true, "显示文字标签", group="显示设置")
|
|
show_alerts = input.bool(true, "生成提醒", group="显示设置")
|
|
bull_color = input.color(color.green, "多头背驰颜色", group="显示设置")
|
|
bear_color = input.color(color.red, "空头背驰颜色", group="显示设置")
|
|
|
|
// === 计算指标 ===
|
|
|
|
// 计算MACD
|
|
[macdLine, signalLine, histLine] = ta.macd(src, fast_length, slow_length, signal_length)
|
|
|
|
// === 寻找峰谷 ===
|
|
|
|
// 函数:检测高点
|
|
isPivotHigh(len) =>
|
|
high[len] > high[len-1] and high[len] > high[len+1] and
|
|
high[len] > high[len-2] and high[len] > high[len+2]
|
|
|
|
// 函数:检测低点
|
|
isPivotLow(len) =>
|
|
low[len] < low[len-1] and low[len] < low[len+1] and
|
|
low[len] < low[len-2] and low[len] < low[len+2]
|
|
|
|
// 寻找价格和MACD的峰和谷
|
|
priceHighs = array.new_float(0)
|
|
priceLows = array.new_float(0)
|
|
macdHighs = array.new_float(0)
|
|
macdLows = array.new_float(0)
|
|
priceHighsPos = array.new_int(0)
|
|
priceLowsPos = array.new_int(0)
|
|
macdHighsPos = array.new_int(0)
|
|
macdLowsPos = array.new_int(0)
|
|
|
|
// 检测最近的高点和低点
|
|
for i = pivot_confirmation to lookback
|
|
if isPivotHigh(i)
|
|
array.push(priceHighs, high[i])
|
|
array.push(priceHighsPos, bar_index[i])
|
|
array.push(macdHighs, macdLine[i])
|
|
array.push(macdHighsPos, bar_index[i])
|
|
break
|
|
|
|
for i = pivot_confirmation to lookback
|
|
if isPivotLow(i)
|
|
array.push(priceLows, low[i])
|
|
array.push(priceLowsPos, bar_index[i])
|
|
array.push(macdLows, macdLine[i])
|
|
array.push(macdLowsPos, bar_index[i])
|
|
break
|
|
|
|
// === 背驰检测 ===
|
|
|
|
// 常规顶背驰:价格创新高,但MACD没有创新高
|
|
bearish_regular_divergence = false
|
|
if regular_div and array.size(priceHighs) >= 2 and array.size(macdHighs) >= 2
|
|
price_high_current = array.get(priceHighs, 0)
|
|
price_high_prev = array.get(priceHighs, 1)
|
|
macd_high_current = array.get(macdHighs, 0)
|
|
macd_high_prev = array.get(macdHighs, 1)
|
|
|
|
if price_high_current > price_high_prev and macd_high_current < macd_high_prev
|
|
bearish_regular_divergence := true
|
|
|
|
// 常规底背驰:价格创新低,但MACD没有创新低
|
|
bullish_regular_divergence = false
|
|
if regular_div and array.size(priceLows) >= 2 and array.size(macdLows) >= 2
|
|
price_low_current = array.get(priceLows, 0)
|
|
price_low_prev = array.get(priceLows, 1)
|
|
macd_low_current = array.get(macdLows, 0)
|
|
macd_low_prev = array.get(macdLows, 1)
|
|
|
|
if price_low_current < price_low_prev and macd_low_current > macd_low_prev
|
|
bullish_regular_divergence := true
|
|
|
|
// 隐形顶背驰:价格未创新高,但MACD创新高(上涨趋势中的调整)
|
|
bearish_hidden_divergence = false
|
|
if hidden_div and array.size(priceHighs) >= 2 and array.size(macdHighs) >= 2
|
|
price_high_current = array.get(priceHighs, 0)
|
|
price_high_prev = array.get(priceHighs, 1)
|
|
macd_high_current = array.get(macdHighs, 0)
|
|
macd_high_prev = array.get(macdHighs, 1)
|
|
|
|
if price_high_current < price_high_prev and macd_high_current > macd_high_prev
|
|
bearish_hidden_divergence := true
|
|
|
|
// 隐形底背驰:价格未创新低,但MACD创新低(下跌趋势中的反弹)
|
|
bullish_hidden_divergence = false
|
|
if hidden_div and array.size(priceLows) >= 2 and array.size(macdLows) >= 2
|
|
price_low_current = array.get(priceLows, 0)
|
|
price_low_prev = array.get(priceLows, 1)
|
|
macd_low_current = array.get(macdLows, 0)
|
|
macd_low_prev = array.get(macdLows, 1)
|
|
|
|
if price_low_current > price_low_prev and macd_low_current < macd_low_prev
|
|
bullish_hidden_divergence := true
|
|
|
|
// === 绘制背驰信号 ===
|
|
|
|
// 常规顶背驰标记
|
|
plotshape(
|
|
bearish_regular_divergence,
|
|
title="常规顶背驰",
|
|
location=location.abovebar,
|
|
color=bear_color,
|
|
style=shape.triangledown,
|
|
size=size.normal,
|
|
text="顶背驰")
|
|
|
|
// 常规底背驰标记
|
|
plotshape(
|
|
bullish_regular_divergence,
|
|
title="常规底背驰",
|
|
location=location.belowbar,
|
|
color=bull_color,
|
|
style=shape.triangleup,
|
|
size=size.normal,
|
|
text="底背驰")
|
|
|
|
// 隐形顶背驰标记
|
|
plotshape(
|
|
bearish_hidden_divergence,
|
|
title="隐形顶背驰",
|
|
location=location.abovebar,
|
|
color=color.new(bear_color, 50),
|
|
style=shape.xcross,
|
|
size=size.normal,
|
|
text="隐顶")
|
|
|
|
// 隐形底背驰标记
|
|
plotshape(
|
|
bullish_hidden_divergence,
|
|
title="隐形底背驰",
|
|
location=location.belowbar,
|
|
color=color.new(bull_color, 50),
|
|
style=shape.xcross,
|
|
size=size.normal,
|
|
text="隐底")
|
|
|
|
// === 显示标签 ===
|
|
|
|
if show_labels and barstate.islast
|
|
if bearish_regular_divergence
|
|
label.new(
|
|
bar_index, high,
|
|
text="常规顶背驰\n价格创新高但MACD未跟进",
|
|
color=bear_color,
|
|
style=label.style_label_down,
|
|
textcolor=color.white)
|
|
|
|
if bullish_regular_divergence
|
|
label.new(
|
|
bar_index, low,
|
|
text="常规底背驰\n价格创新低但MACD未跟进",
|
|
color=bull_color,
|
|
style=label.style_label_up,
|
|
textcolor=color.white)
|
|
|
|
if bearish_hidden_divergence
|
|
label.new(
|
|
bar_index, high,
|
|
text="隐形顶背驰\n价格未创新高但MACD创新高",
|
|
color=color.new(bear_color, 50),
|
|
style=label.style_label_down,
|
|
textcolor=color.white)
|
|
|
|
if bullish_hidden_divergence
|
|
label.new(
|
|
bar_index, low,
|
|
text="隐形底背驰\n价格未创新低但MACD创新低",
|
|
color=color.new(bull_color, 50),
|
|
style=label.style_label_up,
|
|
textcolor=color.white)
|
|
|
|
// === 生成提醒 ===
|
|
|
|
if show_alerts
|
|
if bearish_regular_divergence
|
|
alert("MACD常规顶背驰警告!", alert.freq_once_per_bar_close)
|
|
|
|
if bullish_regular_divergence
|
|
alert("MACD常规底背驰警告!", alert.freq_once_per_bar_close)
|
|
|
|
if bearish_hidden_divergence
|
|
alert("MACD隐形顶背驰警告!", alert.freq_once_per_bar_close)
|
|
|
|
if bullish_hidden_divergence
|
|
alert("MACD隐形底背驰警告!", alert.freq_once_per_bar_close) |