Tosch Stacked EMAs (Fibonacci)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AirshipPirate

//@version=5
library("Tosch_Stacked_EMAs", overlay=true)

//----------------------------
ma(length) => ta.ema(close,length)

float ema05 = ma(5) 
float ema08 = ma(8)
float ema13 = ma(13)
float ema21 = ma(21)
float ema34 = ma(34)
float ema55 = ma(55)
float ema89 = ma(89)

//----------------------------
// array with true/false values if the MA-pairs are lined up (true) or not (false)
the_bear_truth() =>
    the_array = array.from( (ema05<=ema08), (ema08<=ema13), (ema13<=ema21), (ema21<=ema34), (ema34<=ema55), (ema55<=ema89) )
    the_array

the_bull_truth() =>
    the_array = array.from( (ema05>=ema08), (ema08>=ema13), (ema13>=ema21), (ema21>=ema34), (ema34>=ema55), (ema55>=ema89) )
    the_array
    
bool[] ar_bear = the_bear_truth()
bool[] ar_bull = the_bull_truth()

//----------------------------
get_array(ar) =>
    the_ar = array.copy(ar)

bool[] bull_ar = get_array(ar_bull)    
bool[] bear_ar = get_array(ar_bear)


//----------------------------
// count how many MAs are lined up 
stack_count(ar) =>
    the_count = 0
    for el in ar
        the_count += (el ? 1 : 0)
    the_count
    
int bear_count = stack_count(ar_bear)
int bull_count = stack_count(ar_bull)


//----------------------------
// MAs stacked?
bool emastack_bear = (bear_count == 6)
bool emastack_bull = (bull_count == 6)



//----------------------------
// @function Returns the EMA values for lengths 5, 8, 13, 21, 34, 55, 89
export emas() => [ema05,ema08,ema13,ema21,ema34,ema55,ema89]
    
//----------------------------
// @function Returns true if all EMAs are stacked, either way.
export stacked() =>  (emastack_bear or emastack_bull)

//----------------------------
// @function Returns true if the EMAs are stacked bullish/bearish, false otherwise
export bullish() =>	emastack_bull
export bearish() =>	emastack_bear

//----------------------------
// @function Returns the number of stacked EMAs
export bear_count() => bear_count
export bull_count() => bull_count

//----------------------------
// @function Returns the arrays of the true/false values if one EMA is below/above the next one 
//  bear: [ (ema05<=ema08), (ema08<=ema13), (ema13<=ema21), (ema21<=ema34), (ema34<=ema55), (ema55<=ema89) ]
//  bull: [ (ema05>=ema08), (ema08>=ema13), (ema13>=ema21), (ema21>=ema34), (ema34>=ema55), (ema55>=ema89) ]
export bear_bool_array() => bear_ar
export bull_bool_array() => bull_ar


//-------------------------------------------------------------------------------------------------
show_ema = input.bool(defval=true,  title="Show Fibonacci EMAs", group="Show Indicator", inline="FibEMA")
colo_ema = input.bool(defval=true,  title="Color Bars",          group="Show Indicator", inline="FibEMA")
show_cnt = input.bool(defval=true,  title="Show counters",       group="Show Indicator")

//----------------------------
// plot all lines
plot(ema05, style=plot.style_line,    color=color.new(color.orange, 10), display=show_ema?display.all:display.none, linewidth=2)
plot(ema08, style=plot.style_line,    color=color.new(color.white,  20), display=show_ema?display.all:display.none)
plot(ema13, style=plot.style_line,    color=color.new(color.white,  30), display=show_ema?display.all:display.none)
plot(ema21, style=plot.style_circles, color=color.new(color.white,  10), display=show_ema?display.all:display.none, linewidth=2)
plot(ema34, style=plot.style_line,    color=color.new(color.white,  50), display=show_ema?display.all:display.none)
plot(ema55, style=plot.style_line,    color=color.new(color.white,  60), display=show_ema?display.all:display.none)
plot(ema89, style=plot.style_line,    color=color.new(color.lime,   10), display=show_ema?display.all:display.none, linewidth=2)


//----------------------------
// color the bars
the_col = color.yellow
if (emastack_bear or emastack_bull)
    if emastack_bull
        the_col := color.green
    else
        the_col := color.red
barcolor(the_col, display=colo_ema?display.all:display.none)



//----------------------------
// display counters
label.new(bar_index, math.max(ema05,ema89), "bull\n"+str.tostring(bull_count)+"\nbear\n"+str.tostring(bear_count), style=show_cnt?label.style_label_down:label.style_none, textcolor=color.white)
