How to compare arrays using PineScript?

49 Views Asked by At

I want to compare an array like this:

string_array = array.from("A", "B", "C", ...)
float_1_array = array.from(1.5, 2.5, 3.5, ...)
float_2_array = array.from(4.5, 5.5, 6.5, ...)

For a result meaning like this (following is not an actual code):

if array.get(float_1_array, 0) > 1 and
array.get(float_2_array, 0) > 4 ?
array.get(string_array, 0) : ""

string_result = "A"

How to achieve such a result using loop to display the string in a Table? Or is it possible in Pinescript?

1

There are 1 best solutions below

0
vitruvius On

Find the smallest array and set your loop based on that.

string_array = array.from("A", "B", "C")
float_1_array = array.from(1.5, 2.5, 3.5)
float_2_array = array.from(4.5, 5.5, 6.5)

len_string = array.size(string_array)
len_float_1 = array.size(float_1_array)
len_float_2 = array.size(float_2_array)

min_len = math.min(len_string, len_float_1, len_float_2)

if (barstate.islast)
    if (min_len > 0)
        for i = 0 to min_len - 1
            obj_string = array.get(string_array, i)
            obj_float_1 = array.get(float_1_array, i)
            obj_float_2 = array.get(float_2_array, i)
            
            if ((obj_float_1 > 1) and (obj_float_2 > 4) and (obj_string == "A"))
                label.new(bar_index, high, "Found")