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

浪子不回头ぞ 提交于 2021-01-29 12:21:48

问题


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)

回答1:


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.



来源:https://stackoverflow.com/questions/64625567/cannot-use-a-mutable-variable-as-an-argument-of-the-security-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!