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

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 07:37:12