Impulse Momentum MACD - Slow and Fast
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © DOC-STRATEGY
//██████╗░░█████╗░░█████╗░ ░██████╗████████╗██████╗░░█████╗░████████╗███████╗░██████╗░██╗░░░██╗
//██╔══██╗██╔══██╗██╔══██╗ ██╔════╝╚══██╔══╝██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝░╚██╗░██╔╝
//██║░░██║██║░░██║██║░░╚═╝ ╚█████╗░░░░██║░░░██████╔╝███████║░░░██║░░░█████╗░░██║░░██╗░░╚████╔╝░
//██║░░██║██║░░██║██║░░██╗ ░╚═══██╗░░░██║░░░██╔══██╗██╔══██║░░░██║░░░██╔══╝░░██║░░╚██╗░░╚██╔╝░░
//██████╔╝╚█████╔╝╚█████╔╝ ██████╔╝░░░██║░░░██║░░██║██║░░██║░░░██║░░░███████╗╚██████╔╝░░░██║░░░
//╚═════╝░░╚════╝░░╚════╝░ ╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚══════╝░╚═════╝░░░░╚═╝░░░
//𓂀𝔻𝕆ℂ 𝕊𝕋ℝ𝔸𝕋𝔼𝔾𝕐𓂀
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//@version=5
indicator('Impulse Momentum MACD - Slow and Fast', shorttitle='Impulse Momentum MACD - Slow and Fast', timeframe='')
DOC_Strategy    = "MA IMPULSE MACD - SLOW"
DOC_Strategy_II = "MA IMPULSE MACD - FAST"
ma(sourceXD, lengthXD, type) =>
     type == "SMA"          ? ta.sma (sourceXD, lengthXD)    : 
     type == "EMA"          ? ta.ema (sourceXD, lengthXD)    :
     type == "SMMA (RMA)"   ? ta.rma (sourceXD, lengthXD)    : 
     type == "WMA"          ? ta.wma (sourceXD, lengthXD)    :     
     type == "VWMA"         ? ta.vwma(sourceXD, lengthXD)    :
     type == "HMA"          ? ta.wma (2 * ta.wma(sourceXD, lengthXD / 2) - ta.wma(sourceXD, lengthXD), math.floor(math.sqrt(lengthXD))) :
     na

