How to compare Array Elements by greater than?

40 Views Asked by At

I want to create Pinescript Indicator, which compares Three Swings by Price Units and Time Units. There is Swing 1's Price and Time Units, Swing 2's Price and Time Units, Swing 3's Price and Time Units.

I have created Swing Price Units Array and Swing Time Unit Array as follows.

var Swing_PriceUnits = array.new_float(na)
var Swing_TimeUnits = array.new_int(na)

if SwingPresent and array.size(Swing_PriceUnits) > 3 and array.size(Swing_TimeUnits) > 3 
    array.shift(Swing_PriceUnits)
    array.shift(Swing_TimeUnits)

if SwingPresent
    array.push(Swing_PriceUnits, ML_Swing_PUs)
    array.push(Swing_TimeUnits, ML_Swing_TUs)
    
//Compare the Value of Array Element.

I want to compare the elemnts within array and get result like follow.

Swing_PriceUnits [53, 92, 93] that 3rd element is greter than second and second element is greater than first. Swing_TimeUnits [2, 4, 1] that second element is greter than first and first element is greater than third.

1

There are 1 best solutions below

1
vitruvius On

You can do this by using the array.get() function. Before accessing the elements of an array, you should check the array's size. And then just compare the elements.

price_len = array.size(Swing_PriceUnits)
price_cond = false

if (price_len >= 3)
    first_elem = array.get(Swing_PriceUnits, 0)
    second_elem = array.get(Swing_PriceUnits, 1)
    third_elem = array.get(Swing_PriceUnits, 2)

    price_cond := (third_elem > second_elem) and (second_elem > first_elem)

// Use price_cond later in code