SMADIF4 Indicator
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ihsuka942
//@version=4
study("SMADIF4 Indicator", overlay=true)
len = input(defval=100,minval = 0, title="Bars in work", type=input.integer)
ema_1 = input(defval=20,minval = 0, title="Period SMA 1", type=input.integer)
ema_2 = input(defval=50,minval = 0, title="Period SMA 2", type=input.integer)
ema_3 = input(defval=100,minval = 0, title="Period SMA 3", type=input.integer)
ema_4 = input(defval=200,minval = 0, title="Period SMA 4", type=input.integer)
src = input(title="Source", type=input.source, defval=close)
Nstd = input(defval=1.3,minval = 0, title="Confidence Level in Standard deviations", type=input.float)

EMA1 = sma(src, ema_1)
EMA2 = sma(src, ema_2)
EMA3 = sma(src, ema_3)
EMA4 = sma(src, ema_4)
emaclose1 = 100*(src - EMA1)/EMA1
emaclose2 = 100*(src - EMA2)/EMA2
emaclose3 = 100*(src - EMA3)/EMA3
emaclose4 = 100*(src - EMA4)/EMA4
emap1 = sma(emaclose1,len)+Nstd*stdev(emaclose1,len)
emap2 = sma(emaclose2,len)+Nstd*stdev(emaclose2,len)
emap3 = sma(emaclose3,len)+Nstd*stdev(emaclose3,len)
emap4 = sma(emaclose4,len)+Nstd*stdev(emaclose4,len)
emam1 = sma(emaclose1,len)-Nstd*stdev(emaclose1,len)
emam2 = sma(emaclose2,len)-Nstd*stdev(emaclose2,len)
emam3 = sma(emaclose3,len)-Nstd*stdev(emaclose3,len)
emam4 = sma(emaclose4,len)-Nstd*stdev(emaclose4,len)



backgroundemas1 =  emaclose1 > emap1  and  close > sma(close,200) ? color.green:
     emaclose1 < emam1 and close < sma(close,200) ? color.red :
     na
backgroundemas2 =  emaclose2 > emap2  and  close > sma(close,200) ? color.green:
     emaclose2 < emam2 and close < sma(close,200) ? color.red :
     na
backgroundemas3 =  emaclose3 > emap3  and  close > sma(close,200) ? color.green:
     emaclose3 < emam3 and close < sma(close,200) ? color.red :
     na
backgroundemas4 =  emaclose4 > emap4  and  close > sma(close,200) ? color.green:
     emaclose4 < emam4 and close < sma(close,200) ? color.red :
     na

bgcolor(color=backgroundemas1,title ='SMA 20', transp=75)
bgcolor(color=backgroundemas2,title ='SMA 50', transp=75)
bgcolor(color=backgroundemas3,title ='SMA 100', transp=75)
bgcolor(color=backgroundemas4,title ='SMA 200', transp=75)
