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

如何在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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 19:31:09