Moving Averages Histogram
//  This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License https://creativecommons.org/licenses/by-sa/4.0/
//@version=5
/// © dman103
// An interesting idea is to simplify the display of whether ONE fast-moving average crosses FIVE other slower-moving averages using just a histogram.

//The idea is to increase the step counter by 1 every time a fast-moving average crosses over one of the five slower-moving averages and 
//decrease the step counter by 1 every time the fast-moving average crosses down each one of the other five slower moving averages.
// ==== Step System explained ====
// The step system is quite straightforward, every time the FAST moving average crosses one of the slower moving average step counters is increased by 1 until reaching the maximum of 5 (we have 5 slower-moving averages)
//===Cut To Chase=== 
// If the histogram is at the value of 5 (green), it means FAST moving averages is ABOVE ALL slower-moving averages, Hench, the asset is up trending.
// If the histogram is at the value of 0 (red), it means FAST moving average is BELOW ALL slower-moving averages, Hench, the asset is down trending.
//Midway area is the area where you can see how the fast-moving average is cross the slower moving averages.

//You change from a variety of moving averages like EMA, ALMA, HMA, and so on.
//You can reduce the number of slow moving average by placing the same length.
//Note that fast-moving average should have the lowest length.
//You can visualize the moving averages in case you want to see how it works behind, by going to settings and clicking 'Show MA lines'.
//Every moving average length can be modified inside settings.

indicator(title='Moving Averages Histogram')
ma_type = input.string(title='Moving Average Type', defval='RMA', options=['RMA', 'SMA', 'EMA', 'WMA', 'ALMA', 'HMA', 'JMA', 'VMA', 'WWMA', 'SMMA'])

length_fast = input.int(7, minval=1, title='Fast MA Length')
length_1 = input.int(10, minval=1, title='Slow MA Length 1')
length_2 = input.int(20, minval=1, title='Slow MA Length 2')
length_3 = input.int(50, minval=1, title='Slow MA Length 3')
length_4 = input.int(100, minval=1, title='Slow MA Length 4')
length_5 = input.int(200, minval=1, title='Slow MA Length 5')
show_mas = input.bool(false, title="Show MA lines")
source_to_use = input(close, title='Source')
////// Moving averages function
ma_function(source, length, ma_seleted_type) =>
    if ma_seleted_type == 'RMA'
        ta.rma(source, length)
    else
        if ma_seleted_type == 'SMA'
            ta.sma(source, length)
        else
            if ma_seleted_type == 'EMA'
                ta.ema(source, length)
            else
                if ma_seleted_type == 'WMA'
                    ta.wma(source, length)
                else
                    if ma_seleted_type == 'HMA' 
                        ta.wma(2 * ta.wma(source, length / 2) - ta.wma(source, length), math.round(math.sqrt(length)))
                    else
                        if ma_seleted_type == 'ALMA'
                            ta.alma(source, length, 0.85, 6)
                        else
                            if ma_seleted_type == 'JMA'
                                beta = 0.45 * (length - 1) / (0.45 * (length - 1) + 2)
                                alpha = beta
                                tmp0 = 0.0
                                tmp1 = 0.0
                                tmp2 = 0.0
                                tmp3 = 0.0
                                tmp4 = 0.0
                                tmp0 := (1 - alpha) * source + alpha * nz(tmp0[1])
                                tmp1 := (source - tmp0[0]) * (1 - beta) + beta * nz(tmp1[1])
                                tmp2 := tmp0[0] + tmp1[0]
                                tmp3 := (tmp2[0] - nz(tmp4[1])) * (1 - alpha) * (1 - alpha) + alpha * alpha * nz(tmp3[1])
                                tmp4 := nz(tmp4[1]) + tmp3[0]
                                tmp4
                            else
                                if ma_seleted_type == 'VMA'
                                    valpha = 2 / (length + 1)
                                    vud1 = source > source[1] ? source - source[1] : 0
                                    vdd1 = source < source[1] ? source[1] - source : 0
                                    vUD = math.sum(vud1, length)  // was 9 constant
                                    vDD = math.sum(vdd1, length)  // was 9 constant
                                    vCMO = nz((vUD - vDD) / (vUD + vDD))
                                    VAR = 0.0
                                    VAR := nz(valpha * math.abs(vCMO) * source) + (1 - valpha * math.abs(vCMO)) * nz(VAR[1])
                                    VAR
                                else
                                    if ma_seleted_type == 'WWMA'
                                        wwalpha = 1 / length
                                        WWMA = 0.0
                                        WWMA := wwalpha * source + (1 - wwalpha) * nz(WWMA[1])
                                        WWMA
                                    else
                                        if ma_seleted_type == 'SMMA'
                                            smma = float(0.0)
                                            smaval = ta.sma(source, length)
                                            smma := na(smma[1]) ? smaval : (smma[1] * (length - 1) + source) / length
                                            smma


      
      
