简体   繁体   English

如何将 pivot 点保存到稍后在 pine-script 中引用的数组?

[英]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.我在指标线(变量 macdlz)上绘制 pivot 点,并且当它们发生时可以 plot 形状。 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.我想参考过去三个 pivot 点进行计算和趋势线绘制,因为它们随机发生,所以历史参考运算符 [] 不起作用。

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.因此,我尝试将枢轴写入三个值的两个数组(p-highs 和 p-lows),因为它们进来并稍后引用它们。 I want to always have the last three pivot values stored and as new ones come in the oldest gets shifted out.我希望始终存储最后三个 pivot 值,并且随着新值的进入,最旧的值会被移出。 I'm using a bgcolor to visually indicate a triple increase for testing in this case.在这种情况下,我使用 bgcolor 直观地指示测试的三倍增加。 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.我对其他解决方案持开放态度,我从未使用过 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)

Special thanks to trior who answered on Discord, here is the correct code for anyone else looking to do this:特别感谢在 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