MACD Fake Filter [RH]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © HasanRifat

//@version=5
indicator("MACD Fake Filter [RH]", max_labels_count = 500)

// MACD inputs
fastLength   = input(defval=12, title="Fast Length")
slowLength   = input(defval=26, title="Slow Length")
src          = input(defval=close, title="Source")
signalLength = input.int(defval = 9, title="Signal Smoothing",  minval = 1, maxval = 50)
smaSource    = input.string(defval="EMA", title="Oscillator MA Type", options=["SMA", "EMA"])
smaSignal    = input.string(defval="EMA", title="Signal Line MA Type", options=["SMA", "EMA"])

// MACD calculation
fastMA = smaSource == "SMA" ? ta.sma(src, fastLength) : ta.ema(src, fastLength)
slowMA = smaSource == "SMA" ? ta.sma(src, slowLength) : ta.ema(src, slowLength)
macd   = fastMA - slowMA
signal = smaSignal == "SMA" ? ta.sma(macd, signalLength) : ta.ema(macd, signalLength)
hist   = macd - signal

var positiveHeightArray = array.new_float()
var poitiveHeightAvg    = array.new_float()
var negativeHeightArray = array.new_float()
var negativeHeightAvg   = array.new_float()
var float positiveTop   = na 
var float negatieTop    = na 
var int activeWave      = na 

// MACD average wave height calculation
macdZeroCrossOver  = macd > 0 and macd[1] < 0
macdZeroCrossUnder = macd < 0 and macd[1] > 0

if macdZeroCrossOver
    activeWave := 1

if macdZeroCrossUnder
    activeWave := -1

if activeWave == 1
    negatieTop := array.min(negativeHeightArray)
    array.push(positiveHeightArray, macd)
    array.push(negativeHeightAvg, negatieTop)
    array.clear(negativeHeightArray)

if activeWave == -1
    positiveTop := array.max(positiveHeightArray)
    array.push(negativeHeightArray, macd)
    array.push(poitiveHeightAvg, positiveTop)
    array.clear(positiveHeightArray)

positiveAverageHeight = array.avg(poitiveHeightAvg)
negativeAverageHeight = array.avg(negativeHeightAvg)

bearishCrossunder = ta.crossunder(macd, signal)
bullishCrossover  = ta.crossover(macd, signal)

crossunderAboveAvg = bearishCrossunder and macd > positiveAverageHeight
crossaboveBelowAvg = bullishCrossover and macd < negativeAverageHeight

// Labels and plotting
if crossunderAboveAvg
    label.new(bar_index, macd[1], text = "", color = color.red)
if crossaboveBelowAvg
    label.new(bar_index, macd[1], text = "", color = color.green, style = label.style_label_up)

hline(0, "Zero Line", color=color.new(#5171e6, 50))
plot(hist, title="Histogram", style=plot.style_histogram, color=(hist>=0 ? (hist[1] < hist ? #32cd32 : #008000) : (hist[1] < hist ? #800000 : #ff0000)))
mdPlot = plot(macd, title="MACD", color=#2962FF, linewidth = 2)
sgPlot = plot(signal, title="Signal", color=#FF6D00)
psHeight = plot(positiveAverageHeight, color = color.green, title = "Positive Avg Height")
ngHeight = plot(negativeAverageHeight, color = color.red, title = "Negative Avg Height")

fill(plot1 = mdPlot, plot2 = psHeight, title = "Positive Wave", color = macd > positiveAverageHeight ? (macd[3] < macd ? color.new(color.green, 60) : color.new(color.green, 80)) : na)
fill(plot1 = mdPlot, plot2 = ngHeight, title = "Negative Wave", color = macd < negativeAverageHeight ? (macd[3] < macd ? color.new(color.red, 80) : color.new(color.red, 60)) : na)

// Alerts
alertcondition(crossunderAboveAvg, title = "Crossunder Above Average", message = "Crossunder")
alertcondition(crossaboveBelowAvg, title = "Crossover Below Average", message = "Crossover")
