Pine Script代码无法运行:‘未声明标识符’等报错排查与解决
Flag Pattern指标代码报错排查与修复
报错列表
- 4:37处:Undeclared identifier 'input.'
- 4:32处:The 'input' function does not have an argument with the name 'type'
- 6:5处:Could not find function or function reference 'highest'
问题原因
- 输入函数语法不兼容:Pine Script v5对
input函数做了语法调整,不再支持type参数,且原代码中input.integer是v4的写法,v5中需使用input.int这类专用输入函数。 - 函数命名空间变更:Pine Script v5将
highest、lowest这类技术分析函数归入ta命名空间下,直接调用会导致找不到函数。
解决方法
- 修正输入函数写法:将
input(title="Length", type=input.integer, defval=10)替换为input.int(title="Length", defval=10),移除type参数,使用v5对应类型的输入函数。 - 添加函数命名空间前缀:将
highest(high, length)改为ta.highest(high, length),lowest(low, length)改为ta.lowest(low, length),通过ta.前缀调用v5的技术分析函数。
修复后的完整代码
//@version=5 indicator("Flag Pattern", shorttitle="Flag Pattern", overlay=true) length = input.int(title="Length", defval=10) h = ta.highest(high, length) l = ta.lowest(low, length) price_range = h - l flag_height = price_range * 2 flag_width = ta.rma(ta.tr, length) * 2 flagpole_start_x = length - 1 flagpole_start_y = l flagpole_end_x = length - 1 flagpole_end_y = h flag_start_x = flagpole_end_x + 1 flag_start_y = flagpole_end_y flag_end_x = flag_start_x + flag_width flag_end_y = flag_start_y + flag_height line.new(x1=flagpole_start_x, y1=flagpole_start_y, x2=flagpole_end_x, y2=flagpole_end_y, color=color.white, width=1) linebr = line.new(x1=flag_start_x, y1=flag_start_y, x2=flag_end_x, y2=flag_end_y, color=color.white, width=1) line.new(x1=flag_start_x, y1=flagpole_start_y, x2=flagpole_end_x, y2=flagpole_start_y, color=color.white, width=1) line.new(x1=flag_end_x, y1=flagpole_end_y, x2=flagpole_end_x, y2=flag_start_y, color=color.white, width=1) flag_high = ta.valuewhen(ta.crossunder(low, l, 1), high, 0) flag_low = ta.valuewhen(ta.crossover(high, h, 1), low, 0) is_flag = flag_high > 0 and flag_low > 0 and flag_high > flag_low and flag_high - flag_low > price_range bgcolor(is_flag ? color.green : na, transp=80)
注:额外补充了ta.tr和ta.rma的命名空间前缀,因为v5中这些函数同样属于ta命名空间,避免潜在报错。
内容的提问来源于stack exchange,提问作者Sarah Eid
相关产品推荐
相关产品推荐

