rangepine-scriptindicatorrsi

Change RSI Plot Range - Possible or not?


We know that RSI plotting range is between 0 to 100.

Is it possible to change a plotted indicator line so that it will fluctuate in an extended range, say -50 to 100 ?

In simple way I want it to be (stretched) so I can easily draw trend line on the RSI line.

Attached photo is sample of what I mean on is perfect , one that I have is not like it.

I want the the RSI to cross the green and red area without affecting the movement of the indicator. I just want it to be stretching.

enter image description here enter image description here

//@version=5
indicator(title="Relative Strength Index", shorttitle="RSI", format=format.price, precision=2, timeframe="", timeframe_gaps=true)

ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(50, title="MA Length", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)

rsicolor = rsi > 70 ? color.new(#ff0057, 0) : rsi < 30 ? color.new(#ff0057, 0) : #1056ee

plot(rsi, "RSI", color=rsicolor, linewidth=2, style=plot.style_stepline)
plot(rsiMA, "RSI-based MA", color=color.rgb(120, 206, 255))


OverBought1 = input.int(80, minval=1)
OverBought2 = input.int(90, minval=1)
OverSold1 = input.int(20, minval=1)
OverSold2 = input.int(10, minval=1)


OB1 = hline(OverBought1 , color=color.red, linestyle=hline.style_dashed)
OB2 = hline(OverBought2 , color=color.red, linestyle=hline.style_dashed)
OS1 = hline(OverSold1 , color=color.green, linestyle=hline.style_dashed)
OS2 = hline(OverSold2 , color=color.green, linestyle=hline.style_dashed)

fill(OB1 , OB2, color=color.rgb(255, 82, 82, 80), title="Red Zoon Fill")
fill(OS1 , OS2 , color=color.rgb(76, 175, 79, 80), title="Green Zoon Fill")

Any ideas, please?

Thank you!

Colin

Is it possible to change a plotted indicator line so that it will fluctuate in an extended range, say -50 to 100 ?

In simple way I want it to be (stretched) so I can easily draw trend line on the RSI line.

Attached photo is sample of what I mean on is perfect , one that I have is not like it.

I want the the RSI to cross the green and red area without affecting the movement of the indicator. I just want it to be stretching.


Solution

  • You can use normalize() function:

    //@version=5
    indicator("My script")
    
    normalize(_src, _min, _max) =>
        // Normalizes series with unknown min/max using historical min/max.
        // _src      : series to rescale.
        // _min, _min: min/max values of rescaled series.
        var _historicMin =  10e10
        var _historicMax = -10e10
        _historicMin := math.min(nz(_src, _historicMin), _historicMin)
        _historicMax := math.max(nz(_src, _historicMax), _historicMax)
        _min + (_max - _min) * (_src - _historicMin) / math.max(_historicMax - _historicMin, 10e-10)
    
    rsi = ta.rsi(close, 20)
    
    rsiNormalized = normalize(rsi, -50, 100)
    
    plot(rsiNormalized)
    
    hline(-50)
    hline(100)
    

    EDIT

    In your case, since we know the scale will always be between 0 to 100, we can use math to solve the issue much more accurately:

    //@version=5
    indicator("My script")
    
    rsi = ta.rsi(close, 20)    // this will give a value between 0 to 100
    
    // find the value compared to a scale of 100
    rsiPerc = rsi / 100 
    
    // multiply by the new range to get the position of rsi on the new range
    rsiOnNewScale = rsiPerc * 150
    
    // place the newRsi on a scale of -50 to 100
    newRsi = rsiOnNewScale - 50
    
    plot(newRsi)
    
    hline(-50)
    hline(100)
    

    Or in short:

    //@version=5
    indicator("My script")
    
    rsi = ta.rsi(close, 20)    // this will give a value between 0 to 100
    
    newRsi = (rsi / 100 * 150) - 50
    
    plot(newRsi)
    
    hline(-50)
    hline(100)