S2BU2 MACD Growthpercentages
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sucks2bu2
// feel free to use this code but do not stick a pricetag on it and sell it under a different name by itself ...

//@version=5
indicator(title='S2BU2 MACD Growthpercentages', shorttitle='S2BU2 MC%', timeframe='', timeframe_gaps=true)



//=========================================presets=========================================//
//Set MA_lengths
periodFast = input.int(title='Fast Length', defval=12, minval=1)
periodSlow = input.int(title='Slow Length', defval=26, minval = 1)
periodSignal = input.int(title='Signal Smoothing',  minval = 1, maxval = 50, defval = 9)

//set data_source
src = input(title='Source', defval=close)

//set histogram colors
greaterHist = input(title = 'Greater', group = 'style', defval = #1d6ff2)
lowerHist = input(title = 'Lower', group = 'style', defval = #7aa7f0)



//===================================calculate base data===================================//
// Calculate macd, signal, fast and slow MA
float fastMA = ta.ema(src, periodFast)
float slowMA = ta.ema(src, periodSlow)
float macd = fastMA - slowMA
float signal = ta.ema(macd, periodSignal)



//==================================calculate helper vars==================================//
//check mathematical signs of macd and signal 
bool isSignalPositive = signal > 0
bool isMacdPositive = macd > 0
    
//determine relative pos between signal and macd
bool isMacdGreater = macd > signal 



//========================================chartData========================================//
//calculate percentage based on position and sign of macd and signal
//signal + macd above 0-line, macd > signal
float hist =  if (isSignalPositive == true and isMacdPositive == true and isMacdGreater == true)
    signal/macd
else
    //signal + macd above 0-line, macd < signal
    if (isSignalPositive == true and isMacdPositive == true and isMacdGreater == false)
        macd/signal
        
    else
        //signal + macd below 0-line, macd > signal
        if (isSignalPositive == false and isMacdPositive == false and isMacdGreater == true)
        
            //mirror macd + signal above 0-line, offset macd further above by new signal delta relative to 0-line
            (math.abs(signal)/(math.abs(macd)+2*math.abs(signal)))
            
        else
            //signal + macd below 0-line, macd < signal
            if (isSignalPositive == false and isMacdPositive == false and isMacdGreater == false)
                
                //mirror macd + signal above 0-line, offset signal further above by new macd delta relative to 0-line
                (math.abs(macd)/(math.abs(signal) + 2*math.abs(macd)))
                
            else
                //signal below 0-line, macd above 0-line
                if (isSignalPositive == false and isMacdPositive == true)
                    
                    //mirror signal above 0-line, offset macd further above by new signal delta relative to 0-line
                    (math.abs(signal)/(macd + 2*math.abs(signal)))
                    
                else
                    //signal above 0-line, macd below 0-line
                    if (isSignalPositive == true and isMacdPositive == false)
                        
                        //mirror macd above 0-line, offset signal further above by new macd delta relative to 0-line
                        (math.abs(macd)/(signal + 2*math.abs(macd)))



//========================================misc========================================//  
//select bar color depending on increased/decreased size of bar
colorHist = nz(hist[1]) < hist ? greaterHist:lowerHist

//plot bars with 100 multiplier for visibility
plot(hist*100, title='Bars', style=plot.style_columns, color=colorHist)
