RAINBOW AVERAGES - INDICATOR - (AS) - 1/3
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Adam-Szafranski

//@version=5
indicator("RAINBOW AVERAGES - INDICATOR - (AS) - 1/3", overlay = true ,shorttitle = 'RB_AVGs-(AS)-INDICATOR')
///////////////FUNCTIONS AND TOOLTIPS///////////////////////////FUNCTIONS AND TOOLTIPS/////////////////////////////{
TT_00 = 'INFORMATION: \n-RAINBOW moving averages can be used in many different ways, this script is mainly destinded for trend analysis. \n -Indicator is build to be used on chart not below. but if you would like you can turn this off and change code from "overlay=true" to false. This will cause it to show below chart and will let user see how BG color variants are calculated(also turn of this switch if you prefer below chart). \n-This is First of 3 scripts i will publish using RAINBOW indicators. This one shows the averages on chart, second will utilize RAINBOW BANDS and OSCILLATOR. Third one will be a strategy combining everything'
TT_01 = '-Here we choose SOURCE that is mainly used for calculation of first MA and Type of BGcolor we want to use. \n-BGCOLORS: there are 4 different types of bgc that can be used. \n1-CHANGE: Sums change of all 10 MAs and if its positive and not falling BG=green. If value is negative and not rising BG=red. \n2-RAINBW: simply gives green when all averages are in thier order(so MA1>MA2>MA3>MA4 etc...).Red is exactly the other way so MA1<MA2<MA3....<MA9.\n3-TSHOLD: This one calculates difference beetwen MA0 and MA9 then divided by MA9 and multiplies by 100, So = (100*((MA0-MA9)/MA0)).This way we get width of rainbow averages. We get green if width>treshold or red if width<-treshold.\n4-DIRECT: Checks how many MAs are blw/abv SOURCE and adds 1 or subtracts -1. If value (will be set later) is crossed it gives green when positive and red when negative.\nAnd ofc 0-NONE for no BGC at all '
TT_02 = '-Now we choose type and period of MA0 (it says MA1 but its MA0). From this MA all others will be calculated - meaning MA0 will have source that you choosed above but src of MA1 will be MA0 and MA1 will be source of MA2 and so on'
TT_03 = '-smooth MA0? and length of this smoothing. \nfew things:\n-This will only smooth MA0 but as others are calculated based on it will be smthed as well.\n-To smooth script uses 3-pole super smoother.'
TT_04 = '-We move to "MA2" i called it like that but this settings affect all MAs except MA0 (so M1-M9). \nLike previously we define type and length of MAs.\n MA1-9 use same type and length. but keep in mind that these values should be lower as for exaple if we set 55 this will mean that MA1 is average of picked type from 55periods of MA0. MA2 will be 55 periods of MA1 and so on.\n-(Check out RMA)'
TT_05 = '-Moving on to defing some of the inputs for BG color: \nTSH=value of treshold when using 3-TSHOLD-BGC.Its a good idea to change chart to pane below so it will be easier to adjust. Defualt values are from EURUSD-5M.\nFinally we set value that must be crossed if using 4-DIRECT-BGC. There are 10 MAs so max is also 10. if u set for example 9 this will mean that at least 9 Mas must be below/above price for the script to detect trend. (higher values adviced as most of the time this indicator oscillates either around max or min ) '
TT_06 = '-Hope this will be usefull, if you have any ideas/feedback or maybe spotted error in the code - LET ME KNOW \n-Iam looking for ideas for intresting indicators and strategies that i could code, so if you do not know pinescript just message me and i would be glad to write it for you:)....for free obviously. \n-Stay tuned for two remaining scripts using skittles indicators and check out my other scripts \n----Until we meet again!'
SSF3(SRC, LEN) =>
    P = 2 * math.asin(1)
    X = P / LEN
    a = math.exp(-X)
    b = 2 * a * math.cos(1.738 * X)
    c = math.pow(a, 2)
    cf4 = math.pow(c, 2)
    cf3 = -(c + b * c)
    cf2 = b+c
    cf1 = 1-cf2-cf3-cf4
    src1 = nz(SRC[1], SRC)
    src2 = nz(SRC[2], src1)
    src3 = nz(SRC[3], src2)
    SSF = 0.0
    SSF := cf1 * SRC + cf2 * nz(SSF[1], src1) + cf3 * nz(SSF[2], src2) + cf4 * nz(SSF[3], src3)
