Educational: Fill
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fikira

//@version=5

indicator("Educational: Fill", overlay=true)

opt      = input.string(                         '1', title= 'Type colour fill'          , options=['1', '2', '1 & 2']  )
c_e      = input.int   (                          17, title= 'switch colors at ema x'    ,  minval=  1 ,  maxval=  17   ,
                       tooltip= 'if close switches above/below the chosen ema \n-> this switches bullish/bearish colour')

// ––––––––––––––––––––––––––––––––––––––––––––[         input length's         ]––––––––––––––––––––––––––––––––––––––––––––
len1     = input.int   (                          5 , title= '', group= 'length ema\'s 1 -> 4'  , inline= '1', minval= 1)
len2     = input.int   (                          7 , title= '', group= 'length ema\'s 1 -> 4'  , inline= '1', minval= 1)
len3     = input.int   (                         10 , title= '', group= 'length ema\'s 1 -> 4'  , inline= '1', minval= 1)
len4     = input.int   (                         14 , title= '', group= 'length ema\'s 1 -> 4'  , inline= '1', minval= 1)
len5     = input.int   (                         20 , title= '', group= 'length ema\'s 5 -> 8'  , inline= '2', minval= 1)
len6     = input.int   (                         27 , title= '', group= 'length ema\'s 5 -> 8'  , inline= '2', minval= 1)
len7     = input.int   (                         35 , title= '', group= 'length ema\'s 5 -> 8'  , inline= '2', minval= 1)
len8     = input.int   (                         44 , title= '', group= 'length ema\'s 5 -> 8'  , inline= '2', minval= 1)
len9     = input.int   (                         50 , title= '', group= 'length ema\'s 9 -> 12' , inline= '3', minval= 1)
len10    = input.int   (                         65 , title= '', group= 'length ema\'s 9 -> 12' , inline= '3', minval= 1)
len11    = input.int   (                         75 , title= '', group= 'length ema\'s 9 -> 12' , inline= '3', minval= 1)
len12    = input.int   (                         89 , title= '', group= 'length ema\'s 9 -> 12' , inline= '3', minval= 1)
len13    = input.int   (                        100 , title= '', group= 'length ema\'s 13 -> 17', inline= '4', minval= 1)
len14    = input.int   (                        111 , title= '', group= 'length ema\'s 13 -> 17', inline= '4', minval= 1)
len15    = input.int   (                        131 , title= '', group= 'length ema\'s 13 -> 17', inline= '4', minval= 1)
len16    = input.int   (                        150 , title= '', group= 'length ema\'s 13 -> 17', inline= '4', minval= 1)
len17    = input.int   (                        200 , title= '', group= 'length ema\'s 13 -> 17', inline= '4', minval= 1)

// ––––––––––––––––––––––––––––––––––––––––––––[          input colours         ]––––––––––––––––––––––––––––––––––––––––––––
i_colUp1 = input.color (color.rgb(  0, 255,  38, 25), title= '', group='Fill 1 Up'              , inline='1'            )
i_colUp2 = input.color (color.rgb(255,   0,   0,  0), title= '', group='Fill 1 Up'              , inline='1'            )
i_colDn1 = input.color (color.rgb(  0,  47, 255, 25), title= '', group='Fill 1 Down'            , inline='2'            )
i_colDn2 = input.color (color.rgb(255,   0,   0,  1), title= '', group='Fill 1 Down'            , inline='2'            )

i_colUpa = input.color (color.rgb(  8,   0, 255, 25), title= '', group='Fill 2 Up'              , inline='1'            )
i_colUpb = input.color (color.rgb( 38, 255,   0, 25), title= '', group='Fill 2 Up'              , inline='1'            )
i_colDna = input.color (color.rgb(255,   0,   0, 25), title= '', group='Fill 2 Down'            , inline='2'            )
i_colDnb = input.color (color.rgb(255, 238,  88, 25), title= '', group='Fill 2 Down'            , inline='2'            )


// ––––––––––––––––––––––––––––––––––––––––––––[             ema's              ]––––––––––––––––––––––––––––––––––––––––––––
ema1     = ta.ema(close, len1 ), ema2  = ta.ema(close, len2 ), ema3  = ta.ema(close, len3 ), ema4  = ta.ema(close, len4 ) 
ema5     = ta.ema(close, len5 ), ema6  = ta.ema(close, len6 ), ema7  = ta.ema(close, len7 ), ema8  = ta.ema(close, len8 ) 
ema9     = ta.ema(close, len9 ), ema10 = ta.ema(close, len10), ema11 = ta.ema(close, len11), ema12 = ta.ema(close, len12)
ema13    = ta.ema(close, len13), ema14 = ta.ema(close, len14), ema15 = ta.ema(close, len15), ema16 = ta.ema(close, len16)
ema17    = ta.ema(close, len17)


// ––––––––––––––––––––––––––––––––––––––––––––[ Put values in array's and sort ]––––––––––––––––––––––––––––––––––––––––––––
arr  = array.from(close, ema1, ema2, ema3, ema4, ema5, ema6, ema7, ema8, ema9, ema10, ema11, ema12, ema13, ema14, ema15, ema16, ema17) 


// ––––––––––––––––––––––––––––––––––––––––––––[           Sort array           ]––––––––––––––––––––––––––––––––––––––––––––
array.sort   (arr, order.ascending)


