[英]Tradingview Pinescript work with the := operator
我想了解如何 := 和 sum[1] 工作。 这个总和返回给我 6093。但是 sum 是 0,也是 sum[1] = 0 ,对吗? 它如何返回我 6093? 我搜索了tradeview wiki,但我不明白。 我想将此代码更改为另一种语言,例如 javascript 、 c#
testfu(x,y)=>
sum = 0.0
sum:= 1+ nz(sum[1])
sum
[]
在 pine-script 中称为History Referencing Operator 。 这样,就可以参考系列类型的任何变量的历史值(变量在先前柱上的值)。 因此,例如, close[1]
返回昨天的收盘价 - 这也是一个系列。
因此,如果我们分解您的代码(从第一个栏开始):
testfu(x,y)=>
sum = 0.0 // You set sum to 0.0
sum:= 1+ nz(sum[1]) // You add 1 to whatever value sum had one bar ago
// which is 0, because it's the first bar (no previous value)
sum // Your function returns 1 + 0 = 1 for the very first bar
现在,对于第二个酒吧:
testfu(x,y)=>
sum = 0.0 // You set sum to 0.0
sum:= 1+ nz(sum[1]) // You add 1 to whatever value sum had one bar ago
// which is 1, because it was set to 1 for the first bar
sum // Your function now returns 1 + 1 = 2 for the second bar
等等。
看看下面的代码和图表。 该图表有62 个柱, sum
从1
开始一直到62
。
//@version=3
study("My Script", overlay=false)
foo() =>
sum = 0.0
sum:= 1 + nz(sum[1])
sum
plot(series=foo(), title="sum", color=red, linewidth=4)
要回答部分有关:=
运算符的问题:
它将新值分配给已设置的变量。 您可以用任何其他语言将其替换为单个=
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.