sinmov(SRC,LEN) =>
    float result = 0.0
    Pi 		= 2 * math.asin(1)
    sum 	= 0.0
    weigS 	= 0.0
    for i = 0 to LEN - 1 by 1
        weig 	 = math.sin((i + 1) * Pi / (LEN + 1))
        sum 	+= nz(SRC[i]) * weig
        weigS 	+= weig
        weigS
    result := sum / weigS
MA(MA_SRC,MA_LEN,MA_TYP) =>
    switch MA_TYP
    	"EMA" 		=> ta.ema       (MA_SRC, MA_LEN)
    	"SMA" 		=> ta.sma       (MA_SRC, MA_LEN)
    	"RMA" 		=> ta.rma       (MA_SRC, MA_LEN)
    	"WMA" 		=> ta.wma       (MA_SRC, MA_LEN)
		'SIN' 		=> sinmov		(MA_SRC, MA_LEN)

CHANGE  (MA,LEN) => ta.change(MA,LEN)
DIRECT  (MA,SRC) => MA<SRC?1:MA>SRC?-1:0
///////////////FUNCTIONS AND TOOLTIPS///////////////////////////FUNCTIONS AND TOOLTIPS/////////////////////////////}
///////////////////////INPUTS///////////////////////INPUTS///////////////////////INPUTS///////////////////////INPUTS{
MA_PLT  = input.bool	(true                              ,inline='1', tooltip = TT_00,title = 'PLOT RAINBOW AVERAGE ? / !READ ME!-:)')
BG_COL  = input.string	('0-NONE','BGC?/SOURCE'	           ,inline='2', options = ['0-NONE','1-CHANGE','2-RAINBW','3-TSHOLD','4-DIRECT'])
MA_SRC  = input			(hlcc4   ,'/'		               ,inline='2', tooltip = TT_01)
MA1TYP  = input.string	('EMA'	 ,'MA1: TYP/LEN'		   ,inline='3', options = ['EMA','SIN','RMA','SMA','WMA'])
MA1LEN  = input.int		(120	 ,'/'                      ,inline='3', tooltip = TT_02)
MA1SMT  = input.bool	(false   ,'SMT RBW MA1?/SMTHLEN'   ,inline='4')
SM1LEN  = input.int		(55	     ,'/'                      ,inline='4', tooltip = TT_03)
MA2TYP  = input.string	('SMA' 	 ,'MA2: TYP/LEN'		   ,inline='5', options = ['EMA','SIN','RMA','SMA','WMA'])
MA2LEN  = input.int		(31	   	 ,'/'                      ,inline='5', tooltip = TT_04)
TSHOLD  = input.float   (0.1     ,'BG:TSH/CH/DR'           ,inline='6', step    = 0.05)
BG_DIR  = input.int		(7	     ,'/'                      ,inline='6', tooltip = TT_05)
BYEBYE  = input.bool	(true    ,'Last thing....'         ,inline='7', tooltip = TT_06)
//////////////INPUTS///////////////////////INPUTS///////////////////////INPUTS///////////////////////INPUTS//////////////}
/////////////////////CALC////////////////////CALC////////////////////CALC////////////////////CALC////////////////////CALC{
SSF = SSF3 (MA_SRC,SM1LEN)      ,MA0 = MA1SMT?MA(SSF,MA1LEN,MA1TYP) : MA(MA_SRC,MA1LEN,MA1TYP)
MA1 = MA   (MA0,MA2LEN,MA2TYP)  ,MA2 = MA  (MA1,MA2LEN,MA2TYP)  ,MA3 = MA  (MA2,MA2LEN,MA2TYP)
MA4 = MA   (MA3,MA2LEN,MA2TYP)  ,MA5 = MA  (MA4,MA2LEN,MA2TYP)  ,MA6 = MA  (MA5,MA2LEN,MA2TYP)
MA7 = MA   (MA6,MA2LEN,MA2TYP)  ,MA8 = MA  (MA7,MA2LEN,MA2TYP)  ,MA9 = MA  (MA8,MA2LEN,MA2TYP)

