R-Smart - Relative Strength 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Vebhainvest
//@version=4

//https://invest.webspacio.com, location to track screened stocks

study("R-Smart", shorttitle="R-SMART")

[macdLine, signalLine, histLine] = macd(close, 12, 26, 9)
[diplus,diminus,adx] = dmi(14,14)

// Calculating 
adxRSI = rsi(adx,21) > 50 and adx > sma(adx,21) ? 1: -1
macdRSI = rsi(macdLine,21) > 50 ? 1: -1

//Input
source = input(title="Source", type=input.source, defval=close)
comparativeTickerId = input("NSE:NIFTY", type=input.symbol, title="Comparative Symbol") 
length = input(63, type=input.integer, minval=1, title="Daily Period") //one quarter period
wLength = input(21, type=input.integer, minval=1, title="Wkly Period") //one quarter period
maRS = input(defval=5,type=input.integer,minval=1, title="Rel. Avg")

if timeframe.isweekly 
    length := wLength

//Set up
baseSymbol = security(syminfo.tickerid, timeframe.period, source) 
comparativeSymbol = security(comparativeTickerId, timeframe.period, source) 

//Calculations
res = 100*((baseSymbol/baseSymbol[length])/(comparativeSymbol/comparativeSymbol[length]) - 1)
resColor = res > 0 ? color.green : color.orange 
maLine = sma(res,maRS)

//conditions to plot graph
// if RSI value of macdRSI and adxRSI > 0 then it's bullish signal else bearish signal
strongBull = (adxRSI > 0 and macdRSI > 0 and maLine > 0) ? 1 : -1 // green
weakBull = (adxRSI < 0 or macdRSI < 0) and maLine > 0 ? 1 : -1 // blue
strongBear = adxRSI < 0 and macdRSI < 0 and maLine < 0 ? 1 : -1 // red
weakBear = (adxRSI > 0 or macdRSI > 0) and maLine < 0 ? 1 : -1 // orange

strengthColor = if strongBull > 0
    color.green
else if weakBull > 0
    color.blue
else if strongBear > 0
    color.red
else if weakBear > 0
    color.orange

//Plot
plot(0, linewidth=2, color=color.gray, title="Zero Line")
plot(res, title="Relative Strength", linewidth=2, color= resColor,display=0)


plot(maLine, color=  strengthColor , title="Relative Average", linewidth=2,style=plot.style_histogram)



