如何从上一个bar访问功能的状态
禁止修改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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
虽然您无法从内部函数修改全局范围变量是正确的,但是您可以从函数内部修改全球声明的数组的元素。
由于数组是使用
var
声明的,因此它是持久的,因此不会在每个栏上重新定位。因此,连续的增量不仅保留在同一栏上(我们在这里的每个栏上递增了3次),而且还保留了跨栏: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: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]