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

//@version=5
indicator("Relative Strength")

//INPUTS
Den         =   input.symbol("AMEX:SPY", "Denominator")         // Denominator
MAdisplay   =   input(true, "Sma Display")                      // Display Moving Average
MAlength    =   input(10, "Sma Length")                         // Length Moving Average
Cat         =   input.color(color.black,    "NH Color")        // All Time High Color
Cup         =   input.color(color.blue,     "Up Color")         // Up Color
Cdo         =   input.color(color.fuchsia,  "Down Color")       // Down Color
Lookback    =   input.int(0, "New High Lookback", 0, tooltip = "IF 0 then look for NH ELSE look for past X high")

//VARIABLES
Num =   syminfo.tickerid                                        // Current symbol ticker

//REQUESTS
RNum = request.security(Num, na, close)                         // Request Numerator Data
RDen = request.security(Den, na, close)                         // Request Denominator Data

//RELATIVE STRENGTH
RStr = RNum / RDen                                              // Quotien 

//MA
Sma =   ta.sma(RStr, MAlength)                                  // Simple Moving Average

//New High
float   NH =   na
if Lookback == 0
    NH := RStr > ta.max(RStr[1]) ? RStr : NH[1]  
else
    NH := RStr > ta.highest(RStr[1], Lookback) ? RStr : NH[1]

//COLORS 
Prs = RStr == NH ? Cat : RStr * 100 > RStr[1] * 100 ? Cup : Cdo   //RS based on previous RS
Pma = Sma > Sma[1] ? color.green : color.red                    //MA based on previous MA

//PLOTS
plot(RStr, "Relative Strength line", color.new(Prs, 20), 2) 
plot(MAdisplay ? Sma : na,   "Moving Average", color.new(Pma, 50), 1)