在Pine Editor TradingView中排名一组值

发布于 2025-01-21 18:56:24 字数 533 浏览 2 评论 0原文

我试图根据3个值的排名(EMA50,EMA100,EMA200)设置标签行位置。是否有可以实现这一目标的等级函数?尝试根据值大小设置下面的表行(0,1,2)。

IE如果EMA50 = 20,EMA100 = 15,EMA200 = 25,那么我希望它们在表行中排序为:

EMA100 EMA50 EMA200

if barstate.islast
    txt0 = "EMA50"
    txt01 = "EMA100"
    txt02 = "EMA200"
    table.cell(myTable2,0,0,text=txt0,bgcolor=color.black,text_color= color.white)
    table.cell(myTable2,0,1,text=txt01,bgcolor=color.black,text_color= color.white)
    table.cell(myTable2,0,2,text=txt01,bgcolor=color.black,text_color= color.white)

I am trying to set the label row position based on the ranking of 3 values (EMA50,EMA100, EMA200). Is there a rank function that can accomplish this? Trying to set a table row below (0,1,2) based on value size.

i.e. if EMA50 = 20, EMA100 = 15, EMA200 = 25 then I would want them sorted in the table row as:

EMA100
EMA50
EMA200

if barstate.islast
    txt0 = "EMA50"
    txt01 = "EMA100"
    txt02 = "EMA200"
    table.cell(myTable2,0,0,text=txt0,bgcolor=color.black,text_color= color.white)
    table.cell(myTable2,0,1,text=txt01,bgcolor=color.black,text_color= color.white)
    table.cell(myTable2,0,2,text=txt01,bgcolor=color.black,text_color= color.white)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疧_╮線 2025-01-28 18:56:24

可能会有更轻松的方法,但是我使用不同的EMA将不同的EMA附加到数组,而不是对数组进行排序。
这是一个示例代码:

//@version=5
indicator("My script", overlay = true)

//create EMA lines 
ema1 = ta.ema(close, 50) 
ema2 = ta.ema(close, 100) 
ema3 = ta.ema(close, 200)

// color of each EMA lines 
ema1_color = color.purple 
ema2_color = color.yellow 
ema3_color = color.green

// plot each EMA lines 
plot(ema1, "ema 50", color=ema1_color) 
plot(ema2, "ema 100", color=ema2_color) 
plot(ema3, "ema 200", color=ema3_color)

// create arrays of EMA value+name+color 
a = array.new_float(3) 
text_array = array.new_string(3) 
color_array = array.new_color(3)

// create a blank table with enough space for all EMA 
t = table.new(position = position.top_right, columns = 2, rows = 4, frame_width = 2, frame_color=color.black)

if barstate.islast

    // set the EMA to the array
    array.set(a, 0, ema1)
    array.set(a, 1, ema2)
    array.set(a, 2, ema3)

    // sort the EMA by value
    array.sort(a, order.ascending)
    
    //get the new index of each EMA
    ema1_index = array.indexof(a, ema1)
    ema2_index = array.indexof(a, ema2)
    ema3_index = array.indexof(a, ema3)
    
    // set a name with the same index above
    array.set(text_array, ema1_index, "EMA 50")
    array.set(text_array, ema2_index, "EMA 100")
    array.set(text_array, ema3_index, "EMA 200")
        
    // set a color with the same index above
    array.set(color_array, ema1_index, ema1_color)
    array.set(color_array, ema2_index, ema2_color)
    array.set(color_array, ema3_index, ema3_color)
    
    // set the cells for the table
    table.cell(table_id = t, column = 0, row = 1, text = str.tostring(array.get(text_array, 0)), bgcolor=color.white, text_color=array.get(color_array, 0))
    table.cell(table_id = t, column = 0, row = 2, text = str.tostring(array.get(text_array, 1)), bgcolor=color.white, text_color=array.get(color_array, 1))
    table.cell(table_id = t, column = 0, row = 3, text = str.tostring(array.get(text_array, 2)), bgcolor=color.white, text_color=array.get(color_array, 2))
    table.cell(table_id = t, column = 1, row = 1, text = str.tostring(array.get(a, 0)), bgcolor=color.white, text_color=array.get(color_array, 0))
    table.cell(table_id = t, column = 1, row = 2, text = str.tostring(array.get(a, 1)), bgcolor=color.white, text_color=array.get(color_array, 1))
    table.cell(table_id = t, column = 1, row = 3, text = str.tostring(array.get(a, 2)), bgcolor=color.white, text_color=array.get(color_array, 2))

There might be easier way, but I use append the different EMA to an array and than sort the array.
Here is an example code:

//@version=5
indicator("My script", overlay = true)

//create EMA lines 
ema1 = ta.ema(close, 50) 
ema2 = ta.ema(close, 100) 
ema3 = ta.ema(close, 200)

// color of each EMA lines 
ema1_color = color.purple 
ema2_color = color.yellow 
ema3_color = color.green

// plot each EMA lines 
plot(ema1, "ema 50", color=ema1_color) 
plot(ema2, "ema 100", color=ema2_color) 
plot(ema3, "ema 200", color=ema3_color)

// create arrays of EMA value+name+color 
a = array.new_float(3) 
text_array = array.new_string(3) 
color_array = array.new_color(3)

// create a blank table with enough space for all EMA 
t = table.new(position = position.top_right, columns = 2, rows = 4, frame_width = 2, frame_color=color.black)

if barstate.islast

    // set the EMA to the array
    array.set(a, 0, ema1)
    array.set(a, 1, ema2)
    array.set(a, 2, ema3)

    // sort the EMA by value
    array.sort(a, order.ascending)
    
    //get the new index of each EMA
    ema1_index = array.indexof(a, ema1)
    ema2_index = array.indexof(a, ema2)
    ema3_index = array.indexof(a, ema3)
    
    // set a name with the same index above
    array.set(text_array, ema1_index, "EMA 50")
    array.set(text_array, ema2_index, "EMA 100")
    array.set(text_array, ema3_index, "EMA 200")
        
    // set a color with the same index above
    array.set(color_array, ema1_index, ema1_color)
    array.set(color_array, ema2_index, ema2_color)
    array.set(color_array, ema3_index, ema3_color)
    
    // set the cells for the table
    table.cell(table_id = t, column = 0, row = 1, text = str.tostring(array.get(text_array, 0)), bgcolor=color.white, text_color=array.get(color_array, 0))
    table.cell(table_id = t, column = 0, row = 2, text = str.tostring(array.get(text_array, 1)), bgcolor=color.white, text_color=array.get(color_array, 1))
    table.cell(table_id = t, column = 0, row = 3, text = str.tostring(array.get(text_array, 2)), bgcolor=color.white, text_color=array.get(color_array, 2))
    table.cell(table_id = t, column = 1, row = 1, text = str.tostring(array.get(a, 0)), bgcolor=color.white, text_color=array.get(color_array, 0))
    table.cell(table_id = t, column = 1, row = 2, text = str.tostring(array.get(a, 1)), bgcolor=color.white, text_color=array.get(color_array, 1))
    table.cell(table_id = t, column = 1, row = 3, text = str.tostring(array.get(a, 2)), bgcolor=color.white, text_color=array.get(color_array, 2))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文