July 12, 2022

Pine Script™ and charts become better acquainted

New built-ins in the chart namespace now provide scripts with more visibility over chart properties. With them, we also introduce a new type of behavior for your Pine scripts, which allows them to recalculate and redraw on visible bars only, automatically readjusting when traders scroll or zoom on their chart.

Chart properties

The new chart.bg_color built-in returns the color of the chart’s background. You can use it to adapt your script’s color scheme to the chart’s context. Its chart.fg_color companion returns a color that will provide optimal contrast with the chart’s background color, whatever it is.

Another set of new boolean built-ins makes it possible to detect the type of chart your script is running on. They are:

The last one will return true on all standard chart types (the ones returning a non-synthetic close price): bars, candles, hollow candles, columns, line, area or baseline.

Visible bars and dynamic recalculation

The new chart.left_visible_bar_time and chart.right_visible_bar_time built-ins return the opening time of the leftmost and rightmost visible bars of the chart. As a trader scrolls or zooms on their chart – causing the range of visible bars on the chart to change – scripts using either of these new built-ins will automatically re-execute so that calculations and visuals can adapt to the new context. This opens the door to a whole new variety of scripts that dynamically adjust to changes in the range of visible bars.

This example draws an arrow from the open of the first visible bar to the close of the last, and shows the gain/loss for that range:

indicator("Chart gain/loss", "", true)

// Save the `open` of the leftmost visible bar.
var float chartOpen = na
if time == chart.left_visible_bar_time
    chartOpen := open
else if time == chart.right_visible_bar_time
    // Run the following code on the chart's rightmost visible bar.
    color arrowColor = close > chartOpen ? color.lime : color.fuchsia
    // Draw arrow once, then modify it.
    var line arrow = line.new(na, na, na, na, xloc.bar_time, extend.none, na, line.style_arrow_right, 3)
    line.set_xy1(arrow, chart.left_visible_bar_time, chartOpen)
    line.set_xy2(arrow, chart.right_visible_bar_time, close)
    line.set_color(arrow, arrowColor)
    // Draw percentage label once, then modify it.
    var label percentage = label.new(na, na, na, xloc.bar_time, yloc.price, #00000000, label.style_label_down, size = size.huge)
    int midTime = int(math.avg(chart.left_visible_bar_time, chart.right_visible_bar_time))
    label.set_xy(percentage, midTime, math.avg(chartOpen, close))
    label.set_text(percentage, str.tostring((close - chartOpen) / chartOpen * 100, format.percent))
    label.set_textcolor(percentage, arrowColor)

Note that because scripts using this feature will re-execute more often than normal ones, care must be taken to use efficient code and keep calculations light if one wants to preserve the usual responsiveness of TradingView charts.

We have published a Chart VWAP indicator as an example of how this new feature can be used. It automatically anchors on visible bars:

The PineCoders account has also published a VisibleChart library to help Pine Script™ programmers make the most of this new feature.

To stay informed of new Pine Script™ features, keep an eye on the User Manual’s Release notes. The PineCoders account also broadcasts updates from its Squawk Box on Telegram, its Twitter account, and from the “Pine Script™ Q&A” public chat on TradingView.

We hope you find these highly-requested features useful. Please keep sending us your feedback and suggestions for improvement. We build TradingView for you, and we’re always keen to hear from you.

Look first / Then leap

Launch Chart