Search code examples
pine-scriptpine-script-v5

Why does this code NOT add values to the array?


I try to collect Support/Resistance levels into an array but my code bele never adds data to the array so its size always remains 0. What do I do wrong? (Of course, there is another similar code for lowest values, with different length sizes, the code is simplified for you.)

upper = ta.highest(50) 
prec = 2 // its GBPUSD btw.

var SupportResistance = array.new_float() 

if math.round(upper[0], prec) < math.round(upper[1], prec)
    if array.size(SupportResistance) == 0 // first data to the array, how could its size be zero?
        array.push(SupportResistance, math.round(upper[1], prec))
    else 
        for i = 0 to array.size(SupportResistance) - 1
            if math.round(upper[1], prec) != array.get(SupportResistance, i) // no duplications
                array.push(SupportResistance, math.round(upper[1], prec))

Solution

  • try this:

    //@version=5
    indicator("My script")
    
    upper = ta.highest(50) 
    prec = 2 // its GBPUSD btw.
    
    var SupportResistance = array.new_float() 
    
    if math.round(upper[0], prec) < math.round(upper[1], prec)
        if not array.includes(SupportResistance, math.round(upper[1], prec)) 
            SupportResistance.push(math.round(upper[1], prec))
    
    plot(SupportResistance.size())