c_MA0 = CHANGE(MA0,2)      ,d_MA0 = DIRECT (MA0,MA_SRC)
c_MA1 = CHANGE(MA1,2)      ,d_MA1 = DIRECT (MA1,MA_SRC)
c_MA2 = CHANGE(MA2,2)      ,d_MA2 = DIRECT (MA2,MA_SRC)
c_MA3 = CHANGE(MA3,2)      ,d_MA3 = DIRECT (MA3,MA_SRC)
c_MA4 = CHANGE(MA4,2)      ,d_MA4 = DIRECT (MA4,MA_SRC)
c_MA5 = CHANGE(MA5,2)      ,d_MA5 = DIRECT (MA5,MA_SRC)
c_MA6 = CHANGE(MA6,2)      ,d_MA6 = DIRECT (MA6,MA_SRC)
c_MA7 = CHANGE(MA7,2)      ,d_MA7 = DIRECT (MA7,MA_SRC)
c_MA8 = CHANGE(MA8,2)      ,d_MA8 = DIRECT (MA8,MA_SRC)
c_MA9 = CHANGE(MA9,2)      ,d_MA9 = DIRECT (MA9,MA_SRC)

DIRECT_RAINBW  = (d_MA0+d_MA1+d_MA2+d_MA3+d_MA4+d_MA5+d_MA6+d_MA7+d_MA8+d_MA9)
CHANGE_RAINBW  = (c_MA0+c_MA1+c_MA2+c_MA3+c_MA4+c_MA5+c_MA6+c_MA7+c_MA8+c_MA9)
RBW_UPP_ORDER  = MA1>MA2 and MA2>MA3 and MA3>MA4 and MA4>MA5 and MA5>MA6 and MA7>MA8 and MA8>MA9 
RBW_DWN_ORDER  = MA1<MA2 and MA2<MA3 and MA3<MA4 and MA4<MA5 and MA5<MA6 and MA7<MA8 and MA8<MA9 
RAINBOW_WIDTH  = (100*((MA0-MA9)/MA0))
ZERO_OVF_LINE  = 0

DIR_UP = DIRECT_RAINBW >= BG_DIR
DIR_DN = DIRECT_RAINBW <=-BG_DIR
TSH_UP = RAINBOW_WIDTH > TSHOLD and ta.rising  (RAINBOW_WIDTH,3)
TSH_DN = RAINBOW_WIDTH <-TSHOLD and ta.falling (RAINBOW_WIDTH,3)
CHA_UP = CHANGE_RAINBW > 0  and not ta.falling (CHANGE_RAINBW,3)
CHA_DN = CHANGE_RAINBW < 0  and not ta.rising  (CHANGE_RAINBW,3)
RBW_UP = RBW_UPP_ORDER
RBW_DN = RBW_DWN_ORDER
//////////////CALC////////////////////CALC////////////////////CALC////////////////////CALC////////////////////CALC////////////////////CALC}
//////////////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS{
p_MA0  =  plot  (MA_PLT ? MA0 : na , 'MA0' , color.rgb	(255, 0  , 0   ),display = display.all )
p_MA1  =  plot  (MA_PLT ? MA1 : na , 'MA1' , color.rgb	(255, 94 , 0   ),display = display.pane)
p_MA2  =  plot  (MA_PLT ? MA2 : na , 'MA2' , color.rgb	(255, 230, 0   ),display = display.pane)
p_MA3  =  plot  (MA_PLT ? MA3 : na , 'MA3' , color.rgb	(145, 255, 49  ),display = display.pane)
p_MA4  =  plot  (MA_PLT ? MA4 : na , 'MA4' , color.rgb	(59 , 211, 51  ),display = display.pane)
p_MA5  =  plot  (MA_PLT ? MA5 : na , 'MA5' , color.rgb	(28 , 120, 55  ),display = display.pane)
p_MA6  =  plot  (MA_PLT ? MA6 : na , 'MA6' , color.rgb	(58 , 252, 255 ),display = display.pane)
p_MA7  =  plot  (MA_PLT ? MA7 : na , 'MA7' , color.rgb	(54 , 90 , 237 ),display = display.pane)
p_MA8  =  plot  (MA_PLT ? MA8 : na , 'MA8' , color.rgb	(31 , 37 , 139 ),display = display.pane)
p_MA9  =  plot  (MA_PLT ? MA9 : na , 'MA9' , color.rgb	(151, 0  , 182 ),display = display.all )

