MACD Pulse Check
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TheRealDrip2Rip

//@version=5
indicator("MACD Pulse Check", shorttitle="MACD Pulse Check", overlay=false)

// Time filter for alerts
allow_alerts = time(timeframe.period, "0955-1550")
allow_alerts1 = time(timeframe.period, "0930-1550")

// Pre-defined MACD Inputs
fastLength1 = 12
slowLength1 = 26
signalLength1 = 9
fastLength2 = 5
slowLength2 = 13
signalLength2 = 5
fastLength3 = 8
slowLength3 = 21
signalLength3 = 8
fastLength4 = 9
slowLength4 = 19
signalLength4 = 6
fastLength5 = 7
slowLength5 = 15
signalLength5 = 7
fastLength6 = 10
slowLength6 = 30
signalLength6 = 10

////////Overall Trend overlay
primaryFast = 12
primarySlow = 26
primarySignal = 9

secondaryFast = 5
secondarySlow = 13
secondarySignal = 5

tertiaryFast = 8
tertiarySlow = 21
tertiarySignal = 8

// User-defined input for threshold
userThreshold = input.float(0.001, title="Sideways Threshold", minval=0.001, step=0.0001)

// Calculate MACD values
[macdLine1, signalLine1, _] = ta.macd(close, fastLength1, slowLength1, signalLength1)
[macdLine2, signalLine2, _] = ta.macd(close, fastLength2, slowLength2, signalLength2)
[macdLine3, signalLine3, _] = ta.macd(close, fastLength3, slowLength3, signalLength3)
[macdLine4, signalLine4, _] = ta.macd(close, fastLength4, slowLength4, signalLength4)
[macdLine5, signalLine5, _] = ta.macd(close, fastLength5, slowLength5, signalLength5)
[macdLine6, signalLine6, _] = ta.macd(close, fastLength6, slowLength6, signalLength6)

// Overlay MACD Value calculation
// Calculate MACD values
[primaryMacd, primarySignalLine, _] = ta.macd(close, primaryFast, primarySlow, primarySignal)
[secondaryMacd, secondarySignalLine, _] = ta.macd(close, secondaryFast, secondarySlow, secondarySignal)
[tertiaryMacd, tertiarySignalLine, _] = ta.macd(close, tertiaryFast, tertiarySlow, tertiarySignal)

// Calculate the average of all four MACD lines
avgMACD = (macdLine1 + macdLine2 + macdLine3 + macdLine4 + macdLine5 + macdLine6) / 6

// Overlay MACD average calculation
averageMacd = (primaryMacd + secondaryMacd + tertiaryMacd) / 3

// Plot the combined signal line
combinedSignal = avgMACD - ta.sma(avgMACD, 9)

// Overlay signal line
// Plot the combined signal line
consolidatedSignal = averageMacd - ta.sma(averageMacd, 9)

// Determine if price action is sideways
price_5_candles_ago = ta.valuewhen(1, close, 4)
isSideways = math.abs(close - price_5_candles_ago) <= price_5_candles_ago * userThreshold

// Define color based on trend and sideways condition
signalColor = isSideways ? color.gray : (combinedSignal > 0 ? color.green : color.red)

// Plot shape based on signalColor
plotshape(allow_alerts1, color=signalColor, style=shape.circle, size=size.tiny, location=location.top)

// Plot overlay
plotshape(allow_alerts1 and consolidatedSignal < 0, color=color.rgb(255, 82, 82, 55), style=shape.circle, size=size.tiny, location = location.top) 
// Plot green circle when all values are above the zero line
plotshape(allow_alerts1 and consolidatedSignal > 0, color=color.rgb(76, 175, 80, 55), style=shape.circle, size=size.tiny, location = location.top)
