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

Pine Script中temp_x[j]赋值语法错误修复方案咨询

Pine Script 语法错误解决:array[index] = value 赋值报错

问题根源

Pine Script 不支持传统编程语言中直接通过数组名[索引] = 值的方式给数组元素赋值,必须使用官方提供的数组操作函数进行赋值,这是你遇到Syntax error at input '='的核心原因。

解决步骤

  1. 替换赋值语法
    把报错的temp_x[j] = data[i - window_size + j]替换为Pine Script标准的数组赋值函数array.set():

    array.set(temp_x, j, data[i - window_size + j])
    
  2. 检查data的访问方式
    如果data也是数组,同样不能直接用data[索引]取值,需要改用array.get():

    var float[] data = array.new_float()
    // ... 给data赋值的逻辑
    val = array.get(data, i - window_size + j)
    array.set(temp_x, j, val)
    
  3. 额外检查点

    • 确保temp_x是通过array.new_float()/array.new_int()等函数正确初始化的数组,比如:
      var float[] temp_x = array.new_float()
      
    • 验证i、j、window_size的取值范围,避免数组索引越界(索引必须在数组元素数量的合法范围内)
    • 如果data是Pine Script的序列(比如close这类内置行情数据),需要先将序列转为数组再操作:
      float[] data_array = array.from(close)
      val = array.get(data_array, i - window_size + j)
      array.set(temp_x, j, val)
      

修正示例片段

// 初始化数组
var int window_size = 10
var float[] temp_x = array.new_float(window_size)
var float[] data = array.new_float()

// 填充data数组示例
for idx = 0 to bar_index
    array.push(data, close[idx])

// 修正后的赋值逻辑
if bar_index >= window_size
    for j = 0 to window_size - 1
        val = array.get(data, bar_index - window_size + j)
        array.set(temp_x, j, val)

内容的提问来源于stack exchange,提问作者Matias Ignacio

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 10:59:58