在Pine中查找第N高/低的值

huangapple go评论51阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年6月26日 16:01:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定