You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将该4色MACD指标代码转换为TradingView Pine Script 3版本?

4色MACD指标转换为Pine Script 3版本的实现代码

以下是转换后的Pine Script 3版本代码,同时做了小幅度的效率优化:

//@version=3
indicator(shorttitle = "MACD 4C", title = "4 colour MACD")
fastMA = input(title="Fast moving average", defval = 12, minval = 7)
slowMA = input(title="Slow moving average", defval = 26, minval = 7)

currMacd = macd(close, fastMA, slowMA, 9)[0]
prevMacd = currMacd[1]

plotColor = currMacd > 0 
    ? currMacd > prevMacd ? lime : green 
    : currMacd < prevMacd ? maroon : red

plot(currMacd, style = histogram, color = plotColor, linewidth = 3)
plot(0, title = "Zero line", linewidth = 1, color = gray)

关键修改说明

  • 替换study()为indicator():Pine Script 3及以后版本统一使用indicator()声明指标,替代旧版的study()
  • 简化input参数:v3中input会自动根据默认值识别数据类型,无需手动指定type = integer
  • 优化MACD值获取:原代码分别调用两次macd()获取当前和前一根K线的值,新版直接通过currMacd[1]获取前值,减少重复计算,提升运行效率
  • 保留原4色逻辑:完全沿用原代码的颜色判断规则,保证指标的视觉表现和原版本一致

内容的提问来源于stack exchange,提问作者byGEREN

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:12:46