【问题标题】:How can I save pivot points to an array to be referenced later in pine-script?如何将枢轴点保存到稍后在 pine-script 中引用的数组?
【发布时间】:2021-06-12 08:44:10
【问题描述】:

我在指标线上(变量 macdlz)绘制轴心点,并且可以在它们出现时绘制形状。我想参考过去的三个枢轴点进行计算和绘制趋势线,因为它们是随机发生的,所以历史参考运算符 [] 不起作用。

所以我试图将枢轴写入三个值的两个数组(p-highs 和 p-lows),因为它们进来并稍后引用它们。我希望始终存储最后三个枢轴值,并且随着新值的进入,最旧的值会被移出。在这种情况下,我使用 bgcolor 直观地指示测试的三倍增加。我的代码似乎不起作用,因为它在正确的条件下不会产生任何背景颜色。我对其他解决方案持开放态度,我从未使用过数组,我不确定我在做什么。

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)

【问题讨论】:

    标签: arrays pine-script


    【解决方案1】:

    特别感谢在 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
    

    【讨论】:

      猜你喜欢
      • 2020-07-26
      • 2020-08-03
      • 1970-01-01
      • 1970-01-01
      • 2016-06-07
      • 2018-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多