I'm trying to find the Nth highest/lowest value of a ratio within Pine v5. This is ultimately part of a process to try and determine the top/bottom 1% values of a ratio to find extreme outliers. My current thought process is to store all ratio values within an array, sort and find the relevant ID based on the total bars from the bar_index; but I'm experiencing errors and feel I may be going the wrong route...
Here's my current code, replaced the ratio I'm using with just some generic place holder:
ratio = high / low
n_bar = 10
length = bar_index + 1
ratio_array = array.new_float(length)
for i = 0 to length - 1
array.push(ratio_array, ratio)
ranked_ratio = array.sort(ratio_array, order.ascending)
tenth_highest = array.get(ranked_ratio, n_bar)
Currently getting a 'Void expression cannot be assigned to a variable' error.
Any help would be greatly appreciated, thanks in advance!
array.sort()
function returns a void expression that cannot be assigned to a variable (ranked_ratio).
array.sort(id, order) → void
Simply call the sort function and reference the sorted array in the array.get()
:
for i = 0 to length - 1
array.push(ratio_array, ratio)
array.sort(ratio_array, order.ascending)
tenth_highest = ratio_array.size() > n_bar ? array.get(ratio_array, n_bar) : 0
plot(tenth_highest)