TV_123
TV_123

Reputation: 1

Can Pine Script v6 do this? How to plot a line from array values correctly

A very simple attempt to plot a line:

  1. I sorted 10 (arbitrary window size) close prices in an array.
  2. I have displayed the values in a table and labels.
  3. Now I want to plot a line of the ranks (1-10) in a pane.

I am unable to plot the values in a line in the correct order, i.e., the ranking differs from that correctly shown in the table/labels.

Perhaps it is due to how Pine Script calculates the candles, and I simply cannot make the correct connection to it.

Any help with a solution or a smart workaround is highly appreciated.

Here is the some code:

lookBack        = input.int(10, 'Lookback Period')
src             = input.source(close, "Input source")

var array<float> priceArray     = array.new_float(lookBack)
var array<int>   rankArray      = array.new_int(lookBack)
var array<label> labelArray     = array.new<label>()

var array<int>   timeArray      = array.new_int(0)      // Store timestamps
var array<float> rankValues     = array.new_float(0)    // Store corresponding ranks


size = array.size(labelArray)
if size > 0
    for i = 0 to size - 1
        label.delete(array.get(labelArray, i))
    array.clear(labelArray)


// Initialize start timestamp when enough bars
var int start_ts = na

if bar_index == lookBack
    start_ts := time[lookBack - 1] // Start from the first bar in lookback window
    start_ts

if bar_index >= lookBack
    array.clear(rankArray)
    array.clear(priceArray)
    
    // Fill price array
    for i = 0 to lookBack - 1
        array.push(priceArray, src[i])
        array.push(rankArray, 0)

    // Get sorted indices
    int[] sortedIndices = array.sort_indices(priceArray, order.ascending)

    // Assign ranks based on sorted order
    for i = 0 to lookBack - 1
        array.set(rankArray, array.get(sortedIndices, i), i + 1)

    // Get current bar's rank
    int currentIndex = array.indexof(priceArray, src[0])
    if currentIndex != -1
        float currentRank = float(array.get(rankArray, currentIndex))
        
        // Store rank and timestamp
        array.push(rankValues, currentRank)
        array.push(timeArray, time)

    // Create labels
    for i = 0 to lookBack - 1 by 1
        float price = array.get(priceArray, i)
        int rank = array.get(rankArray, i)

        newLabel = label.new(bar_index - i, price, text = 'Price: ' + str.tostring(price, '#.##') + '\nRank: ' + str.tostring(rank), color = color.white, textcolor = color.black, size = size.small, style = label.style_label_down)
        array.push(labelArray, newLabel)



// Index for maintaining position in arrays
var int index = na

if time > start_ts
    // Debug indexof operation
    int found_index = array.indexof(timeArray, time)
    index := found_index
    index

// Plotting 

line_plot = not na(start_ts) and time >= start_ts and index <= array.size(rankValues) - 1

plot(line_plot ? array.get(rankValues, array.indexof(timeArray, time)) : na, title = 'Ranks', color = color.orange, linewidth = 2)

Upvotes: 0

Views: 44

Answers (0)

Related Questions