可变变量不能在request.security()中使用,但有时我们被迫使用它们。有解决方法吗? V5

发布于 2025-01-27 00:46:59 字数 741 浏览 3 评论 0原文

我们有一个事件的布尔变量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-02-03 00:46:59

request.security()不允许使用可变变量的原因是因为它们在脚本的计算过程中根据脚本的全局范围中的计算进行了更改。例如,可变变量a可以在开始时为0,然后根据MAIM的BAR_INDEX突变为a:= 1图表。

这不会在request.security()中飞行,因为传递给安全性的整个表达式是在传递给它的符号/时间表上分别计算出来的。您无法从security()请求,因为它明确绑定到图表上当前打开的符号的上下文,因为该变量是根据条件突变的这是在当前图表上计算的。

解决此问题的方法是使用用户定义的功能。如果将所需的所有内容包装在功能中,则可以将可变变量传递到security(),并且可以正确计算它们。这是因为该函数是独立的,可以将其提取,而无需与图表上的主要符号有任何联系,并在security()内进行计算。

就您而言,这看起来像这样:

fun() =>
    SomethingHappened = (high > close) and (low < high) 
    SomethingHappenedInSeries = false
    for ArrayOfOffsets = 0 to k
        SomethingHappenedInSeries := SomethingHappenedInSeries and/or SomethingHappened[n+ArrayOfOffsets]
    SomethingHappenedInSeries
    
SomethingHappenedOnlyIn5Min = request.security(syminfo.tickerid, "5", fun())

只要您将所有内容包裹在一个可以安全地提取到不同上下文中的中,就可以在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 variable a can be 0 at the beginning and then get mutated to a := 1 based on the bar_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 request a from security() 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 the security().

In your case, this would look something like this:

fun() =>
    SomethingHappened = (high > close) and (low < high) 
    SomethingHappenedInSeries = false
    for ArrayOfOffsets = 0 to k
        SomethingHappenedInSeries := SomethingHappenedInSeries and/or SomethingHappened[n+ArrayOfOffsets]
    SomethingHappenedInSeries
    
SomethingHappenedOnlyIn5Min = request.security(syminfo.tickerid, "5", fun())

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文