Я полный новичок в программировании и просто пытаюсь добавить визуальные эффекты в свою диаграмму, поэтому прошу прощения, если мой вопрос глупый...
Я попытался создать таблицу относительной силы секторов в сосне и теперь задавался вопросом, можно ли автоматически ранжировать эти сектора в таблице? Либо в порядке возрастания/убывания, либо число от 1 до 11 присваивается рядом с сектором в зависимости от его силы.
Я прикреплю скриншот и, надеюсь, то, что я прошу, понятно.
Извиняюсь за беспорядочный код ниже, я не удалял строки, которые не используются, и, как я уже сказал, я полный новичок. Код моей текущей таблицы:
indicator("Sector Relative Strength", overlay = true)
// Main logic
var _t = table.new (position.middle_left, 2, 12, frame_width = 5, border_width = 1)
var _t1 = table.new (position.bottom_right, 5, 2, frame_width = 2, border_width = 2)
// 6 month % momentum
lastMonthmomo = request.security(syminfo.tickerid, "W", close)
sixMonthsclosemomo = request.security(syminfo.tickerid, "W", close [26])
diffM6momo = sixMonthsclosemomo - lastMonthmomo
indTextM6momo = "M6: " + str.tostring((lastMonthmomo - sixMonthsclosemomo)/sixMonthsclosemomo *100, format.percent)
// 12 months % momentum
twelveMonthsclosemomo = request.security(syminfo.tickerid, "W", close [52])
diffM12momo = twelveMonthsclosemomo - lastMonthmomo
indTextM12momo = "M12: " + str.tostring((lastMonthmomo - twelveMonthsclosemomo)/twelveMonthsclosemomo *100, format.percent)
// XLP %
XLPlastMonth = request.security("XLP", "W", close)
twelveMonthsclose = request.security("XLP", "W", close [52])
diffM12 = twelveMonthsclose - XLPlastMonth
XLPindTextM12 = "XLP M12: " + str.tostring((XLPlastMonth - twelveMonthsclose)/twelveMonthsclose * 100, format.percent)
// XLB %
XLBlastMonth = request.security("XLB", "W", close)
XLBtwelveMonthsclose = request.security("XLB", "W", close [52])
XLBdiffM12 = XLBtwelveMonthsclose - XLBlastMonth
XLBindTextM12 = "XLB M12: " + str.tostring((XLBlastMonth - XLBtwelveMonthsclose)/XLBtwelveMonthsclose * 100, format.percent)
// XLE %
XLElastMonth = request.security("XLE", "W", close)
XLEtwelveMonthsclose = request.security("XLE", "W", close [52])
XLEdiffM12 = XLEtwelveMonthsclose - XLElastMonth
XLEindTextM12 = "XLE M12: " + str.tostring((XLElastMonth - XLEtwelveMonthsclose)/XLEtwelveMonthsclose * 100, format.percent)
// XLU %
XLUlastMonth = request.security("XLU", "W", close)
XLUtwelveMonthsclose = request.security("XLU", "W", close [52])
XLUdiffM12 = XLUtwelveMonthsclose - XLUlastMonth
XLUindTextM12 = "XLU M12: " + str.tostring((XLUlastMonth - XLUtwelveMonthsclose)/XLUtwelveMonthsclose * 100, format.percent)
// XLF %
XLFlastMonth = request.security("XLF", "W", close)
XLFtwelveMonthsclose = request.security("XLF", "W", close [52])
XLFdiffM12 = XLFtwelveMonthsclose - XLFlastMonth
XLFindTextM12 = "XLF M12: " + str.tostring((XLFlastMonth - XLFtwelveMonthsclose)/XLFtwelveMonthsclose * 100, format.percent)
// XLI %
XLIlastMonth = request.security("XLI", "W", close)
XLItwelveMonthsclose = request.security("XLI", "W", close [52])
XLIdiffM12 = XLItwelveMonthsclose - XLIlastMonth
XLIindTextM12 = "XLI M12: " + str.tostring((XLIlastMonth - XLItwelveMonthsclose)/XLItwelveMonthsclose * 100, format.percent)
// XLK %
XLKlastMonth = request.security("XLK", "W", close)
XLKtwelveMonthsclose = request.security("XLK", "W", close [52])
XLKdiffM12 = XLKtwelveMonthsclose - XLKlastMonth
XLKindTextM12 = "XLK M12: " + str.tostring((XLKlastMonth - XLKtwelveMonthsclose)/XLKtwelveMonthsclose * 100, format.percent)
// XLV %
XLVlastMonth = request.security("XLV", "W", close)
XLVtwelveMonthsclose = request.security("XLV", "W", close [52])
XLVdiffM12 = XLVtwelveMonthsclose - XLVlastMonth
XLVindTextM12 = "XLV M12: " + str.tostring((XLVlastMonth - XLVtwelveMonthsclose)/XLVtwelveMonthsclose * 100, format.percent)
// XLY %
XLYlastMonth = request.security("XLY", "W", close)
XLYtwelveMonthsclose = request.security("XLY", "W", close [52])
XLYdiffM12 = XLYtwelveMonthsclose - XLYlastMonth
XLYindTextM12 = "XLY M12: " + str.tostring((XLYlastMonth - XLYtwelveMonthsclose)/XLYtwelveMonthsclose * 100, format.percent)
// XLRE %
XLRElastMonth = request.security("XLRE", "W", close)
XLREtwelveMonthsclose = request.security("XLRE", "W", close [52])
XLREdiffM12 = XLREtwelveMonthsclose - XLRElastMonth
XLREindTextM12 = "XLRE M12: " + str.tostring((XLRElastMonth - XLREtwelveMonthsclose)/XLREtwelveMonthsclose * 100, format.percent)
// XLC %
XLClastMonth = request.security("XLC", "W", close)
XLCtwelveMonthsclose = request.security("XLC", "W", close [52])
XLCdiffM12 = XLCtwelveMonthsclose - XLClastMonth
XLCindTextM12 = "XLC M12: " + str.tostring((XLClastMonth - XLCtwelveMonthsclose)/XLCtwelveMonthsclose * 100, format.percent)
// Plot % change
table.cell(_t, 1, 0, XLPindTextM12, text_color = diffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 1, XLBindTextM12, text_color = XLBdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 2, XLEindTextM12, text_color = XLEdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 3, XLUindTextM12, text_color = XLUdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 4, XLFindTextM12, text_color = XLFdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 5, XLIindTextM12, text_color = XLIdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 6, XLKindTextM12, text_color = XLKdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 7, XLVindTextM12, text_color = XLVdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 8, XLYindTextM12, text_color = XLYdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 9, XLREindTextM12, text_color = XLREdiffM12 > 0 ? color.red : color.green, text_size = size.small)
table.cell(_t, 1, 10, XLCindTextM12, text_color = XLCdiffM12 > 0 ? color.red : color.green, text_size = size.small)
Конечно, я отредактирую свой пост, чтобы включить полный код. Вы должны извинить меня за беспорядочный код, я не удалял строки, которые не используются, и, как я уже сказал, я новичок.
table.cell
вызовы довольно интенсивны, лучше всего создать ячейки один раз, а затем изменять их по мере необходимости.
При получении нескольких значений из одного и того же тикера нет необходимости использовать несколько вызовов безопасности. Существует ограничение в 40 вызовов службы безопасности, и это также интенсивная операция. Несколько значений могут быть получены с помощью кортежей, т.е.:
[a, b] = request.security(ticker, res, [a, b])
При сортировке значений мы сначала вставляем их в существующем порядке в массивы, где значения title, diff и perc соответствуют индексу массива.
В этом методе мы используем пузырьковую сортировку, используя массив perc для определения порядка. Если значение необходимо переместить, мы также одновременно перемещаем соответствующие значения из массива diff и title, чтобы сохранить соответствующие значения perc/diff/title в одной и той же позиции в массивах.
//@version=5
indicator("Sector Relative Strength", overlay = true)
// Main logic
var _t = table.new (position.middle_left, 2, 12, frame_width = 5, border_width = 1)
var _t1 = table.new (position.bottom_right, 5, 2, frame_width = 2, border_width = 2)
// 6 month % momentum
[lastMonthmomo, sixMonthsclosemomo, twelveMonthsclosemomo] = request.security(syminfo.tickerid, "W", [ close, close [26], close [52] ])
diffM6momo = sixMonthsclosemomo - lastMonthmomo
indTextM6momo = "M6: " + str.tostring((lastMonthmomo - sixMonthsclosemomo)/sixMonthsclosemomo *100, format.percent)
// 12 months % momentum
diffM12momo = twelveMonthsclosemomo - lastMonthmomo
percM12momo = (lastMonthmomo - twelveMonthsclosemomo)/twelveMonthsclosemomo *100
indTextM12momo = "M12: " + str.tostring(percM12momo, format.percent)
// XLP %
[XLPlastMonth, twelveMonthsclose] = request.security("XLP", "W", [ close, close [52] ])
XLPdiffM12 = twelveMonthsclose - XLPlastMonth
XLPpercM12 = (XLPlastMonth - twelveMonthsclose)/twelveMonthsclose * 100
// XLB %
[XLBlastMonth, XLBtwelveMonthsclose] = request.security("XLB", "W", [ close, close [52] ])
XLBdiffM12 = XLBtwelveMonthsclose - XLBlastMonth
XLBpercM12 = (XLBlastMonth - XLBtwelveMonthsclose)/XLBtwelveMonthsclose * 100
// XLE %
[XLElastMonth, XLEtwelveMonthsclose] = request.security("XLE", "W", [ close, close [52] ])
XLEdiffM12 = XLEtwelveMonthsclose - XLElastMonth
XLEpercM12 = (XLElastMonth - XLEtwelveMonthsclose)/XLEtwelveMonthsclose * 100
// XLU %
[XLUlastMonth, XLUtwelveMonthsclose] = request.security("XLU", "W", [ close, close [52] ])
XLUdiffM12 = XLUtwelveMonthsclose - XLUlastMonth
XLUpercM12 = (XLUlastMonth - XLUtwelveMonthsclose)/XLUtwelveMonthsclose * 100
// XLF %
[XLFlastMonth, XLFtwelveMonthsclose] = request.security("XLF", "W", [ close, close [52] ])
XLFdiffM12 = XLFtwelveMonthsclose - XLFlastMonth
XLFpercM12 = (XLFlastMonth - XLFtwelveMonthsclose)/XLFtwelveMonthsclose * 100
// XLI %
[XLIlastMonth, XLItwelveMonthsclose] = request.security("XLI", "W", [ close, close [52] ])
XLIdiffM12 = XLItwelveMonthsclose - XLIlastMonth
XLIpercM12 = (XLIlastMonth - XLItwelveMonthsclose)/XLItwelveMonthsclose * 100
// XLK %
[XLKlastMonth, XLKtwelveMonthsclose] = request.security("XLK", "W", [ close, close [52] ])
XLKdiffM12 = XLKtwelveMonthsclose - XLKlastMonth
XLKpercM12 = (XLKlastMonth - XLKtwelveMonthsclose)/XLKtwelveMonthsclose * 100
// XLV %
[XLVlastMonth, XLVtwelveMonthsclose] = request.security("XLV", "W", [ close, close [52] ])
XLVdiffM12 = XLVtwelveMonthsclose - XLVlastMonth
XLVpercM12 = (XLVlastMonth - XLVtwelveMonthsclose)/XLVtwelveMonthsclose * 100
// XLY %
[XLYlastMonth, XLYtwelveMonthsclose] = request.security("XLY", "W", [ close, close [52] ])
XLYdiffM12 = XLYtwelveMonthsclose - XLYlastMonth
XLYpercM12 = (XLYlastMonth - XLYtwelveMonthsclose)/XLYtwelveMonthsclose * 100
// XLRE %
[XLRElastMonth, XLREtwelveMonthsclose] = request.security("XLRE", "W", [ close, close [52] ])
XLREdiffM12 = XLREtwelveMonthsclose - XLRElastMonth
XLREpercM12 = (XLRElastMonth - XLREtwelveMonthsclose)/XLREtwelveMonthsclose * 100
// XLC %
[XLClastMonth, XLCtwelveMonthsclose] = request.security("XLC", "W", [ close, close [52] ])
XLCdiffM12 = XLCtwelveMonthsclose - XLClastMonth
XLCpercM12 = (XLClastMonth - XLCtwelveMonthsclose)/XLCtwelveMonthsclose * 100
var string[] M12_titles = array.from("XLP M12: ", "XLB M12: ", "XLE M12: ", "XLU M12: ", "XLF M12: ", "XLI M12: ", "XLK M12: ", "XLV M12: ", "XLY M12: ", "XLRE M12: ", "XLC M12: ")
float[] M12_diff_vals = array.from(XLPdiffM12, XLBdiffM12, XLEdiffM12, XLUdiffM12, XLFdiffM12, XLIdiffM12, XLKdiffM12, XLVdiffM12, XLYdiffM12, XLREdiffM12, XLCdiffM12)
float[] M12_perc_vals = array.from(XLPpercM12, XLBpercM12, XLEpercM12, XLUpercM12, XLFpercM12, XLIpercM12, XLKpercM12, XLVpercM12, XLYpercM12, XLREpercM12, XLCpercM12)
f_linked_bubble_sort(_indexing_array, _linked_array1, _linked_array2) =>
bool _change_found = true
_i_array = array.copy(_indexing_array)
_l_array1 = array.copy(_linked_array1)
_l_array2 = array.copy(_linked_array2)
_n = array.size(_i_array)
if _n >= 2 and _n == array.size(_l_array1) and _n == array.size(_l_array2)
while _change_found == true
_changed = false
for _i = 0 to _n - 2
_i_val1 = array.get(_i_array, _i)
_i_val2 = array.get(_i_array, _i + 1)
_l1_val1 = array.get(_l_array1, _i)
_l1_val2 = array.get(_l_array1, _i + 1)
_l2_val1 = array.get(_l_array2, _i)
_l2_val2 = array.get(_l_array2, _i + 1)
if _i_val1 < _i_val2
array.set(_i_array, _i, _i_val2)
array.set(_l_array1, _i, _l1_val2)
array.set(_l_array2, _i, _l2_val2)
array.set(_i_array,_i + 1, _i_val1)
array.set(_l_array1, _i + 1, _l1_val1)
array.set(_l_array2, _i + 1, _l2_val1)
_changed := true
if _changed == false
_change_found := false
[_i_array, _l_array1, _l_array2]
[sorted_M12_perc_vals, sorted_M12_titles, sorted_M12_diff_vals] = f_linked_bubble_sort(M12_perc_vals, M12_titles, M12_diff_vals)
if barstate.isfirst
for x = 0 to 1
for y = 0 to 10
table.cell(table_id = _t, column = x, row = y, text_size = size.small, text_color = color.blue)
if barstate.islast
for y = 0 to 10
title_text = array.get(sorted_M12_titles, y)
perc_text = str.tostring(array.get(sorted_M12_perc_vals, y), format.percent)
diff = array.get(sorted_M12_diff_vals, y)
diff_color = diff > 0 ? color.red : color.green
table.cell_set_text(table_id = _t, column = 0, row = y, text = title_text)
table.cell_set_text(table_id = _t, column = 1, row = y, text = perc_text)
table.cell_set_text_color(table_id = _t, column = 1, row = y, text_color = diff_color)```
Таблицы не имеют возможности сортировать свое содержимое напрямую. Было бы лучше, если бы вы предоставили компилируемый код, чтобы мы могли видеть, как управлять вашими переменными, поскольку их необходимо вставлять в массивы для сортировки.