ScalpTrader RSI
// This work is licensed under a Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) https://creativecommons.org/licenses/by-nc-sa/4.0/
// ©ScalpTradr

//@version=5

// Defining the main indicator with a short title
indicator(title='RSI [ScalpTradr]', shorttitle='ST RSI', overlay=false)

// User input for RSI length and whether to show bands
len = input(14)
bands = input(true)

// Function for calculating Hull Moving Average (HMA)
f_hma(_src, _length) =>
    // Calculating the weighted moving average and assigning it to _return
    _return = ta.wma(2 * ta.wma(_src, _length / 2) - ta.wma(_src, _length), math.round(math.sqrt(_length)))
    // Returning the result
    _return

// Source data for the RSI calculation is closing price
src = close

// Up is the relative moving average of the upward changes
up = ta.rma(math.max(ta.change(src), 0), len)

// Down is the relative moving average of the downward changes
down = ta.rma(-math.min(ta.change(src), 0), len)

// RSI calculation
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - 100 / (1 + up / down)

// Plots the middle line (zero line) with gray color
plotZero = plot(50, color=color.new(color.gray, 50), title='ZeroMiddle')

// Determines fill color based on RSI value
fillColor = rsi < 50 ? color.red : color.green

// Fills the area between RSI plot and zero line with determined color
fill(plot1=plot(rsi, color=color.new(color.black, 0), linewidth=2), plot2=plotZero, color=color.new(fillColor, 50))

// Bollinger Bands
var float upper = na
var float lower = na

if bands
    basis = ta.sma(rsi, len)
    dev = 2 * ta.stdev(rsi, len)
    upper := basis + dev
    lower := basis - dev

// Determines RSI color based on its value and its position relative to the Bollinger Bands
sma_color_rsi = rsi > upper ? color.red : rsi < lower ? color.red : color.black

// Plots RSI with determined color
plot(rsi, color=sma_color_rsi, title='RSI', linewidth=3)

plot(upper, color=color.blue, title='Bollinger Bands Top')
plot(lower, color=color.blue, title='Bollinger Bands Bottom')
