Pine Script提取可见K线最高价报错:length参数无效问题处理
动态获取可见K线收盘价最高值的问题与解决方法
问题背景
我想要在滚动图表时动态获取可见K线及指标的最高值,先编写了统计可见K线总数的代码,运行正常,返回值可随滚动动态调整:
VisibleBars() => bool result = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time TotalBars() => var int result = 0 if VisibleBars() result += 1 result T = TotalBars() plot(T,"Total")
但后续尝试获取可见K线收盘价最高值时,程序编译通过却无法绘图,报错:
Study Error: Error on bar 0: invalid value of 'length' in'highest' function. Must be>0
尽管图表显示T>0,但ta.highest函数仍识别length参数无效,手动替换T为数值则正常运行,尝试math.abs和int强制转换也无效。
解决方案
通过以下代码解决问题,使用math.max消除T的零值,针对长周期添加缓冲区设置:
var float HighClose = na T2 = math.max(1, T) T2_passed_length = bar_index == 0 ? 1000 : T2 HighestClose := ta.highest(close,T2_passed_length) plot(HighClose,"highest close", color=color.white)
内容的提问来源于stack exchange,提问作者paamachat
相关产品推荐
相关产品推荐