CLR_LIN_GR = color.rgb(167, 170, 182, 18)   ,CLR_FIL_MA = color.rgb(80, 136, 240 , 89)
CLR_IND_UP = color.rgb(61, 202, 66  , 13)   ,CLR_IND_DN = color.rgb(235 , 65, 13  , 9)
CLR_BGC_UP = color.rgb(44, 216, 107 , 80)   ,CLR_BGC_DN = color.rgb(205 , 24, 72 , 80)

CLR_IND_DR = DIR_UP?CLR_IND_UP:DIR_DN?CLR_IND_DN:CLR_LIN_GR
CLR_IND_TH = TSH_UP?CLR_IND_UP:TSH_DN?CLR_IND_DN:CLR_LIN_GR
CLR_IND_CH = CHA_UP?CLR_IND_UP:CHA_DN?CLR_IND_DN:CLR_LIN_GR

BGCLR1_CHA = CHA_UP?CLR_BGC_UP:CHA_DN?CLR_BGC_DN:na         
BGCLR2_RBW = RBW_UP?CLR_BGC_UP:RBW_DN?CLR_BGC_DN:na
BGCLR3_TSH = TSH_UP?CLR_BGC_UP:TSH_DN?CLR_BGC_DN:na         
BGCLR4_DIR = DIR_UP?CLR_BGC_UP:DIR_DN?CLR_BGC_DN:na

plot(not MA_PLT and BG_COL=='1-CHANGE'?CHANGE_RAINBW:na,'CHANGE_RBW' ,CLR_IND_CH,1,plot.style_area   )
plot(not MA_PLT and BG_COL=='1-CHANGE'?ZERO_OVF_LINE:na,'ZERO_HLINE' ,CLR_LIN_GR,1,plot.style_linebr )
plot(not MA_PLT and BG_COL=='3-TSHOLD'?RAINBOW_WIDTH:na,'RAINBW_WID' ,CLR_IND_TH,1,plot.style_area   )
plot(not MA_PLT and BG_COL=='3-TSHOLD'?TSHOLD       :na,'TSHOLD_POS' ,CLR_LIN_GR,1,plot.style_linebr )
plot(not MA_PLT and BG_COL=='3-TSHOLD'?-TSHOLD      :na,'TSHOLD_NEG' ,CLR_LIN_GR,1,plot.style_linebr )
plot(not MA_PLT and BG_COL=='4-DIRECT'?DIRECT_RAINBW:na,'DIRECT_RBW' ,CLR_IND_DR,1,plot.style_columns)


BACKGROUND_COLOR = switch BG_COL
    '0-NOCOLR' => na
    '1-CHANGE' => BGCLR1_CHA
    '2-RAINBW' => BGCLR2_RBW
    '3-TSHOLD' => BGCLR3_TSH
    '4-DIRECT' => BGCLR4_DIR
bgcolor(BACKGROUND_COLOR ,title = 'RAINBOW BGC TREND')
fill(p_MA0,p_MA9,CLR_FIL_MA,title = 'Gentle fill for MAs')
//////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS//////////////////////PLOTS}

