在Pinescript TradingView中分离元组值和分配



我试图在当前图表的条形(15分钟)内显示栏内关闭值。所以有三个值。但是这些值是元组。我不能把元组分成三个值。我尝试了下面的代码以及使用for循环(我不熟悉),

[fir, sec, thi] = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
table.cell(top_boxes, 0, 0, text=str.tostring(a), bgcolor=color.new(color.blue, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(b), bgcolor=color.new(color.red, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(c), bgcolor=color.new(color.yellow, 0), text_color=color.white, width=4, height=8)

有人能帮我把数组中的值分开并显示出来吗

由于您只请求closeprice,因此request.security_lower_tf()函数返回一个包含3个元素的数组。

您可以使用array.get():

获取每个元素的值
close_5 = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
if barstate.islast
table.cell(top_boxes, 0, 0, text=str.tostring(array.get(close_5, 0)), bgcolor=color.new(color.blue, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 1, 0, text=str.tostring(array.get(close_5, 1)), bgcolor=color.new(color.red, 0), text_color=color.white, width=4, height=8)
table.cell(top_boxes, 2, 0, text=str.tostring(array.get(close_5, 2)), bgcolor=color.new(color.yellow, 0), text_color=color.white, width=4, height=8)

在某些情况下,为了将3个元素返回到该数组中,将会丢失一些5分钟栏。你可以写一个"更安全"使用for循环的代码:

//@version=5
indicator("request 5 min timeframe", overlay = true)
close_5 = request.security_lower_tf(syminfo.tickerid, "5", close)
var table top_boxes = table.new(position.bottom_center, 6, 2)
if barstate.islast
color[] cell_bg_color = array.from(color.blue, color.red, color.yellow)
for [index, price] in close_5
table.cell(top_boxes, index, 0, text=str.tostring(price), bgcolor=array.get(cell_bg_color, index), text_color=color.white)

最新更新