简体   繁体   中英

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

I'm plotting pivot points on an indicator line (variable macdlz) and can plot a shape when they occur. I'd like to reference the past three pivot points for calculations and trendline drawing, and as they occur randomly the history referencing operator[] isn't going to work.

So I'm trying to write the pivots to two arrays(p-highs and p-lows) of three values as they come in and reference them later. I want to always have the last three pivot values stored and as new ones come in the oldest gets shifted out. I'm using a bgcolor to visually indicate a triple increase for testing in this case. My code doesn't seem to work as it doesn't produce any background color on the correct conditions. I am open to other solutions, I have never used arrays and I'm not sure what I'm doing.

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)

Special thanks to trior who answered on Discord, here is the correct code for anyone else looking to do this:

//@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM