Home > other >  Separate Tuples Values and Assign in Pinescript TradingView
Separate Tuples Values and Assign in Pinescript TradingView

Time:11-22

I was trying to display intrabar close values inside the current chart's bar(15 min). So there are three values. But the values comes as tuple. I cannot separate the tuples to three values. I have tried the below code as well as using for loop(which I am not familiar with),

[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)

Can someone help me to separate the values in the array and display them.

CodePudding user response:

Since you only request close price, request.security_lower_tf() function returns an array with 3 elements.

You can get the value of each element using 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)

In some cases, there will be missing some 5 min bars in order to return 3 elements into that array. You can write a "safer" code using a for loop:

//@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)
  • Related