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

Pine Script动态变量在ta.lowest/ta.highest中失效问题求助

问题原因及解决方案

问题根源

报错提示Invalid value of the 'length' argument (0),说明你动态计算的i_LocalLookBack在部分K线(bar)上出现了≤0的数值,而ta.lowest()要求length参数必须大于0。

具体逻辑问题:

  • ta.highestbars(n)返回当前bar到最近最高值的偏移量(负数表示在当前bar之前),乘以-1后得到正向偏移值;
  • 当i_HighestBar为1时,i_HighestBar - 1 = 0,此时传入ta.lowest()的length为0,触发参数非法错误;
  • 在图表初始化的第一根bar(bar 0),由于历史数据不足,ta.highestbars()的计算结果也会导致i_LocalLookBack出现非法值。

修复代码

给动态计算的length参数添加下限约束,用math.max()确保它始终≥1,满足ta.lowest()的参数要求:

// @version = 5
indicator(title="Fibonacci Retracement", shorttitle="Fib Retrace", overlay = true)

i_LookBackPeriods = input.int(defval=60, title="Periods To Look Back", group="Options")
i_HighestBar      = ta.highestbars(i_LookBackPeriods + 1) * -1
// 强制约束length最小值为1,避免非法参数
i_LocalLookBack   = math.max(i_HighestBar - 1, 1)
f_LocalClose      = ta.lowest(close, i_LocalLookBack)

额外说明

TradingView确实支持动态length参数,但前提是参数必须符合函数的输入规则(此处即>0)。通过math.max()强制约束数值范围,就能解决动态参数的合法性问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 05:32:50