RSI Trend Line
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © chrism665

//@version=5
indicator('RSI Trend Line', overlay=true)

// Tooltips {
string tt_input_lensrc = 'Select a length and input source to use for your RSI calculation'
string tt_input_levels = 'If using bands, select a level for Overbought and Oversold. These are typically the upper and lower lines you see on a RSI chart'
string tt_input_factor = 'This is effectively the responsiveness of the RSI and bands overlaid on price. 1.0 is the minimum and 5.0 is the maximum (most aggressive)'
string tt_input_smooth = 'How much smoothing to apply to the trend line and bands. Use a value of \'1\' if no smoothing is desired'
string tt_input_trendcol = 'Color of the RSI Trend line'
string tt_input_bandscol = 'Check the box to display upper and lower bands.\nOB/OS is the color of the band lines and the Fill is the color of the background to fill the bands'
string tt_input_benchmark = 'Check the box to enable plotting a Comparative RSI Strength.\nEnter a symbol to use as a benchmark'
string tt_input_benchcol = 'Color of the Comparative RSI Strength line'
// }

// Functions {
f_scale(x, z) =>
    (x - 50) / (5 * z)
// }

// Inputs {
len = input.int(defval=14, minval=1, maxval=999, title='Length', group='RSI', inline='RSI')
src = input.source(defval=close, title='', group='RSI', inline='RSI', tooltip=tt_input_lensrc)
ob = input.int(defval=70, minval=2, maxval=100, title='Overbought', group='RSI', inline='Levels')
os = input.int(defval=30, minval=1, maxval=99, title='Oversold', group='RSI', inline='Levels', tooltip=tt_input_levels)

factor = input.float(defval=2.0, minval=1.0, maxval=5.0, step=0.1, title='Responsiveness', group='Inputs', tooltip=tt_input_factor)
smooth = input.int(defval=3, minval=1, maxval=999, title='Smoothing', group='Inputs', tooltip=tt_input_smooth)

use_bench = input.bool(defval=false, title='Use Benchmark', group='Comparative Strength', inline='Bench')
bench_sym = input.symbol(defval='SPY', title='', group='Comparative Strength', inline='Bench', tooltip=tt_input_benchmark)

trend_col = input.color(defval=color.rgb(255, 152, 0, 0), title='Trend', group='Outputs', tooltip=tt_input_trendcol)
use_bands = input.bool(defval=true, title='Use Bands', group='Outputs', inline='Bands')
extremes_col = input.color(defval=color.rgb(41, 98, 255, 0), title='OB/OS', group='Outputs', inline='Bands')
fill_col = input.color(defval=color.rgb(33, 150, 243, 95), title='Fill', group='Outputs', inline='Bands', tooltip=tt_input_bandscol)
bench_col = input.color(defval=color.rgb(244, 67, 54, 0), title='Benchmark', group='Outputs', tooltip=tt_input_benchcol)
// }

// Variables & Declarations {
float atr = ta.atr(len)
float rsi = ta.rsi(src, len)
float bench = request.security(bench_sym,timeframe.period,rsi)
float diff = f_scale(rsi, factor) - f_scale(bench, factor)

float trend = ta.ema(src - f_scale(rsi, factor) * atr, smooth)
float upper = ta.ema(trend + f_scale(ob, factor) * atr, smooth)
float lower = ta.ema(trend + f_scale(os, factor) * atr, smooth)
float compare = ta.ema(trend - diff * atr, smooth)
// }

// Plot Series {
plot(trend, color=trend_col, linewidth=1, title='RSI Trend Line')
u = plot(use_bands ? upper : na, color=extremes_col, title='Upper')
l = plot(use_bands ? lower : na, color=extremes_col, title='Lower')
fill(u, l, color=fill_col, title='Background')
plot(use_bench ? compare : na, color=bench_col, linewidth=1, title="Comparative RSI Strength")
// }

