106 lines
3.3 KiB
Plaintext
106 lines
3.3 KiB
Plaintext
// MACD与价格背驰检测指标
|
|
// 作者: Claude AI
|
|
// 版本: 1.0
|
|
|
|
//@version=5
|
|
indicator("MACD背驰检测", shorttitle="MACD背驰", overlay=true)
|
|
|
|
// MACD参数
|
|
fast_length = input.int(12, "MACD快线长度", minval=1)
|
|
slow_length = input.int(26, "MACD慢线长度", minval=1)
|
|
signal_length = input.int(9, "MACD信号线长度", minval=1)
|
|
|
|
// 背驰检测参数
|
|
lookback = input.int(10, "回溯检测周期", minval=5)
|
|
threshold = input.float(0.5, "背驰阈值", minval=0.1, step=0.1)
|
|
|
|
// 计算MACD
|
|
[macdLine, signalLine, histLine] = ta.macd(close, fast_length, slow_length, signal_length)
|
|
|
|
// 寻找价格的高点和低点
|
|
price_high = ta.highest(high, lookback)
|
|
price_low = ta.lowest(low, lookback)
|
|
|
|
// 判断当前柱是否是局部高点或低点
|
|
is_price_high = high == price_high and high > high[1] and high > high[2]
|
|
is_price_low = low == price_low and low < low[1] and low < low[2]
|
|
|
|
// 保存价格高点和低点及其对应的MACD值
|
|
var float last_price_high = na
|
|
var float last_price_low = na
|
|
var float last_macd_at_price_high = na
|
|
var float last_macd_at_price_low = na
|
|
|
|
// 寻找MACD的高点和低点
|
|
macd_high = ta.highest(macdLine, lookback)
|
|
macd_low = ta.lowest(macdLine, lookback)
|
|
|
|
// ----- 背驰检测逻辑 -----
|
|
|
|
// 顶背驰:价格创新高,但MACD没有创新高
|
|
bearish_divergence = false
|
|
if is_price_high
|
|
// 保存当前高点及对应的MACD值
|
|
if not na(last_price_high) and high > last_price_high and macdLine < last_macd_at_price_high
|
|
bearish_divergence := true
|
|
|
|
last_price_high := high
|
|
last_macd_at_price_high := macdLine
|
|
|
|
// 底背驰:价格创新低,但MACD没有创新低
|
|
bullish_divergence = false
|
|
if is_price_low
|
|
// 保存当前低点及对应的MACD值
|
|
if not na(last_price_low) and low < last_price_low and macdLine > last_macd_at_price_low
|
|
bullish_divergence := true
|
|
|
|
last_price_low := low
|
|
last_macd_at_price_low := macdLine
|
|
|
|
// ----- 标注背驰 -----
|
|
|
|
// 顶背驰标注
|
|
plotshape(
|
|
bearish_divergence,
|
|
title="顶背驰",
|
|
location=location.abovebar,
|
|
color=color.red,
|
|
style=shape.triangledown,
|
|
size=size.normal,
|
|
text="顶背驰")
|
|
|
|
// 底背驰标注
|
|
plotshape(
|
|
bullish_divergence,
|
|
title="底背驰",
|
|
location=location.belowbar,
|
|
color=color.green,
|
|
style=shape.triangleup,
|
|
size=size.normal,
|
|
text="底背驰")
|
|
|
|
// ----- 辅助指标 -----
|
|
|
|
// 在单独窗口绘制MACD
|
|
plot(macdLine, "MACD", color=color.blue, display=display.none)
|
|
plot(signalLine, "信号线", color=color.orange, display=display.none)
|
|
plot(histLine, "柱状图", color=histLine >= 0 ? color.green : color.red, style=plot.style_histogram, display=display.none)
|
|
|
|
// 显示背驰信息
|
|
var label divergenceLabel = na
|
|
if barstate.islast
|
|
label.delete(divergenceLabel)
|
|
if bearish_divergence
|
|
divergenceLabel := label.new(
|
|
bar_index, high,
|
|
text="顶背驰警告!\n价格创新高但MACD未跟进",
|
|
color=color.red,
|
|
style=label.style_label_down,
|
|
textcolor=color.white)
|
|
else if bullish_divergence
|
|
divergenceLabel := label.new(
|
|
bar_index, low,
|
|
text="底背驰警告!\n价格创新低但MACD未跟进",
|
|
color=color.green,
|
|
style=label.style_label_up,
|
|
textcolor=color.white) |