ma_fast=ma_function(source_to_use,length_fast,ma_type)
ma_1=ma_function(source_to_use,length_1,ma_type)
ma_2=ma_function(source_to_use,length_2,ma_type)
ma_3=ma_function(source_to_use,length_3,ma_type)
ma_4=ma_function(source_to_use,length_4,ma_type)
ma_5=ma_function(source_to_use,length_5,ma_type)
varip int counter = 0
var color Pcolor = na
ma_fast_cross_over_1= ma_fast>ma_1 and ma_fast[1]<=ma_1[1]
ma_fast_cross_under_1= ma_fast<ma_1 and ma_fast[1]>=ma_1[1]
ma_fast_cross_over_2=ma_fast>ma_2 and ma_fast[1]<=ma_2[1]
ma_fast_cross_under_2=ma_fast<ma_2 and ma_fast[1]>=ma_2[1]
ma_fast_cross_over_3=ma_fast>ma_3 and ma_fast[1]<=ma_3[1]
ma_fast_cross_under_3=ma_fast<ma_3 and ma_fast[1]>=ma_3[1]
ma_fast_cross_over_4=ma_fast>ma_4 and ma_fast[1]<=ma_4[1]
ma_fast_cross_under_4=ma_fast<ma_4 and ma_fast[1]>=ma_4[1]
ma_fast_cross_over_5=ma_fast>ma_5 and ma_fast[1]<=ma_5[1]
ma_fast_cross_under_5=ma_fast<ma_5 and ma_fast[1]>=ma_5[1]
if(ma_fast_cross_over_1)
    counter:=math.min(counter+1,5)
if(ma_fast_cross_over_2)
    counter:=math.min(counter+1,5)
if(ma_fast_cross_over_3)
    counter:=math.min(counter+1,5)
if(ma_fast_cross_over_4)
    counter:=math.min(counter+1,5)
if(ma_fast_cross_over_5)
    counter:=math.min(counter+1,5)
if (ma_fast_cross_under_1)
    counter:=math.max(counter-1,0)
if (ma_fast_cross_under_2)
    counter:=math.max(counter-1,0)
if (ma_fast_cross_under_3)
    counter:=math.max(counter-1,0)
if (ma_fast_cross_under_4)
    counter:=math.max(counter-1,0)
if (ma_fast_cross_under_5)
    counter:=math.max(counter-1,0)
Pcolor:=color.from_gradient(counter, 0, 5, color.new(color.red, 50), color.new(color.green, 0))
plot(show_mas==false ? counter : na, color=color.new(Pcolor, 0), style=plot.style_columns,linewidth=3)
plot(show_mas ? ma_fast : na ,color=color.yellow, linewidth=2)
plot(show_mas ? ma_1 : na ,color=color.orange)
plot(show_mas ? ma_2: na ,color=color.blue)
plot(show_mas ? ma_3 : na ,color=color.lime)
plot(show_mas ? ma_4 : na ,color=color.purple)
plot(show_mas ? ma_5 : na ,color=color.navy)

