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
相关产品推荐
相关产品推荐

