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

//original concept from dman103
//modified from "Percentage Relative Strength" script by dman103 https://www.tradingview.com/v/v3bbnm1Z/
 

//@version=5

indicator('Moving Average Relative Strength', shorttitle="MARS", timeframe='')

//Inputs

matype = input.string(title='Moving Average Type', defval='SMA', options=['SMA', 'EMA', 'WMA'], group='Moving Average')
malength = input(title='Moving Average Length', defval=50, group='Moving Average')

index = input.string(title='Index to compare To', defval='NSE:NIFTY', options=['NSE:NIFTY', 'NSE:CNX500', 'NSE:NIFTYSMLCAP250', 'NSE:CNXSMALLCAP'],group='Relative Strength')
compareToOtherIndex = input.bool (title="Compare to different index?", defval=false,group='Relative Strength',inline='Different compare')
otherIndex = input.symbol(title='', defval='',group='Relative Strength',inline='Different compare')

Paintbar=input(false,'Paint Bar?', group="Customisation")
Backgroundcolor=input(true,'Background Color?', group="Customisation")
Easycolors=input(false,'Easy Colors?', group="Customisation")
showZeroLine = input.bool(defval=false, title="Show Zero Line", inline='line 1', group="Customisation")
zerocolor = input(color.black, title='', inline='line 1', group="Customisation")

//Color palette

ropcolor = input(#32CD32, "Relative Outperformance", group="Histogram Colors")
gopcolor = input(#228B22, "Gross Outperformance", group="Histogram Colors")
aopcolor = input(#5ea3f0, "Absolute Outperformance", group="Histogram Colors")

rupcolor = input(#df6b90, "Relative Underperformance", group="Histogram Colors")
gupcolor = input(color.maroon, "Gross Underperformance", group="Histogram Colors")
aupcolor = input(color.black, "Absolute Underperformance", group="Histogram Colors")

ColorUp = #5ea3f0
ColorDown = #df6b90

//Configuring the Moving Average
ma_function(source, length) =>
    if matype == 'SMA'
        ta.sma(source, length)
    else
        if matype == 'EMA'
            ta.ema(source, length)
        else
            if matype == 'WMA'
                ta.wma(source, length)

// Basic conditions

indexToUse = compareToOtherIndex==false ? index : otherIndex
closeIndex = request.security(indexToUse, timeframe.period, close)


ma = ma_function(close, malength)
maIndex = ma_function(closeIndex, malength)

symbolPercent = (close - ma) / ma * 100
indexPercent = (closeIndex - maIndex) / maIndex * 100
val = symbolPercent - indexPercent

priceIsAboveMa = close > ma
priceIsAboveIndexMa = closeIndex > maIndex
indexgreen = request.security(indexToUse, timeframe.period, priceIsAboveIndexMa)

priceIsBelowMa = close <= ma
priceIsBelowIndexMa = closeIndex <= maIndex
indexred = request.security(indexToUse, timeframe.period, priceIsBelowIndexMa)

iff_1 = indexred ? #FBAED2 : color.black
IndexColor = indexgreen ? #9BDDFF : iff_1


//Color coding

rop = indexred and priceIsBelowMa and val>0
gop = indexgreen and priceIsAboveMa and val>0
aop = indexred and priceIsAboveMa

rup = indexgreen and priceIsAboveMa and val<0
gup = indexred and priceIsBelowMa and val<0
aup = indexgreen and priceIsBelowMa

BarColor = rop ? ropcolor : gop ? gopcolor : aop ? aopcolor : rup ? rupcolor : gup ? gupcolor : aup ? aupcolor : color.gray

EasyColor = val >= 0 ? ColorUp : ColorDown

//Plots

BackColor = color.new(IndexColor, 20)

bgcolor(Backgroundcolor? BackColor : na)

barcolor(Paintbar? BarColor : na)

plot(val, color=Easycolors ? EasyColor : BarColor, style=plot.style_columns, linewidth=5)

plot(showZeroLine ? 0 : na, linewidth=1, color=zerocolor, title="Zero Line")


