RSI Influenced Average
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © EsIstTurnt

//@version=5
indicator("RSI Influenced Average",overlay=true)
//Inputs
option    = input.bool(false,'Alternative Calculation Method',group='Calculate')
invert    = input.bool(false,'Invert RSI Values',group='Calculate')
colorize  = input.bool(true,'Show Alternating Colors',group='Appearance')
length    = input.int(64,'MA Length',group='Calculate')
rsiscale  = input.string('900',title='Alternate Calculation Scaling',options=['100','333','666','900'],group='Calculate')
rsi_isr   = input.string(title="RSI/ISR/AVG", defval="AVG", options=["RSI","ISR","AVG"],group='Calculate')
maInput   = input.string(title="MA", defval="LRC", options=["EMA", "SMA", "VWMA", "WMA","LRC"],group='Calculate')
width     = input.string('2',title='Line Width',options=['1','2','3','4'],group='Appearance')
upcol     = input.color(#acfb00,'Up Color',group='Appearance')
dncol     = input.color(#ff0000,'Down Color',group='Appearance')

////////////////Calculate/////////////////
//RSI
rsi       = ta.rsi(input.source(close,'RSI Source'),input.int(32,'RSI Length'))
isr       = 100-rsi
rsilen1   = rsi>80?64:rsi>70?56:rsi>60?48:rsi>50?40:rsi>40?32:rsi>30?24:rsi>20?16:8
//Alternate Calculation Scaling
scale1    = rsiscale=='100'?100:rsiscale=='333'?333:rsiscale=='666'?666 :rsiscale=='900'?900:na
scale2    = rsiscale=='100'?.5 :rsiscale=='333'?.85:rsiscale=='666'?.925:rsiscale=='900'?.945:na
dif1      = (rsi/scale1)+scale2
dif2      = (isr/scale1)+scale2
//Center
h1        = ta.highest(high,input.int(48,'Upper Lookback'))
h2        = ta.highest(high,rsilen1)
l1        = ta.lowest (low ,input.int(48,'Lower Lookback'))
l2        = ta.lowest (low ,rsilen1)
hl_1      = math.avg(h1,l1) //Centerpoint 1
hl_2      = math.avg(h2,l2) //Centerpoint 2 
//Difference
hl_1_dif  = h1-l1 
hl_2_dif  = h2-l2
difscale1 = (rsi/100)*hl_1_dif
difscale2 = (isr/100)*hl_2_dif
//Apply RSI 
rsiscale1 = ta.swma(l1+difscale1)
rsiscale2 = ta.swma(l2+difscale2)
isrscale1 = ta.swma(h1-difscale1)
isrscale2 = ta.swma(h2-difscale2)
rsima     = dif1*hl_1
isrma     = dif2*hl_2
rsiisr1   = rsi_isr == "RSI" ? rsiscale1 : rsi_isr == "AVG" ? math.avg(rsima,isrma) : isrma
rsiisr2   = rsi_isr == "RSI" ? rsiscale2 : rsi_isr == "AVG" ? math.avg(rsima,isrma) : isrma
rsiisr3   = rsi_isr == "RSI" ? rsiscale1 : rsi_isr == "AVG" ? math.avg(rsiscale1,isrscale1) : isrscale1
rsiisr4   = rsi_isr == "RSI" ? rsiscale2 : rsi_isr == "AVG" ? math.avg(rsiscale2,isrscale2) : isrscale2
basis1    = maInput == "EMA" ?  ta.ema(rsima, length)  :maInput == "SMA"? ta.sma(rsima  , length):maInput == "VWMA"? ta.vwma(rsima  , length):maInput == "WMA"? ta.wma(rsima  , length):maInput == "LRC"?ta.linreg(rsima  ,length,0):rsima
basis2    = maInput == "EMA" ?  ta.ema(isrma, length)  :maInput == "SMA"? ta.sma(isrma  , length):maInput == "VWMA"? ta.vwma(isrma  , length):maInput == "WMA"? ta.wma(isrma  , length):maInput == "LRC"?ta.linreg(isrma  ,length,0):isrma
basis3    = maInput == "EMA" ?  ta.ema(rsiisr3, length):maInput == "SMA"? ta.sma(rsiisr3, length):maInput == "VWMA"? ta.vwma(rsiisr3, length):maInput == "WMA"? ta.wma(rsiisr3, length):maInput == "LRC"?ta.linreg(rsiisr3,length,0):rsiisr1
basis4    = maInput == "EMA" ?  ta.ema(rsiisr4, length):maInput == "SMA"? ta.sma(rsiisr4, length):maInput == "VWMA"? ta.vwma(rsiisr4, length):maInput == "WMA"? ta.wma(rsiisr4, length):maInput == "LRC"?ta.linreg(rsiisr4,length,0):rsiisr2
rsi_ma    = option?invert?basis2:basis1:invert?basis4:basis3

////////////////Appearance/////////////////
//Colors
color     = colorize?color.new(rsi_ma<ta.sma(close,8)?upcol:dncol,input.int(50,'Transparency')):input.color(color.gray,'Static Color')
//Thickness
linewidth = width=='1'?1:width=='2'?2:width=='3'?3:4
//Plot
plot(rsi_ma,linewidth=linewidth,color=color)






