簡體   English   中英

如何將 pivot 點保存到稍后在 pine-script 中引用的數組?

[英]How can I save pivot points to an array to be referenced later in pine-script?

我在指標線(變量 macdlz)上繪制 pivot 點,並且當它們發生時可以 plot 形狀。 我想參考過去三個 pivot 點進行計算和趨勢線繪制,因為它們隨機發生,所以歷史參考運算符 [] 不起作用。

因此,我嘗試將樞軸寫入三個值的兩個數組(p-highs 和 p-lows),因為它們進來並稍后引用它們。 我希望始終存儲最后三個 pivot 值,並且隨着新值的進入,最舊的值會被移出。 在這種情況下,我使用 bgcolor 直觀地指示測試的三倍增加。 我的代碼似乎不起作用,因為它在正確的條件下不會產生任何背景顏色。 我對其他解決方案持開放態度,我從未使用過 arrays 並且我不確定自己在做什么。

leftBars = input(6)
rightBars=input(3)
ph = pivothigh(macdlz, leftBars, rightBars)
pl = pivotlow(macdlz, leftBars, rightBars)
plotshape(ph > 0, style=shape.labeldown, color=#C2FFB4, location=location.top, offset=-rightBars)
plotshape(pl < 0, style=shape.labelup, color=#FF9898, location=location.bottom, offset=-rightBars) 

H = array.new_float(0)
L = array.new_float(0)
array.push(H, ph)
array.push(L, pl)
if (array.size(H) > 3)
    array.shift(H)
if (array.size(L) > 3)
    array.shift(L)    

val1 = array.get(H, 2)
val2 = array.get(H, 1)
val3 = array.get(H, 0)
increasing_three_highs = val1 > val2 and val2 > val3
bgcolor(increasing_three_highs ? color.green : na)

特別感謝在 Discord 上回答的 trior,這里是其他任何想要這樣做的人的正確代碼:

//@version=4
study("arrays test")
leftBars = input(6)
rightBars=input(3)

// I changed the macdlz to close I believe you can change it back
ph = pivothigh(close, leftBars, rightBars)
pl = pivotlow(close, leftBars, rightBars)

// You need the 'var' so that you don't get a new array at every bar
// You need the size to be at least 3 so when you call 'array.get(H, 2)' you don't get an error
var H = array.new_float(3)
var L = array.new_float(3)

// Notice that pivothigh and pivotlow return 'na' sometime you need to filter that
// No need to check the size of the arrays to shift them as the size is always 3
plot(ph)
plot(pl, color=color.red)

if not na(ph)
    array.push(H, ph)
    array.shift(H)
    
if not na(pl)
    array.push(L, pl)
    array.shift(L)

val1 = array.get(H, 2)
val2 = array.get(H, 1)
val3 = array.get(H, 0)

increasing_three_highs = val1 > val2 and val2 > val3
bgcolor(increasing_three_highs ? color.green : na)

// For debugging plot a label with arrays as string
if barstate.islast
    label.new(bar_index, 0, "Array H: " + tostring(H) + "\nArray L: " + tostring(L))
    
// Check the arrays Docs could be very helpfull 
// https://www.tradingview.com/pine-script-docs/en/v4/essential/Arrays.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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