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

Pine-Script合并脚本时变量遮蔽警告问题求助

解决Pine-Script变量遮蔽警告问题

问题描述

作为编程新手,使用Pine-Script编写交易策略时,合并两段代码后出现黄色警告,导致无法继续。警告信息如下:

410:13 警告:遮蔽了父作用域中已存在的变量'a',是否应使用':='运算符替代'='?
420:9 警告:遮蔽了父作用域中已存在的变量'l',是否应使用':='运算符替代'='?

原代码

length = input.int(50, 'Evaluation Window', minval=0, maxval=200)
fcast = input.int(50, 'Forecast Window', minval=1, maxval=200)

fmode = input.string('Similarity', 'Forecast Mode', options=['Similarity', 'Dissimilarity'])
cmode = input.string('Cumulative', 'Forecast Construction', options=['Cumulative', 'Mean', 'Linreg'])
src = input(close)

fcast_col = input.color(#2157f3, 'Forecast Style', inline='fcast_style', group='Style')
fcast_style = input.string('· · ·', '', options=['──', '- - -', '· · ·'], inline='fcast_style', group='Style')

show_area = input.bool(true, 'Show Area', inline='areas', group='Style')
fcast_area = input.color(color.new(#ff5d00, 50), '', inline='areas', group='Style')
corr_area = input.color(color.new(#0cb51a, 50), '', inline='areas', group='Style')
eval_area = input.color(color.new(color.gray, 50), '', inline='areas', group='Style')
//----
var lines = array.new_line(0)
if barstate.isfirst
    for i = 0 to fcast - 1 by 1
        array.push(lines, line.new(na, na, na, na))
//----
n = bar_index
d = ta.change(src)

top = ta.highest(src, length + fcast * 2)
btm = ta.lowest(src, length + fcast * 2)

if barstate.islast
    float val = na
    k = 0
    A = array.new_float(0)
    X = array.new_int(0)
    for i = 0 to fcast * 2 + length by 1
        array.push(A, src[i])
        if cmode == 'Linreg'
            array.push(X, n[i])

    a = array.slice(A, 0, fcast - 1)
    for i = 0 to length - 1 by 1
        b = array.slice(A, fcast + i, fcast * 2 + i - 1)
        r = array.covariance(a, b) / (array.stdev(a) * array.stdev(b))
        if fmode == 'Similarity'
            val := r >= nz(val, r) ? r : val
            val
        else
            val := r <= nz(val, r) ? r : val
            val
        k := val == r ? i : k
        k

    prev = src
    current = src

    for i = 0 to fcast - 1 by 1
        e = d[fcast + k + fcast - i - 1]
        if cmode == 'Mean'
            current := array.avg(a) + e
            current
        else if cmode == 'Linreg'
            a = array.slice(A, 0, fcast)
            x = array.slice(X, 0, fcast)
            alpha = array.covariance(a, x) / array.variance(x)
            beta = array.avg(a) - alpha * array.avg(x)
            current := alpha * (n + i + 1) + beta + e
            current
        else
            current += e
            current

        l = array.get(lines, i)
        line.set_xy1(l, n + i, prev)
        line.set_xy2(l, n + i + 1, current)
        line.set_color(l, fcast_col)

        if fcast_style == '- - -'
            line.set_style(l, line.style_dashed)
        else if fcast_style == '· · ·'
            line.set_style(l, line.style_dotted)

        prev := current
        prev

    if show_area
        box.delete(box.new(n - length - fcast * 2 + 1, top, n - fcast + 1, btm, border_color=na, bgcolor=eval_area)[1])
        box.delete(box.new(n - fcast + 1, top, n, btm, border_color=na, bgcolor=fcast_area)[1])
        box.delete(box.new(n - k - fcast * 2 + 1, btm, n - k - fcast, top, border_color=na, bgcolor=corr_area)[1])

解决方案

问题原因

Pine-Script中,=用于声明并初始化新变量,:=用于给已存在的变量重新赋值。当你在子作用域(比如内层循环、条件分支)用=重新声明父作用域已存在的变量时,会触发"变量遮蔽"警告——这意味着你创建了一个和父作用域同名的局部变量,覆盖了外层变量的作用范围,可能导致逻辑错误。

修改步骤

  1. 变量a的修改:在barstate.islast块中已经用a = array.slice(A, 0, fcast - 1)声明了a,后续Linreg分支里的a = array.slice(A, 0, fcast)是给已有变量重新赋值,需改为a := array.slice(A, 0, fcast)。
  2. 变量l的修改:提前在barstate.islast块内声明l为line类型变量,然后在循环内用l := array.get(lines, i)赋值,避免触发遮蔽警告。

修改后的完整代码

length = input.int(50, 'Evaluation Window', minval=0, maxval=200)
fcast = input.int(50, 'Forecast Window', minval=1, maxval=200)

fmode = input.string('Similarity', 'Forecast Mode', options=['Similarity', 'Dissimilarity'])
cmode = input.string('Cumulative', 'Forecast Construction', options=['Cumulative', 'Mean', 'Linreg'])
src = input(close)

fcast_col = input.color(#2157f3, 'Forecast Style', inline='fcast_style', group='Style')
fcast_style = input.string('· · ·', '', options=['──', '- - -', '· · ·'], inline='fcast_style', group='Style')

show_area = input.bool(true, 'Show Area', inline='areas', group='Style')
fcast_area = input.color(color.new(#ff5d00, 50), '', inline='areas', group='Style')
corr_area = input.color(color.new(#0cb51a, 50), '', inline='areas', group='Style')
eval_area = input.color(color.new(color.gray, 50), '', inline='areas', group='Style')
//----
var lines = array.new_line(0)
if barstate.isfirst
    for i = 0 to fcast - 1 by 1
        array.push(lines, line.new(na, na, na, na))
//----
n = bar_index
d = ta.change(src)

top = ta.highest(src, length + fcast * 2)
btm = ta.lowest(src, length + fcast * 2)

if barstate.islast
    float val = na
    k = 0
    A = array.new_float(0)
    X = array.new_int(0)
    for i = 0 to fcast * 2 + length by 1
        array.push(A, src[i])
        if cmode == 'Linreg'
            array.push(X, n[i])

    a = array.slice(A, 0, fcast - 1)
    for i = 0 to length - 1 by 1
        b = array.slice(A, fcast + i, fcast * 2 + i - 1)
        r = array.covariance(a, b) / (array.stdev(a) * array.stdev(b))
        if fmode == 'Similarity'
            val := r >= nz(val, r) ? r : val
            val
        else
            val := r <= nz(val, r) ? r : val
            val
        k := val == r ? i : k
        k

    prev = src
    current = src
    line l = na // 提前声明line类型变量,规范作用域

    for i = 0 to fcast - 1 by 1
        e = d[fcast + k + fcast - i - 1]
        if cmode == 'Mean'
            current := array.avg(a) + e
            current
        else if cmode == 'Linreg'
            a := array.slice(A, 0, fcast) // 修改为:=,给已有变量重新赋值
            x = array.slice(X, 0, fcast)
            alpha = array.covariance(a, x) / array.variance(x)
            beta = array.avg(a) - alpha * array.avg(x)
            current := alpha * (n + i + 1) + beta + e
            current
        else
            current += e
            current

        l := array.get(lines, i) // 修改为:=,给已声明的变量赋值
        line.set_xy1(l, n + i, prev)
        line.set_xy2(l, n + i + 1, current)
        line.set_color(l, fcast_col)

        if fcast_style == '- - -'
            line.set_style(l, line.style_dashed)
        else if fcast_style == '· · ·'
            line.set_style(l, line.style_dotted)

        prev := current
        prev

    if show_area
        box.delete(box.new(n - length - fcast * 2 + 1, top, n - fcast + 1, btm, border_color=na, bgcolor=eval_area)[1])
        box.delete(box.new(n - fcast + 1, top, n, btm, border_color=na, bgcolor=fcast_area)[1])
        box.delete(box.new(n - k - fcast * 2 + 1, btm, n - k - fcast, top, border_color=na, bgcolor=corr_area)[1])

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 11:27:50