Plot an array of labels

I would like to plot around 20 labels on a chart. I'll get the data in a CSV format. It will contain price, date and an id.

So far I figured out how to plot a single label containing data from three arrays but I'm not sure how to loop through an array. Also, I couldn't figure out how to have cleaner looking arrays with the data.

Here is what I have so far.

//@version=4
study(title="My study", overlay=true)

// Make a label once (as preparation)
var float[] prices = array.new_float(2)
var string[] ids = array.new_string(2)
var int[] date = array.new_int(2)

array.set(prices, 0, 8)
array.set(prices, 1, 6)
array.set(ids, 0, "CF442W")
array.set(ids, 1, "WI211KK")
array.set(date, 0, 1641774575000)
array.set(date, 1, 1621772575000)

var monthLabel = label.new(x=na, y=na, xloc=xloc.bar_time, color=color.black, textcolor=color.white)
var monthLabel1 = label.new(x=na, y=na, xloc=xloc.bar_time, color=color.black, textcolor=color.white)

// // Update the label on the chart's last bar
if (barstate.islast)
    labelText = "TRADE: " + array.get(ids, 0) + "\n\n BUY: " + tostring(array.get(prices, 0))

    label.set_y(id=monthLabel, y=array.get(prices, 0))
    label.set_x(id=monthLabel, x=array.get(date, 0))

    label.set_text(id=monthLabel, text=labelText)

if (barstate.islast)
    labelText1 = "TRADE: " + array.get(ids, 1) + "\n\n BUY: " + tostring(array.get(prices, 1))

    label.set_y(id=monthLabel1, y=array.get(prices, 1))
    label.set_x(id=monthLabel1, x=array.get(date, 1))

    label.set_text(id=monthLabel1, text=labelText1)

enter image description here


This will make it clear. I've also converted it to Pine script v5 while I was at it.

//@version=5
indicator("My study", max_labels_count=500, overlay=true)

// Define arrays of undefined size, so you can add many datapoints
var float[]     prices  = array.new_float()
var string[]    ids     = array.new_string()
var int[]       date    = array.new_int()

// Function to add data to the arrays
f_data(_price, _id, _date) =>
    array.push(prices, _price)
    array.push(ids, _id)
    array.push(date, _date)

// Function to create a label
f_label(_idx) => 
    labelText = "TRADE: " + array.get(ids, _idx) + "\n\n BUY: " + str.tostring(array.get(prices, _idx))
    label.new(array.get(date, _idx), array.get(prices, _idx), labelText, xloc.bar_time, yloc.belowbar, color=color.red, textcolor=color.white, style=label.style_label_up)

// Enter data into the arrays on the first bar only. 
// No need to do it on every bar (for performance).
if barstate.isfirst
    // You don't need to use the epoch time integer, you can also use the timestamp() function
    f_data(8, 'CF442W',  timestamp(2022, 01, 10, 0, 0, 0))
    f_data(6, 'WI211KK', timestamp(2021, 05, 23, 0, 0, 0))

// Create the labels on the chart's last bar
if barstate.islast
    for i = 0 to array.size(ids) - 1
        f_label(i)