viewpine-scripttradingview-api

How can I overlay text using TradingView Pine Script?


I would like to overlay text over my TradingView graph. I would like to show the text "over bought" at the RSI 85 level, centered in white, and also show the text "over sold" at the RSI 15 level. But I don't know how to do that. a label or text shows over bought at rsi 85 and over sold at rsi 15 above lines can anyone help me do that,thanks

enter image description here

How can I add the text?

This is the code I have so far:

//@version=4
study(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, resolution="")
len = input(14, minval=1, title="Length")
src = input(close, "Source", type = input.source)
up = rma(max(change(src), 0), len)
down = rma(-min(change(src), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=color.rgb(255, 255, 255))
band6 = hline (85, "upper band2", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)
band5 = hline (70, "upper Band3", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)
band4 = hline (60, "upper band4", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)
band2 = hline (30, "lower band2", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)
band1 = hline (15, "lower band3", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)
band0 = hline (00, "lower band4", color=#ffffff70,linestyle=hline.style_dotted, linewidth=1)

This is what my graph looks like so far:

screenshot of TraderView graph without text


Solution

  • First, upgrade v4 to v5
    https://www.tradingview.com/pine-script-docs/migration-guides/overview/

    To center your chart, you can use chart.right_visible_bar_time and chart.left_visible_bar_time
    https://www.tradingview.com/pine-script-reference/v6/#var_chart.right_visible_bar_time

    Something like that:

    //@version=6
    indicator(title = 'Relative Strength Index', shorttitle = 'RSI', format = format.price, precision = 2)
    len = input.int(14, minval = 1, title = 'Length')
    src = input(close, 'Source')
    up = ta.rma(math.max(ta.change(src), 0), len)
    down = ta.rma(-math.min(ta.change(src), 0), len)
    rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)
    plot(rsi, 'RSI', color = color.rgb(255, 255, 255))
    band6 = hline(85, 'upper band2', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    band5 = hline(70, 'upper Band3', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    band4 = hline(60, 'upper band4', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    band2 = hline(30, 'lower band2', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    band1 = hline(15, 'lower band3', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    band0 = hline(00, 'lower band4', color = #ffffff70, linestyle = hline.style_dotted, linewidth = 1)
    
    
    f_print(_text, _level) =>
        // Create label on the first bar.
        var _label = label.new(na, na, _text, xloc.bar_time, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_center)
        _centerChart = chart.right_visible_bar_time - ((chart.right_visible_bar_time - chart.left_visible_bar_time) / 2)
        // On next bars, update the label's x and y position, and the text it displays.
        label.set_xy(_label, _centerChart, _level)
        label.set_text(_label, _text)
    
    f_print('HIGH RSI', 85)
    f_print('LOW RSI', 0)
    

    test