如何从上一个bar访问功能的状态

发布于 2025-01-24 09:39:21 字数 523 浏览 0 评论 0原文

禁止修改Pinescript中的全球状态。像这样:

globalVar = 0

function() =>
    globalVar := 1

如果您不能在全局范围中持续存在,我如何访问i功能中最后一个迭代(最后一个栏)的值?

在其他语言中,一个选项是将值从函数返回并将其馈送到下一个bar中的函数调用中,

increment(i, modified) =>
    modified2 = nz(modified, i) + 1
    [i, modified2]
state = increment(0, array.get(state[1], 1))

但是,这是因为state无效,因此失败了。这是有道理的,因为在定义之前无法读取它。但另一方面,在最后一个bar 状态[1]中定义了它。

那么,如何从函数范围从最后一个栏中访问修改变量以增加

Modifying global state in PineScript is prohibited. Like so:

globalVar = 0

function() =>
    globalVar := 1

So how do i access a value which was calcualted the last iteration (the last bar) within i function if you can not persist it in global scope?

In other languages an option would be to return the values from the function and feed them to the function call in the next bar, like so:

increment(i, modified) =>
    modified2 = nz(modified, i) + 1
    [i, modified2]
state = increment(0, array.get(state[1], 1))

However, this fails because state is not valid. Which makes sense since it can not be read before it was defined. But on the other hand in the last bar state[1] it was defined.

So how would I access the modified variable from the function's scope from the last bar to increment it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

白昼 2025-01-31 09:39:21

虽然您无法从内部函数修改全局范围变量是正确的,但是您可以从函数内部修改全球声明的数组的元素。

由于数组是使用var声明的,因此它是持久的,因此不会在每个栏上重新定位。因此,连续的增量不仅保留在同一栏上(我们在这里的每个栏上递增了3次),而且还保留了跨栏:

//@version=5
indicator("")
// Create an array of one element of value zero.
var globalArray = array.from(0)
// Increase the array's element by 1 from inside the function.
increment(a) =>
    newValue = array.get(a, 0) + 1
    array.set(a, 0, newValue)
    newValue

plot(increment(globalArray))
plot(increment(globalArray))
plot(increment(globalArray))

While it is correct that you cannot modify global scope variables from inside functions, you can modify elements of a globally declared array from inside a function.

Because the array is declared using var it is persistent, so it does not re-initialize on each bar. The successive increments are thus preserved not only on the same bar (we increment three times on each bar here), but across bars also:

//@version=5
indicator("")
// Create an array of one element of value zero.
var globalArray = array.from(0)
// Increase the array's element by 1 from inside the function.
increment(a) =>
    newValue = array.get(a, 0) + 1
    array.set(a, 0, newValue)
    newValue

plot(increment(globalArray))
plot(increment(globalArray))
plot(increment(globalArray))
橘亓 2025-01-31 09:39:21

Pine脚本中没有数组。他们以系列的形式工作。

简单地致电最后一个关闭:
last_close =关闭[1]
2nd_last_close =关闭[2]

there is no array in pine script. they work in the form of series.

for simply calling the last close:
last_close = close[1]
2nd_last_close = close[2]

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