代码之家  ›  专栏  ›  技术社区  ›  Emre Ekşioğlu

Tradingview脚本使用:=运算符

  •  -1
  • Emre Ekşioğlu  · 技术社区  · 7 年前

    我想了解如何:=和[1]相加。这个和返回我6093。但是和是0,也就是和[1]=0,对吗?它怎么还我6093?我搜索了tradingview维基,但我不明白。我想把这段代码改成另一种语言,比如javascript,c#

    testfu(x,y)=>
        sum = 0.0
        sum:= 1+ nz(sum[1])
        sum
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   vitruvius    7 年前

    [] 在松树的脚本中称为 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)
    

    enter image description here