code like this:
//@version=5
indicator("My script")
x = if open > close
var a = 10
var b = 20
a := 20
b := 30
(a+b)[1]
plot(x)
compiler report "a" and "b" in (a+b)[1] are undeclared identifiers:
line 11: Undeclared identifier 'a';
line 11: Undeclared identifier 'b'
but 'a' and 'b' are declared in the block if modify expr '(a+b)[1]' to 'a+b', the compilation is successful. how the compiler works?
In my case, compiler reports the issue specifically with the a
on the 11th line (in the else
branch). In this case, the error is expected because the a
variable was declared inside the if
branch and does not exist in the else
branch at all.
For this to work, you need to declare a outside of the if/else
condition. Here's one possible way to rewrite this code:
//@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)
Very often, the compiler message does not exactly match the problem, you have to guess what it is trying to say.
In this case and logically, the declarations should not be in an "if" section because the variables are accessible everywhere in the script, so you must declare them at the lowest level.
You can use it like (a[1]+b[1]) right in the meantime
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.