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

Pine Script已设置max_bars_back仍提示序列引用长度报错如何解决

报错根因

Pine Script编译器无法解析动态偏移的序列引用长度:你在for循环中使用动态变量index_cross作为偏移计算的基准,即使全局设置了max_bars_back=50,编译器也无法预判你实际要回溯的RSI序列长度,因此抛出错误。
同时原代码的for循环逻辑存在错误:index_cross存储的是K线的绝对索引bar_index,直接作为偏移值使用完全不符合「交叉时点前50根K线」的需求。

修复方案

不需要使用for循环,直接用Pine内置的区间极值函数即可实现需求,性能更高且不会触发回溯长度报错:

study("error max bars back fix", overlay=false, max_bars_back=50)

//input
oversold = input(30, title="oversold")
rsiLen   = input(14, title="rsi len")

//rsi and ema
rsi = rsi(close, rsiLen)
ema1 = ema(close, 21)
ema2 = ema(close,50)

// 核心逻辑:ema1上穿ema2时,校验过去50周期RSI最小值是否低于超卖阈值
cross = crossover(ema1, ema2)
rsi_valid = cross and lowest(rsi, 50) < oversold

//final condition and plot
a = rsi_valid ? 1 : 0
plot(a, color=color.white)
补充说明

如果坚持要使用for循环实现,可在RSI计算后单独指定RSI序列的回溯长度:

rsi = rsi(close, rsiLen)
max_bars_back(rsi, 50)

再调整for循环的偏移逻辑即可,但内置函数的实现方式更符合Pine Script的设计规范,执行效率也更高。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 14:06:04