英文:
Find Nth highest/lowest values in Pine
问题
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.
英文:
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!
答案1
得分: 0
array.sort()
函数返回一个无法赋值给变量(ranked_ratio)的 void 表达式。
> array.sort(id, order) → void
只需调用 sort 函数并引用排序后的数组在 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)
英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论