pine-script

Oscillator Channel Fill for Custom Indicators in Tradingview


I have coded RSI from scratch in pine script in tradingview for learning purposes only. The indicator is working just fine apart from two issues. First, I am failing to figure out a way to extend the background fill indefinitely to the right side. Then, changing desired length from the setting menu of the indicator. Please help me to fix these two issues. My code is as follows -

//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
n = 14
up_days = ta.rma(close > close[1] ? close - close[1] :0, n)
down_days = ta.rma(close < close[1] ? close[1] - close :0, n)
rs = up_days / down_days
rsi = 100 - (100/(1+rs))
plot(rsi)
bandm = plot(50, color=color.black)
band1 = plot(70, color=color.black)
band0 = plot(30, color=color.black)
fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background")  

Thanks & regards.


Solution

  • To carry constant lines forward you can use hline() instead of plot. I have inserted an input in place of 14 for the "n" variable as well. Please also consider the commented rsi code for an alternative way to use the built in ta functions an skip the calcs.

    //@version=5
    indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)
    n = input.int(14, "Length")
    up_days = ta.rma(close > close[1] ? close - close[1] :0, n)
    down_days = ta.rma(close < close[1] ? close[1] - close :0, n)
    rs = up_days / down_days
    rsi = 100 - (100/(1+rs))
    // rsi = ta.rsi(close,n)
    plot(rsi)
    bandm = plot(50, color=color.black)
    band1 = hline(70, color=color.black, linestyle= hline.style_solid)
    band0 = hline(30, color=color.black, linestyle= hline.style_solid)
    fill(band1, band0, color=color.rgb(126, 87, 194, 90), title="Background") 
    

    Cheers and best of luck with your coding