在 Pine Editor TradingView 中对一组值进行排序
Rank a Set of Values in Pine Editor TradingView
我正在尝试根据 3 个值(EMA50、EMA100、EMA200)的排名设置标签行位置。是否有可以实现此功能的排名功能?尝试根据值大小在 (0,1,2) 下面设置 table 行。
即如果 EMA50 = 20、EMA100 = 15、EMA200 = 25 那么我希望它们在 table 行中排序为:
EMA100
均线50
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)
可能有更简单的方法,但我使用将不同的 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))
我正在尝试根据 3 个值(EMA50、EMA100、EMA200)的排名设置标签行位置。是否有可以实现此功能的排名功能?尝试根据值大小在 (0,1,2) 下面设置 table 行。
即如果 EMA50 = 20、EMA100 = 15、EMA200 = 25 那么我希望它们在 table 行中排序为:
EMA100 均线50 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)
可能有更简单的方法,但我使用将不同的 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))