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

Pinescript V4数组赋值语法错误求助:更新histogram数组失败

解决Pinescript 4中数组元素更新的语法错误

你的问题出在Pinescript 4的数组操作语法上——Pinescript 4不支持通过数组[索引] := 值的方式直接修改数组元素,必须使用内置的array.set()函数来完成元素赋值。

修改后的代码片段

// Function to calculate POC, VAH, and VAL
calcVolumeProfile() =>
    // Create and initialize a histogram
    var float[] histogram = array.new_float(0)

    for i = 0 to array.size(dailyPrices) - 1
        price = dailyPrices[i]
        vol = dailyVolumes[i]
        priceBin = floor(price / (syminfo.mintick * binSize)) * (syminfo.mintick * binSize)
        index = array.indexof(histogram, priceBin)
        
        if index == -1
            array.push(histogram, priceBin)
            array.push(histogram, vol)
        else
            // 使用array.set()替换直接索引赋值
            array.set(histogram, index + 1, array.get(histogram, index + 1) + vol)

关键说明

  • 在Pinescript 4中,读取数组元素用array.get(数组名, 索引),修改元素用array.set(数组名, 索引, 新值),这是官方规定的数组操作方式。
  • 你设计的数组结构(价格bin与成交量交替存储)是可行的,只要保证索引计算正确即可。
  • var关键字的使用是正确的,它确保数组只会在脚本初始化时创建一次,不会每次bar都重置。

内容的提问来源于stack exchange,提问作者Jonas Bäumer

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 04:22:08