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

TradingView脚本报错:array.get()索引-1越界,数组大小为0

问题原因

报错的核心是**exitCondition的计算逻辑在bar 0(第一根K线)时就会执行array.get(stopLosses, count_of_trade - 1)**:

  • 初始状态下count_of_trade = 0,计算count_of_trade -1得到无效索引-1
  • 尽管你在后续的if判断中添加了数组大小检查,但exitCondition的计算是独立于if语句的,在bar 0时会优先执行这行代码,直接触发数组越界错误
修复方案

修改exitCondition的定义,先判断数组非空且交易计数大于0,再执行数组取值操作:

// 原错误代码
// exitCondition = close <= array.get(stopLosses, count_of_trade - 1)

// 修改后的代码
exitCondition = array.size(stopLosses) > 0 and count_of_trade > 0 and close <= array.get(stopLosses, count_of_trade - 1)

同时可以优化switchCondition的逻辑,添加索引有效性判断,避免极端情况下的潜在问题:

if (switchCondition and array.size(stopLosses) > 0 and array.size(targets) > 0)
    lastIdx = array.size(stopLosses) - 1
    if lastIdx >= 0
        stopLoss := array.get(stopLosses, lastIdx)
        entryPrice := array.get(targets, lastIdx)
        target := entryPrice + (entryPrice - stopLoss)
        array.set(stopLosses, lastIdx, stopLoss)
        array.set(targets, lastIdx, target)
完整修复后的代码
// Define variables
var float target = na
var bool inLong = false
var float pip_distance_threshold = 0.03 / syminfo.mintick
var float tp = pip_distance_threshold / 2
var int count_of_trade = 0
var float entryPrice = na
var float stopLoss = na

// Arrays to store trade information
var float[] entryPrices = array.new_float(0)
var float[] stopLosses = array.new_float(0)
var float[] targets = array.new_float(0)

// Function to find the nearest demand zone
f_nearest_demand_zone(demand_zones) =>
    float nearest_demand_zone = na
    float min_distance = 10000000000.01
    for i = 0 to array.size(demand_zones) - 1
        top_price = box.get_top(array.get(demand_zones, i))
        bottom_price = box.get_bottom(array.get(demand_zones, i))
        demand_zone_price = (top_price + bottom_price) / 2
        if open >= demand_zone_price
            distance = open - demand_zone_price
            if distance < min_distance
                min_distance := distance
                nearest_demand_zone := demand_zone_price
    nearest_demand_zone

nearestDemandZone = f_nearest_demand_zone(current_demand_box)
distanceToDemandZone = na(nearestDemandZone) ? na : open - nearestDemandZone

buyCondition = tradeSignal and bullishCandle and not na(nearestDemandZone) and (distanceToDemandZone / syminfo.mintick <= pip_distance_threshold)
plotshape(buyCondition, title="buy", location=location.belowbar, color=color.green, style=shape.triangleup, text="confirm buy")

if (buyCondition and not inLong)
    entryPrice := close
    stopLoss := nearestDemandZone - (tp * syminfo.mintick)
    target := close + (close- (nearestDemandZone - (tp*syminfo.mintick)))
    inLong := true
    count_of_trade += 1
    array.push(entryPrices, entryPrice)
    array.push(stopLosses, stopLoss)
    array.push(targets, target)

switchCondition = high >= target

if (switchCondition and array.size(stopLosses) > 0 and array.size(targets) > 0)
    lastIdx = array.size(stopLosses) - 1
    if lastIdx >= 0
        stopLoss := array.get(stopLosses, lastIdx)
        entryPrice := array.get(targets, lastIdx)
        target := entryPrice + (entryPrice - stopLoss)
        array.set(stopLosses, lastIdx, stopLoss)
        array.set(targets, lastIdx, target)

// 修改后的exitCondition,先判断数组和计数有效性
exitCondition = array.size(stopLosses) > 0 and count_of_trade > 0 and close <= array.get(stopLosses, count_of_trade - 1)

if (exitCondition and inLong)
    inLong := false
    string exitText = "Exit Buy Position " + str.tostring(count_of_trade)
    label.new(x=bar_index, y=low, text=exitText, style=label.style_label_down, color=color.red, textcolor=color.white)
    // Clear arrays
    array.clear(entryPrices)
    array.clear(stopLosses)
    array.clear(targets)
    // 重置交易计数,避免下次交易索引错位
    count_of_trade := 0
额外优化点
  • 退出仓位后重置count_of_trade = 0,防止后续交易时计数累加导致索引错位
  • 移除了exitText的var声明,避免文本被重复定义

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 21:26:23