Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6875

Cannot use a mutable variable as an argument of the security function

The script below does not compile.
It throws the error Cannot use a mutable variable as an argument of the security function
I don't understand why.
The arguments I use in the security function are not mutable variables.
When I comment out the line h := h * 3, the script compiles ok.
Does anybody know what's going on here?
Could this be a Pine script bug?

//@version=4
study("My Script")

[h, l, c] = security(syminfo.ticker, "D", [high,low,close], lookahead = barmerge.lookahead_on) // actual daily high,low,close.
h := h * 3 // Commenting this line results removes the error: "Cannot use a mutable variable as an argument of the security function."

plot(h)

Upvotes: 1

Views: 714

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8779

For some reason, destructured assignments aren't treated the same way when a user-defined function returns them vs when security() does. Encapsulating your security() call in a function will work:

//@version=4
study("")
f_sec() => security(syminfo.tickerid, "D", [high,low,close], lookahead = barmerge.lookahead_on)
[h, l, c] = f_sec()
h := h * 3
plot(h)

Note that you are using future data on historical bars when using lookahead and not offsetting the series by 1, as you are doing in there.

Upvotes: 1

Related Questions