不能使用可变变量作为安全函数的参数 [英] Cannot use a mutable variable as an argument of the security function

查看:55
本文介绍了不能使用可变变量作为安全函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的脚本无法编译.
它抛出错误Cannot use a mutable variable as an argument of the security function
我不明白为什么.
我在安全函数中使用的参数不是可变变量.
当我注释掉 h := h * 3 行时,脚本编译正常.
有人知道这里发生了什么吗?
这可能是 Pine 脚本的错误吗?

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)

推荐答案

出于某种原因,当用户定义的函数返回它们时,解构赋值的处理方式与 security() 时不同做.将您的 security() 调用封装在一个函数中即可:

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)

请注意,您在使用前瞻时使用的是历史柱的未来数据,而不是像您在那里所做的那样将系列偏移 1.

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.

这篇关于不能使用可变变量作为安全函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