// Data Entry///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Define the length of the momentum indicator
Momentum_Length             = input.int   (title='Momentum Length'    , minval=1, defval= 14,                                                              group='Momentum')
Momentum_Source             = input.source(close  ,     "Momentum Source",                                                                                 group='Momentum')
ColorMomentumL              = input.color (#0ebb9e,   'Momentum Line',                                                                                   group='Momentum')
ColorMomentumS              = input.color (#cd0a68 ,  'Momentum Line',                                                                                   group='Momentum') 

ColorMomentumLLL            = input.color (#0ebb8426, 'Momentum Area',                                                                                   group='Momentum')
ColorMomentumSSS            = input.color (#bb0e6424 ,'Momentum Area',                                                                                   group='Momentum')


// Calculate the momentum indicator using the ta.mom function
Momentum     = ta.mom(Momentum_Source, Momentum_Length)/2.6


// Color - Momentum
Color_Momentum       = Momentum     >= 0 ? ColorMomentumL    : ColorMomentumS
Color_Momentum_Area  = Momentum     >= 0 ? ColorMomentumLLL  : ColorMomentumSSS 

// Plot the momentum indicator using the plot function
plot(Momentum,     color=Color_Momentum,      linewidth=1,  title='Standard Momentum Indicator',                        display=display.all)
plot(Momentum,     color=Color_Momentum_Area, linewidth=2,  title='Momentum Indicator - Area', style=plot.style_areabr, display=display.all)


// Data Entry////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Period to calculate main line value
Calculation_Period_For_Line = input.int(defval=10, title='Period to calculate the Trend Line', minval=1,                                                  group='Trend Line')
Line_ColorLB                = input.color (color.rgb(15, 200, 139), "Long/Buy"     ,                                                                    group='Trend Line')
Line_ColorSS                = input.color (color.rgb(202, 15, 121), "Short/Sell"   ,                                                                    group='Trend Line')
TFTL                        = input.timeframe('', title='Time Frame the Trend Line - Tickets (Minutes: 1=5, 3=15, 5=30, 15=60)',                          group='Trend Line')

o  = request.security (syminfo.tickerid, TFTL, open,  barmerge.gaps_off, barmerge.lookahead_on)
c  = request.security (syminfo.tickerid, TFTL, close, barmerge.gaps_off, barmerge.lookahead_on)
h  = request.security (syminfo.tickerid, TFTL, high,  barmerge.gaps_off, barmerge.lookahead_on)
l  = request.security (syminfo.tickerid, TFTL, low,   barmerge.gaps_off, barmerge.lookahead_on)

// Function to calculate line value////////////////////////////////////////////////////////////////////////////////////////////
Calculate_Line(Period) =>
    float highS = ta.highest(Period) or  h > c  ? h : na
    float lowS  = ta.lowest (Period) or  l < c ?  l : na

    int trend = 0
    trend := c > h[1] ? 1 : c < l[1] ? -1 : nz(trend[1])
    trend

// Function to draw line on chart//////////////////////////////////////////////////////////////////////////////////////////////
Draw_Line(Period, Main_Trend) =>
    float highS = ta.highest(Period) or  h > c ?  h : na
    float lowS  = ta.lowest (Period) or  l < c ?  l : na

    int trend = 0
    trend := c > h[1] ? 1 : c < l[1] ? -1 : nz(trend[1])
    Main_Trend == 1 ? trend == 1 ? Line_ColorLB : Line_ColorLB : Main_Trend == -1 ? trend == -1 ? Line_ColorSS : Line_ColorSS : na

// Calculate main line value///////////////////////////////////////////////////////////////////////////////////////////////////
Main_Trend = Calculate_Line(Calculation_Period_For_Line)

// Lines - UP and Down////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// Line - Up
hline(0.050,  title='Up level',   linestyle=hline.style_dotted, color=#f60c0c, display=display.none)
hline(0.1,    title='Up level',   linestyle=hline.style_dotted, color=#989898, display=display.none)

// Line - Down
hline(-0.050, title='Down Level', linestyle=hline.style_dotted, color=#2b9f1e, display=display.none)
hline(-0.1,   title='Down Level', linestyle=hline.style_dotted, color=#989898, display=display.none)

// Data Entry/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ma1_type         = input.string("SMA"  , "               "    ,             inline="MAS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy   )
ma2_type         = input.string("EMA"  , "               "    ,             inline="MAS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy   )
ma3_type         = input.string("HMA"  , "               "    ,             inline="MAS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy   )
ma4_type         = input.string("SMA"  , "               "    ,             inline="MAF #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)
ma5_type         = input.string("SMA"  , "               "    ,             inline="MAF #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)
ma6_type         = input.string("WMA"  , "               "    ,             inline="MAF #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

lengthMA         = input       (34,                 title='Impulse MACD - Slow',                                                                           group='Impulse MACD - Slow') 
lengthSignal     = input       (9,                  title='Signal Impulse MACD - Slow',                                                                    group='Impulse MACD - Slow')
colorLookback    = input.int   (2,  minval=0,       title='Color Look Back',                                                                               group='Impulse MACD - Slow') 

hist_color0      = input.color (color.new(#22ff00, 20), 'Positive Rising',                                                                               group='Histogram: Impulse MACD - Slow')  // positive rising
hist_color1      = input.color (color.new(#2ff7f7, 30), 'Positive Falling',                                                                              group='Histogram: Impulse MACD - Slow')  // positive falling
hist_color2      = input.color (color.new(#f82e71, 30), 'Negative Falling',                                                                              group='Histogram: Impulse MACD - Slow')  // negative falling
hist_color3      = input.color (color.new(#ff0000, 20), 'Negative Rising',                                                                               group='Histogram: Impulse MACD - Slow')  // negative rising

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

lengthMAS        = input       (15,                 title='Impulse MACD - Fast',                                                                           group='Impulse MACD - Fast') 
lengthSignalS    = input       (5,                  title='Signal Impulse MACD - Fast',                                                                    group='Impulse MACD - Fast')
colorLookbackS   = input.int   (2,  minval=0,       title='Color Look Back',                                                                               group='Impulse MACD - Fast') 

hist_color0S     = input.color (color.new(#0800ff, 20), 'Positive Rising',                                                                               group='Histogram: Impulse MACD - Fast')  // positive rising
hist_color1S     = input.color (color.new(#000000, 30), 'Positive Falling',                                                                              group='Histogram: Impulse MACD - Fast')  // positive falling
hist_color2S     = input.color (color.new(#000000, 31), 'Negative Falling',                                                                              group='Histogram: Impulse MACD - Fast')  // negative falling
hist_color3S     = input.color (color.new(#0800ff, 21), 'Negative Rising',                                                                               group='Histogram: Impulse MACD - Fast')  // negative rising

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//Impulse MACD - Slow
calc_smma(src, len) =>
    smma = 0.0
    sma_1 = ma(src, len, ma1_type)
    smma := na(smma[1]) ? sma_1 : (smma[1] * (len - 1) + src) / len
    smma

//Impulse MACD - Fast
calc_smmaS(srcS, lenS) =>
    smmaS = 0.0
    sma_1S = ma(srcS, lenS, ma4_type)
    smmaS := na(smmaS[1]) ? sma_1S : (smmaS[1] * (lenS - 1) + srcS) / lenS
    smmaS

//Impulse MACD - Slow
calc_zlema(src, length) =>
    ema1 = ma(src, length, ma2_type)
    ema2 = ma(ema1, length, ma2_type)
    d = ema1 - ema2
    ema1 + d

//Impulse MACD - Fast
calc_zlemaS(srcS, lengthS) =>
    ema1S = ma(srcS, lengthS, ma5_type)
    ema2S = ma(ema1S, lengthS, ma5_type)
    dS = ema1S - ema2S
    ema1S + dS

//Impulse MACD - Slow
srcWW = hlc3
hi = calc_smma(high, lengthMA)
lo = calc_smma(low, lengthMA)
mi = calc_zlema(srcWW, lengthMA)

//Impulse MACD - Fast
srcWWS = hlc3
hiS = calc_smma(high, lengthMAS)
loS = calc_smma(low, lengthMAS)
miS = calc_zlema(srcWWS, lengthMAS)

//Impulse MACD - Slow
md = mi > hi ? mi - hi : mi < lo ? mi - lo : 0
sb = ma(md, lengthSignal, ma3_type)

sh = md - sb

is_rising  = ta.rising(sh, colorLookback)
is_falling = ta.falling(sh, colorLookback)

//Impulse MACD - Fast
mdS = miS > hiS ? miS - hiS : miS < loS ? miS - loS : 0
sbS = ma(mdS, lengthSignalS, ma6_type)

shS = mdS - sbS

is_risingS  = ta.rising(shS, colorLookbackS)
is_fallingS = ta.falling(shS, colorLookbackS)

//Impulse MACD - Slow
histColor  = colorLookback  == 0 ? sh  > 0 ? hist_color0  : hist_color3  : sh  > 0 ? is_rising ? hist_color0   : hist_color1 :  is_falling  ? hist_color3  : hist_color2

mdc  = srcWW  > mi  ? srcWW  > hi  ? color.rgb(4, 255, 0)  : color.rgb(2, 252, 164, 30) : srcWW  < lo   ? color.rgb(255, 2, 133) : color.rgb(255, 4, 163, 30)

//Impulse MACD - Fast
histColorS = colorLookbackS == 0 ? shS > 0 ? hist_color0S : hist_color3S : shS > 0 ? is_risingS ? hist_color0S : hist_color1S : is_fallingS ? hist_color3S : hist_color2S

mdcS = srcWWS > miS ? srcWWS > hiS ? color.rgb(21, 0, 255) : color.rgb(0, 140, 255, 30) : srcWWS < loS  ? color.rgb(255, 0, 238) : color.rgb(255, 0, 217, 30)

//GRAPHICS/////////////////////////////////////////////////////////////////////////////

//Plot - Middle Line
plot(0, color=color.new(#65665c, 0), linewidth=1, title='Mid Line')

//Plot - Columns Slow And Fast
//Fast
plot(shS, style=plot.style_columns, color=histColorS)
//Slow
plot(sh,  style=plot.style_columns, color=histColor)

//Plot - Fast Lines
plot(mdS, color=mdcS, linewidth=3, title='Impulse MACD - Fast', style=plot.style_line)
plot(sbS, color=color.new(#ffffff, 0), linewidth=3, title='Impulse MACD Signal - Fast')

//Plot - Slow Lines
plot(md,  color=mdc,  linewidth=3, title='Impulse MACD - Slow', style=plot.style_line)
plot(sb,  color=color.new(#ccf309, 0), linewidth=3, title='Impulse MACD Signal - Slow')

//Plot Line Zero
plot(    0, color=Draw_Line(Calculation_Period_For_Line - 9, Main_Trend), title="Zero Trend Line 𓂀",   style=plot.style_stepline, histbase=   0, linewidth=4)

//Bar Colors - Slow and Fast///////////////////////////////////////////////////////////

//Slow
ebc  = input(false, title='Enable Bar Colors: Impulse MACD - Slow', group='Impulse MACD - Slow') 
barcolor(ebc  ? mdc  : na)
//Fast
ebcS = input(false, title='Enable Bar Colors: Impulse MACD - Fast', group='Impulse MACD - Fast') 
barcolor(ebcS ? mdcS : na)

//////////////////////////////////////////////////////////////////////////////////////

//The End////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////