Colored RS(Relative Strength)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © akhileshsaipangallu

//@version=4
study("Colored RS(Relative Strength)")

period_length = input(55, type=input.integer, minval=1, title="Period Length")
rs_ema_length = input(14, type=input.integer, minval=1, title="RS EMA length")

benchmark_index_input = input("NSE:NIFTY", type=input.symbol, title="Benchmark Index")
benchmark_index = security(benchmark_index_input, timeframe.period, close)

rs = ((close / close[period_length]) / (benchmark_index / benchmark_index[period_length])) - 1

// green
long_condition = (rs > 0) and (benchmark_index > benchmark_index[period_length])
// red
short_condition =(rs < 0) and (benchmark_index < benchmark_index[period_length])
// blue
strength_condition = (rs > 0) and (benchmark_index < benchmark_index[period_length])
// yellow
weak_condition = (rs < 0) and (benchmark_index > benchmark_index[period_length])

hline(0, color=color.black)
plot_color = long_condition ? color.green : short_condition ? color.red : weak_condition ? color.yellow : strength_condition ? color.blue : color.white
plot(series=rs, title="RS 55", color=color.black, linewidth=1)
bgcolor(plot_color, transp=45)

rs_ema = ema(rs, rs_ema_length)
plot(series=rs_ema, title="RS EMA", color=color.orange, linewidth=1)
