FDL
FDL

Reputation: 115

How to get closing price of most right-hand visible bar on a tradingview chart?

I would like to get the closing price of the most right-hand last bar visible on a tradingview chart with pine-script.

I am using pine-script v5

Upvotes: 0

Views: 806

Answers (1)

mr_statler
mr_statler

Reputation: 2161

chart.right_visible_bar_time variable will return the time of the rightmost bar currently visible on the chart.

time variable will return the current bar time.

Comparing them will return true only on the rightmost visible bar. You can have things done when this condition is true (like assigning the close price to your own variable) but keep in mind it will reset on each bar unless you use the var keyword.

var float rightmostClosePrice = na

if time == chart.right_visible_bar_time
    rightmostClosePrice := close

t = table.new(position = position.top_right, columns = 1, rows = 1, bgcolor = color.yellow, border_width = 1)
table.cell(table_id = t, column = 0, row = 0, text = "Close is " + str.tostring(rightmostClosePrice))

Upvotes: 1

Related Questions