// ––––––––––––––––––––––––––––––––––––––––––––[              Plot              ]––––––––––––––––––––––––––––––––––––––––––––
// 'Type colour fill' -> '1' or '1 & 2'
p1      = plot(opt == '1' or opt == '1 & 2' ? array.get(arr,  0) : na, color= na, display= display.data_window)
p2      = plot(opt == '1' or opt == '1 & 2' ? array.get(arr, 17) : na, color= na, display= display.data_window)

// 'Type colour fill' -> '2' or '1 & 2'
pClose  = plot(opt == '2' or opt == '1 & 2' ?             close  : na, color= na, display= display.data_window) 
p_ema1  = plot(opt == '2' or opt == '1 & 2' ?             ema1   : na, color= na, display= display.data_window) 
p_ema2  = plot(opt == '2' or opt == '1 & 2' ?             ema2   : na, color= na, display= display.data_window) 
p_ema3  = plot(opt == '2' or opt == '1 & 2' ?             ema3   : na, color= na, display= display.data_window) 
p_ema4  = plot(opt == '2' or opt == '1 & 2' ?             ema4   : na, color= na, display= display.data_window) 
p_ema5  = plot(opt == '2' or opt == '1 & 2' ?             ema5   : na, color= na, display= display.data_window) 
p_ema6  = plot(opt == '2' or opt == '1 & 2' ?             ema6   : na, color= na, display= display.data_window) 
p_ema7  = plot(opt == '2' or opt == '1 & 2' ?             ema7   : na, color= na, display= display.data_window) 
p_ema8  = plot(opt == '2' or opt == '1 & 2' ?             ema8   : na, color= na, display= display.data_window) 
p_ema9  = plot(opt == '2' or opt == '1 & 2' ?             ema9   : na, color= na, display= display.data_window) 
p_ema10 = plot(opt == '2' or opt == '1 & 2' ?             ema10  : na, color= na, display= display.data_window) 
p_ema11 = plot(opt == '2' or opt == '1 & 2' ?             ema11  : na, color= na, display= display.data_window) 
p_ema12 = plot(opt == '2' or opt == '1 & 2' ?             ema12  : na, color= na, display= display.data_window) 
p_ema13 = plot(opt == '2' or opt == '1 & 2' ?             ema13  : na, color= na, display= display.data_window) 
p_ema14 = plot(opt == '2' or opt == '1 & 2' ?             ema14  : na, color= na, display= display.data_window) 
p_ema15 = plot(opt == '2' or opt == '1 & 2' ?             ema15  : na, color= na, display= display.data_window) 
p_ema16 = plot(opt == '2' or opt == '1 & 2' ?             ema16  : na, color= na, display= display.data_window) 
p_ema17 = plot(opt == '2' or opt == '1 & 2' ?             ema17  : na, color= na, display= display.data_window) 


// ––––––––––––––––––––––––––––––––––––––––––––[     switch colors at ema x     ]––––––––––––––––––––––––––––––––––––––––––––
a_ema  = array.from(ema1, ema2, ema3, ema4, ema5, ema6, ema7, ema8, ema9, ema10, ema11, ema12, ema13, ema14, ema15, ema16, ema17)
pickEm = array.get(a_ema, c_e -1)


// ––––––––––––––––––––––––––––––––––––––––––––[          Colour Fill           ]––––––––––––––––––––––––––––––––––––––––––––
// 'Type colour fill' -> '1' or '1 & 2'
colUp  = color.from_gradient(array.get(arr,  2), array.get(arr, 0), array.get(arr, 17), i_colUp1, i_colUp2) // Position array.get(arr,  2) {= 3rd  lowest ema} between lowest & highest values 
colDn  = color.from_gradient(array.get(arr, 15), array.get(arr, 0), array.get(arr, 17), i_colDn1, i_colDn2) // Position array.get(arr, 15) {= 3rd highest ema} between lowest & highest values 
col    = ema1 > pickEm ? colUp : colDn


// 'Type colour fill' -> '2' or '1 & 2'
pickColor(ema)    =>    color.from_gradient(ema, array.get(arr, 0), array.get(arr, 17), close > pickEm ? i_colUpa : i_colDna, close > pickEm ? i_colUpb : i_colDnb)


// ––––––––––––––––––––––––––––––––––––––––––––[              Fill              ]––––––––––––––––––––––––––––––––––––––––––––
// 'Type colour fill' -> '1' or '1 & 2'
fill(p1, p2, color= col)

// 'Type colour fill' -> '2' or '1 & 2'
fill(pClose , p_ema1 , color= pickColor(close))
fill(p_ema1 , p_ema2 , color= pickColor(ema1 ))
fill(p_ema2 , p_ema3 , color= pickColor(ema2 ))
fill(p_ema3 , p_ema4 , color= pickColor(ema3 ))
fill(p_ema4 , p_ema5 , color= pickColor(ema4 ))
fill(p_ema5 , p_ema6 , color= pickColor(ema5 ))
fill(p_ema6 , p_ema7 , color= pickColor(ema6 ))
fill(p_ema7 , p_ema8 , color= pickColor(ema7 ))
fill(p_ema8 , p_ema9 , color= pickColor(ema8 ))
fill(p_ema9 , p_ema10, color= pickColor(ema9 ))
fill(p_ema10, p_ema11, color= pickColor(ema10))
fill(p_ema11, p_ema12, color= pickColor(ema11))
fill(p_ema12, p_ema13, color= pickColor(ema12))
fill(p_ema13, p_ema14, color= pickColor(ema13))
fill(p_ema14, p_ema15, color= pickColor(ema14))
fill(p_ema15, p_ema16, color= pickColor(ema15))
fill(p_ema16, p_ema17, color= pickColor(ema16))
