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

Pine Script脚本报错‘Syntax error at input ':='’求助排查

Pine Script 语法错误修正及逻辑优化

错误原因分析

第17行的语法错误源于Pine Script v4中数组元素赋值的语法规则:不能直接用profile[i] := volume[i]这种方式给数组元素赋值,必须使用内置的array.set()函数来操作数组元素。

另外你的脚本还存在几处逻辑错误,会导致运行后显示异常,一并修正如下:

修正后的完整代码

//@version=4
study("Auto Anchored Volume Profile", overlay=true)

// 定义输入变量
autoAnchor = input(true, "自动锚定")
anchorPeriod = input(1, "锚定期数") // 用于锚定剖面的K线数量
profileWidth = input(10, "剖面宽度") // 剖面包含的K线数量

// 计算锚定价格
anchorPrice = na
if autoAnchor
    anchorPrice := close[anchorPeriod]

// 计算成交量剖面
profile = array.new_float(profileWidth, na)
for i = 0 to profileWidth - 1
    // 修正:使用array.set()给数组元素赋值
    array.set(profile, i, volume[i])

// 绘制成交量剖面
for i = 0 to profileWidth - 1
    volValue = array.get(profile, i)
    if not na(volValue)
        // 修正:line.new的x参数为K线索引,y参数为价格,原逻辑x/y颠倒
        // 将成交量转换为价格偏移量(可根据品种调整比例)
        barPos = bar_index - (profileWidth - 1 - i)
        line.new(x1=barPos, y1=anchorPrice, x2=barPos, y2=anchorPrice + volValue/1000, color=color.blue, width=1)

// 绘制锚定点
// 修正:通过price参数指定标签的价格位置
plotshape(autoAnchor, title="锚定点", location=location.absolute, price=anchorPrice, color=color.red, style=shape.labelup, text="A")

// 绘制当前价格
// 修正:用price参数指定当前价格位置,条件设为true则持续显示
plotshape(true, title="当前价格", location=location.absolute, price=close, color=color.green, style=shape.labeldown, text="C")

关键修改点说明

  • 数组赋值修正:将profile[i] := volume[i]替换为array.set(profile, i, volume[i]),这是Pine Script v4操作数组的标准语法,读取数组元素对应使用array.get()。
  • 线条绘制逻辑修正:原代码line.new的x/y参数颠倒,Pine Script的x轴是K线索引(bar_index),y轴是价格;同时将成交量转换为价格方向的偏移量(除以1000是适配价格刻度的示例,可根据交易品种调整比例)。
  • Plotshape参数修正:plotshape的第一个参数是显示条件,需通过price参数指定标签的价格位置,否则无法正确显示在目标价格上。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 09:16:10