如何在Pine Script v5中将序列向后偏移1位?
Pine Script v5 实现序列向后偏移并补0
如果你的current是数组类型,可以通过复制原数组并在末尾追加0的方式实现需求:
//@version=5 indicator("序列偏移示例", overlay=true) // 初始化原数组(对应你给出的[1 0 0 1 0 0 1 0 0 1]) current = array.new_int(10, 0) array.set(current, 0, 1) array.set(current, 3, 1) array.set(current, 6, 1) array.set(current, 9, 1) // 生成偏移后的数组:复制原数组 + 末尾补0 shifted = array.copy(current) array.push(shifted, 0) // 最后一根K线时输出结果,用于验证 if barstate.islast label.new(bar_index, high, "原数组:" + str.tostring(current) + "\n偏移后:" + str.tostring(shifted), color=color.white, textcolor=color.black)
关键步骤说明
- 使用
array.copy()复制原数组,确保原数组内容不被修改 - 通过
array.push()在复制后的数组末尾添加0,直接得到长度+1且末尾为0的目标序列
内容的提问来源于stack exchange,提问作者FDL
相关产品推荐
相关产品推荐

