簡體   English   中英

Tradingview Pinescript 使用 := 運算符

[英]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 個柱sum1開始一直到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM