可变变量不能在request.security()中使用,但有时我们被迫使用它们。有解决方法吗? V5
我们有一个事件的布尔变量:
SomethingHappened = (high > close) and (low < high) //
…如果想限于某个时间范围,则request.security()
是我们的救世主:
SomethingHappenedOnlyIn5Min = request.security(syminfo.tickerid, "5", SomethingHappened)
但是在下面的情况下,我们的somethered happeded happed
事件可以 成为一个可变变体:
//'n' is where our scope array starts from, if not in the current 0 offset
for ArrayOfOffsets = 0 to k
SomethingHappenedInSeries := SomethingHappenedInSeries and/or SomethingHappened[n+ArrayOfOffsets]
在这种情况下,我们的request.security(syminfo.tickerid,“ 5”,sosings happendedInseries)
函数won工作。有没有办法避免此陷阱?
We have a boolean variable for an event:
SomethingHappened = (high > close) and (low < high) //
… which is if wanted to be limited to a certain timeframe then request.security()
is our savior:
SomethingHappenedOnlyIn5Min = request.security(syminfo.tickerid, "5", SomethingHappened)
But e.g. in the following case, our SomethingHappened
event can only be a mutable variant:
//'n' is where our scope array starts from, if not in the current 0 offset
for ArrayOfOffsets = 0 to k
SomethingHappenedInSeries := SomethingHappenedInSeries and/or SomethingHappened[n+ArrayOfOffsets]
In this case, our request.security(syminfo.tickerid, "5", SomethingHappenedInSeries)
function won't work. Is there a way to avoid this trap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
request.security()
不允许使用可变变量的原因是因为它们在脚本的计算过程中根据脚本的全局范围中的计算进行了更改。例如,可变变量a
可以在开始时为0,然后根据MAIM的BAR_INDEX
突变为a:= 1
图表。这不会在
request.security()
中飞行,因为传递给安全性的整个表达式是在传递给它的符号/时间表上分别计算出来的。您无法从security()
请求,因为它明确绑定到图表上当前打开的符号的上下文,因为该变量是根据条件突变的这是在当前图表上计算的。
解决此问题的方法是使用用户定义的功能。如果将所需的所有内容包装在功能中,则可以将可变变量传递到
security()
,并且可以正确计算它们。这是因为该函数是独立的,可以将其提取,而无需与图表上的主要符号有任何联系,并在security()
内进行计算。就您而言,这看起来像这样:
只要您将所有内容包裹在一个可以安全地提取到不同上下文中的中,就可以在
Security()。
The reason
request.security()
does not allow for mutable variables is because they get changed during the script's calculations based on the calculations done in the global scope of the script. For example, a mutable variablea
can be 0 at the beginning and then get mutated toa := 1
based on thebar_index
of the main chart.This doesn't fly in the
request.security()
because the whole expression passed to security is calculated separately on the symbol/timeframe passed to it. You can't requesta
fromsecurity()
because it is explicitly tied to the context of the symbol currently open on your chart, because the variable is mutated based on the condition that is calculated on the current chart.The way to work around this is to use user-defined functions. If you wrap everything you need in a function, you can pass mutable variables to
security()
and it will calculate them properly. This is because the function is self-contained, it can be extracted without any ties to the main symbol on the chart and calculated inside of thesecurity()
.In your case, this would look something like this:
As long as you wrap everything in a self-contained function that can be safely extracted into a different context, mutable variables are fine to be used in
security()
.