82 lines
2.6 KiB
Plaintext
82 lines
2.6 KiB
Plaintext
// 自定义MACD柱子面积计算指标
|
|
// 作者: Claude AI
|
|
// 版本: 1.0
|
|
|
|
//@version=5
|
|
indicator("自定义MACD柱子面积", shorttitle="自定义MACD面积", overlay=false)
|
|
|
|
// MACD参数
|
|
fast_length = input.int(12, "快线长度", minval=1)
|
|
slow_length = input.int(26, "慢线长度", minval=1)
|
|
signal_length = input.int(9, "信号线长度", minval=1)
|
|
|
|
// 面积计算参数
|
|
reset_on_crossover = input.bool(true, "交叉时重置面积")
|
|
use_cumulative = input.bool(false, "使用累计总面积")
|
|
|
|
// 计算MACD
|
|
[macdLine, signalLine, histLine] = ta.macd(close, fast_length, slow_length, signal_length)
|
|
|
|
// 初始化面积变量
|
|
var float upArea = 0.0
|
|
var float downArea = 0.0
|
|
var float totalUpArea = 0.0
|
|
var float totalDownArea = 0.0
|
|
|
|
// 检测柱子正负性并累加面积
|
|
if histLine > 0
|
|
if use_cumulative
|
|
totalUpArea := totalUpArea + histLine
|
|
upArea := totalUpArea
|
|
else
|
|
upArea := upArea + histLine
|
|
|
|
// 当从负值转为正值时,根据设置决定是否重置下跌面积
|
|
if histLine[1] < 0 and reset_on_crossover
|
|
downArea := 0.0
|
|
else if histLine < 0
|
|
if use_cumulative
|
|
totalDownArea := totalDownArea + math.abs(histLine)
|
|
downArea := totalDownArea
|
|
else
|
|
downArea := downArea + math.abs(histLine)
|
|
|
|
// 当从正值转为负值时,根据设置决定是否重置上涨面积
|
|
if histLine[1] > 0 and reset_on_crossover
|
|
upArea := 0.0
|
|
|
|
// 绘制结果
|
|
upColor = color.new(color.green, 50)
|
|
downColor = color.new(color.red, 50)
|
|
|
|
plot(upArea, "上涨段面积", color=upColor, style=plot.style_area)
|
|
plot(downArea, "下跌段面积", color=downColor, style=plot.style_area)
|
|
plot(histLine, "MACD柱状图", color=histLine >= 0 ? color.green : color.red, style=plot.style_histogram)
|
|
plot(macdLine, "MACD", color=color.blue)
|
|
plot(signalLine, "信号线", color=color.orange)
|
|
|
|
// 添加文字标签
|
|
var label upLabel = na
|
|
var label downLabel = na
|
|
|
|
if barstate.islast
|
|
upLabel := label.new(
|
|
bar_index, upArea,
|
|
text="上涨面积: " + str.tostring(upArea, "#.##"),
|
|
color=color.green,
|
|
style=label.style_label_down,
|
|
textcolor=color.white)
|
|
|
|
downLabel := label.new(
|
|
bar_index, downArea * -1,
|
|
text="下跌面积: " + str.tostring(downArea, "#.##"),
|
|
color=color.red,
|
|
style=label.style_label_up,
|
|
textcolor=color.white)
|
|
|
|
label.delete(upLabel[1])
|
|
label.delete(downLabel[1])
|
|
|
|
// 绘制上涨/下跌面积比率
|
|
ratio = upArea / (downArea + 0.0001)
|
|
plot(ratio, "上涨/下跌比率", color=color.purple, linewidth=2) |