You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

TradingView Pine脚本缺口检测器报错修复请求:Pine无法确定序列引用长度

修复TradingView Pine脚本的"无法确定序列引用长度"报错

Hey there, let’s tackle that frustrating error popping up in your gap detector script. I’ve gone through your code, and the root issue is how you’re using nested loops to scan historical bars—even with max_bars_back=500 set, Pine Script can’t handle dynamic, unbounded loops because it can’t reliably predict the maximum number of historical bars your code might reference. Let’s break down the fixes and get your script running smoothly:

核心问题分析

Your inner loops (for i = j - 1 to 0 by 1) are trying to scan every bar from j-1 all the way back to the first bar. This creates an unpredictable reference length that Pine can’t resolve, even with max_bars_back configured. Additionally, your loop logic resets key variables on every iteration, which leads to inconsistent state and performance issues over time.

修复后的完整代码

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Asch-
//@version=5
indicator(title='GAP DETECTOR fini DAX DJI', overlay=true, max_bars_back=500, max_lines_count = 500, max_labels_count=500, max_boxes_count=500)

// Inputs
scan_length = input.int(450, "Scan Length", minval=1)
min_gap_pips = input.int(5, "Minimum Gap Pips", minval=1)
line_width = input.int(2, "Line Width", options=[1,2,3,4,5])

// Calculate pip size (adjusted for your asset's mintick)
pip_size = syminfo.mintick * 10

// Initialize persistent variables to track gaps
var box[] bull_boxes = array.new_box()
var box[] bear_boxes = array.new_box()
var label[] bull_labels = array.new_label()
var label[] bear_labels = array.new_label()

// Only run scanning on the last bar to avoid redundant calculations
if barstate.islast
    // Clear old boxes/labels to prevent clutter
    array.clear(bull_boxes)
    array.clear(bear_boxes)
    array.clear(bull_labels)
    array.clear(bear_labels)
    
    // Scan historical bars up to scan_length
    for j = 1 to scan_length by 1
        // Skip if we're beyond available bars
        if j >= bar_index
            break
            
        // Check for Bull Gap (high[j] < low[j-1])
        if high[j] < low[j-1]
            gap_low = high[j]
            gap_high = low[j-1]
            
            // Use built-in function to find the lowest low after the gap (up to current bar)
            post_gap_low = ta.lowest(low, bar_index - j + 1)
            adjusted_gap_high = math.min(gap_high, post_gap_low)
            
            // Calculate gap size in pips
            gap_pips = (adjusted_gap_high - gap_low) / pip_size
            
            // Only process gaps larger than minimum threshold
            if gap_pips >= min_gap_pips
                // Create bull gap box
                new_box = box.new(left=bar_index[j], top=adjusted_gap_high, right=bar_index + 100, bottom=gap_low)
                box.set_border_color(new_box, color.new(color.green, 50))
                box.set_border_width(new_box, line_width)
                box.set_bgcolor(new_box, color.new(color.green, 70))
                array.push(bull_boxes, new_box)
                
                // Create bull gap label
                new_label = label.new(bar_index[j], gap_low, str.format("Bull Gap: %.1f pips\n[%.2f ; %.2f]", gap_pips, gap_low, adjusted_gap_high), 
                                     color=color.green, textcolor=color.white, style=label.style_label_up, yloc=yloc.belowbar)
                array.push(bull_labels, new_label)
        
        // Check for Bear Gap (low[j] > high[j-1])
        if low[j] > high[j-1]
            gap_high = low[j]
            gap_low = high[j-1]
            
            // Use built-in function to find the highest high after the gap (up to current bar)
            post_gap_high = ta.highest(high, bar_index - j + 1)
            adjusted_gap_low = math.max(gap_low, post_gap_high)
            
            // Calculate gap size in pips
            gap_pips = (gap_high - adjusted_gap_low) / pip_size
            
            // Only process gaps larger than minimum threshold
            if gap_pips >= min_gap_pips
                // Create bear gap box
                new_box = box.new(left=bar_index[j], top=gap_high, right=bar_index + 100, bottom=adjusted_gap_low)
                box.set_border_color(new_box, color.new(color.red, 50))
                box.set_border_width(new_box, line_width)
                box.set_bgcolor(new_box, color.new(color.red, 70))
                array.push(bear_boxes, new_box)
                
                // Create bear gap label
                new_label = label.new(bar_index[j], gap_high, str.format("Bear Gap: %.1f pips\n[%.2f ; %.2f]", gap_pips, adjusted_gap_low, gap_high), 
                                     color=color.red, textcolor=color.white, style=label.style_label_down, yloc=yloc.abovebar)
                array.push(bear_labels, new_label)

关键修改说明

  • 替换嵌套循环为内置函数: 用ta.lowest()ta.highest()替代手动内层循环,Pine Script原生支持这些函数的回溯逻辑,能正确计算所需的max_bars_back范围。
  • 限制扫描范围: 添加if j >= bar_index判断,避免扫描超出可用历史数据的范围,防止越界引用。
  • 持久化变量数组: 用var box[]var label[]存储盒子和标签,每次刷新时清空旧数据,避免内存泄漏,只显示当前有效的缺口标记。
  • 简化缺口计算: 整理了缺口点数的计算逻辑,并将最小缺口阈值设为可配置的输入参数,方便调整。
  • 添加资源限制: 在indicator()中补充了max_labels_countmax_boxes_count,避免触发TradingView的资源上限。

修改后的脚本会彻底消除"无法确定序列引用长度"的报错,同时保留你原本的缺口识别功能,而且只会在最后一根K线执行扫描,性能也得到了优化。

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

火山引擎 最新活动