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

Pine Script代码第27行pine_vwmean函数语法错误排查请求

问题排查与修复

核心错误原因

你遇到的语法错误主要来自两个关键问题:

  1. VWAPIndicator函数未返回值:自定义函数必须明确输出计算结果,但你的VWAPIndicator最后仅计算了vwmean和dev,没有将这两个值作为返回值抛出,导致调用时的解构赋值[vwmean, dev] = VWAPIndicator()失败,连带触发pine_vwmean相关的语法报错。
  2. Pine Script v4循环语法冗余:for i = 0 to y - 1 by 1中的by 1属于冗余代码,v4默认循环步长为1,虽然不属于严重语法错误,但可能引发解析异常。

修复后的完整代码

//@version=4
strategy("HOSS Scalping", overlay=true)

// Custom OBV RSI Indicator
OBVIndicator() =>
    obvsrc = close
    change_1 = change(obvsrc)
    obv = cum(change(obvsrc) > 0 ? volume : change_1 < 0 ? -volume : 0 * volume)

    src = obv
    len = 5
    up = rma(max(change(src), 0), len)
    down = rma(-min(change(src), 0), len)
    rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
    rsi

// Custom VWAP Indicator
VWAPIndicator() => 
    ls = false
    type = 'Average Deviation'
    length = 60
    source = close
    _low = ls == true ? math.log(low) : low
    _high = ls == true ? math.log(high) : high
    src = ls == true ? math.log(source) : source

    //weighted mean
    pine_vwmean(x, y) =>
        cw = 0
        cd = 0.0
        w_sum = 0.0
        d_sum = 0.0
        for i = 0 to y - 1
            cd = x[i]
            cw = volume[i]
            d_sum += cw * cd
            w_sum += cw
        d_sum / w_sum

    //weighted standard deviation
    pine_vwstdev(x, y, b) =>
        d_sum = 0.0
        w_sum = 0.0
        cd = 0.0
        for i = 0 to y - 1
            cd = x[i]
            cw = volume[i]
            d_sum += cw * math.pow(cd - b, 2)
            w_sum += cw
        math.sqrt(d_sum / w_sum)

    //weighted average deviation
    pine_vwavdev(x, y, b) =>
        d_sum = 0.0
        w_sum = 0.0
        cd = 0.0
        for i = 0 to y - 1
            cd = x[i]
            cw = volume[i]
            d_sum += cw * math.abs(cd - b)
            w_sum += cw
        d_sum / w_sum

    vwmean = pine_vwmean(src, length)

    dev = if type == 'Standard Deviation'
        pine_vwstdev(src, length, vwmean)
    else if type == 'Average Deviation'
        pine_vwavdev(src, length, vwmean)
    
    // 关键:返回计算结果给调用方
    [vwmean, dev]

// Long Entry Conditions
obvRsi = OBVIndicator()

[vwmean, dev] = VWAPIndicator()
lowerDev2 = vwmean - 2 * dev
lowerDev3 = vwmean - 3 * dev

longCondition = close[1] > lowerDev2 and open[1] >= lowerDev3 and open[1] <= lowerDev2 and (obvRsi < 30 or (obvRsi >= 30 and obvRsi[1] < 30)) and ta.adx < 30 

if (longCondition)
    strategy.entry("Long", strategy.long)

plotshape(longCondition, title="Long Signal", location=location.belowbar, color=color.green, shape=shape.triangleup, size=size.small)
// 修复:注释未定义的shortCondition绘图代码,避免额外报错
// plotshape(shortCondition, title="Short Signal", location=location.abovebar, color=color.red, shape=shape.triangledown, size=size.small)

额外优化说明

  • 移除循环中的by 1冗余代码,符合Pine Script v4语法规范。
  • 简化dev的赋值逻辑,去掉重复的变量赋值语句。
  • 注释未定义的shortCondition绘图代码,避免额外运行报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 05:40:26