Pine Script报错:输入'indiseBox'存在语法错误,求修复方案
修复Pine Script 5 Heikin Ashi指标的语法错误
错误原因及修复点
- 触发语法错误的核心原因:
box.new()函数的参数里,right=bar_index后面没加逗号,导致代码解析中断,直接抛出Syntax error at input 'indiseBox'。 - 额外问题修复:
- 初始状态下
indiseBox是na,直接调用box.set_right会触发运行时错误,必须加非空判断。 - 原代码的
plotcandle用了原始K线的开高低收,没用到你获取的Heikin Ashi数据,等于白计算HA数据。
- 初始状态下
修复后的完整代码
//@version=5 indicator("heikinashi", overlay = true) bullish_color = input(title = "Bullish Bar color", defval = color.rgb(0, 191, 255, 0)) bearish_color = input(title = "Bearish Bar color", defval = color.rgb(242, 227, 12, 0)) // 获取Heikin Ashi数据 heikinashi_open = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open) heikinashi_high = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high) heikinashi_low = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low) heikinashi_close = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) isInside = heikinashi_high < heikinashi_high[1] and heikinashi_low > heikinashi_low[1] var box indiseBox = na if isInside // 修复:right参数后添加逗号,解决语法解析问题 indiseBox := box.new(left = bar_index-1, top=heikinashi_high[1], right=bar_index, bottom = heikinashi_low[1], bgcolor = color.new(color.silver,85) , border_color = color.new(color.silver,85)) else // 修复:增加非空判断,避免na调用函数报错 if not na(indiseBox) box.set_right(indiseBox, bar_index) // 修复:使用Heikin Ashi数据绘制蜡烛,正确展示HA形态 plotcandle(heikinashi_open, heikinashi_high, heikinashi_low, heikinashi_close, title = "heikinashi", color = heikinashi_open < heikinashi_close ? bullish_color : bearish_color)
关键修改说明
- 在
box.new()的right=bar_index后补加逗号,解决语法解析失败的问题。 - else分支中加入
if not na(indiseBox)判断,确保只有当盒子已创建时才执行扩展操作,避免运行时报错。 - 将
plotcandle的参数替换为Heikin Ashi的开高低收数据,正确绘制HA蜡烛形态。
内容的提问来源于stack exchange,提问作者Titikarn Waitayasuwan
相关产品推荐
相关产品推荐

