3

我想了解如何 := 和 sum[1] 工作。这个总和返回 6093。但是总和是 0,也是 sum[1] = 0 ,对吗?它如何返回我 6093?我搜索了tradingview wiki,但我不明白。我想将此代码更改为另一种语言,例如 javascript 、 c#

testfu(x,y)=>
    sum = 0.0
    sum:= 1+ nz(sum[1])
    sum
4

1 回答 1

7

[]在 pine-script 中称为历史引用运算符。这样,就可以参考系列类型的任何变量的历史值(该变量在前一根柱线上的值)。因此,例如,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)

在此处输入图像描述

于 2018-10-20T11:40:14.900 回答