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

Pine Script报错:'Object has no field histogram',跨周期MACD条件待修复

Pine Script 错误修复:'Object has no field histogram'

错误原因

在Pine Script v5中,ta.macd()函数返回的是元组类型,包含三个元素:MACD线、信号线、柱状图,不存在名为histogram的属性字段,直接调用.histogram会触发字段不存在的错误。

修正方案

  1. 将.histogram替换为元组索引[2](柱状图是ta.macd()返回的第三个元素,索引从0开始)
  2. 移除request.security返回值后的[0],因为该函数已经返回对应时间框架的最新数据,多余索引会导致数据引用异常

修正后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Pardon3000

//@version=5
indicator("Pine Script Example", overlay=true)

// Define the indicators
ema9 = ta.ema(close, 9)
wma70 = ta.wma(close, 70)
ema200 = ta.ema(close, 200)
// 修正MACD柱状图的获取方式,使用索引[2]替代.histogram,移除多余的[0]
macd2h = request.security(syminfo.tickerid, "2h", ta.macd(close, 12, 26, 9)[2])
macd12h = request.security(syminfo.tickerid, "12h", ta.macd(close, 12, 26, 9)[2])

// Plot the indicators
plot(ema9, color=color.blue)
plot(wma70, color=color.red)
plot(ema200, color=color.green)

// Check for crossover condition
crossover_condition = ta.crossover(ema9, wma70) and ema9 > ema200 and macd2h[1] > 0 and macd12h[1] > 0

// Generate an alert if crossover condition is true
alertcondition(crossover_condition, title="Crossover Alert", message="EMA9 crossed over WMA70 and is above EMA200 on current timeframe. MACD histogram is above 0 on 2h and 12h timeframe.")

// Plot an arrow up under the candle if crossover condition is true
plotshape(crossover_condition ? low : na, style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)

关键逻辑验证

修正后的代码会在以下条件同时满足时触发警报和箭头标记:

  • 9周期EMA上穿70周期WMA
  • 9周期EMA处于200周期EMA上方
  • 2小时时间框架的前一根K线MACD柱状图大于0
  • 12小时时间框架的前一根K线MACD柱状图大于0

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 00:10:25