【问题标题】:Pine Script v5 compiler report "undeclared identifier"Pine Script v5 编译器报告“未声明的标识符”
【发布时间】:2022-07-25 17:45:28
【问题描述】:

这样的代码:

//@version=5
indicator("My script")

x = if open > close
    var a = 10
    var b = 20
    a := 20
    b := 30
    (a+b)[1]
else
    a
plot(x)

编译器报告 (a+b)[1] 中的“a”和“b”是未声明的标识符:

line 11: Undeclared identifier 'a';
line 11: Undeclared identifier 'b'

【问题讨论】:

    标签: pine-script pinescript-v5


    【解决方案1】:

    在我的例子中,编译器在第 11 行(在 else 分支中)专门用 a 报告了这个问题。在这种情况下,错误是意料之中的,因为 a 变量是在 if 分支中声明的,而在 else 分支中根本不存在。

    为此,您需要在if/else 条件之外声明一个。这是重写此代码的一种可能方法:

    //@version=5
    indicator("My script")
    
    f() =>
        var a = 10
        var b = 20
        if open > close
            a := 20
            b := 30
            (a+b)[1]
        else
            a
    x = f()
        
    plot(x)
    

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 2020-07-22
      • 1970-01-01
      • 1970-01-01
      • 2023-02-11
      • 2014-08-17
      • 2023-02-05
      • 2021-03-18
      • 2012-09-04
      相关资源
      最近更新 更多