Stochastic 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/
//
//██████╗░░█████╗░░█████╗░ ░██████╗████████╗██████╗░░█████╗░████████╗███████╗░██████╗░██╗░░░██╗
//██╔══██╗██╔══██╗██╔══██╗ ██╔════╝╚══██╔══╝██╔══██╗██╔══██╗╚══██╔══╝██╔════╝██╔════╝░╚██╗░██╔╝
//██║░░██║██║░░██║██║░░╚═╝ ╚█████╗░░░░██║░░░██████╔╝███████║░░░██║░░░█████╗░░██║░░██╗░░╚████╔╝░
//██║░░██║██║░░██║██║░░██╗ ░╚═══██╗░░░██║░░░██╔══██╗██╔══██║░░░██║░░░██╔══╝░░██║░░╚██╗░░╚██╔╝░░
//██████╔╝╚█████╔╝╚█████╔╝ ██████╔╝░░░██║░░░██║░░██║██║░░██║░░░██║░░░███████╗╚██████╔╝░░░██║░░░
//╚═════╝░░╚════╝░░╚════╝░ ╚═════╝░░░░╚═╝░░░╚═╝░░╚═╝╚═╝░░╚═╝░░░╚═╝░░░╚══════╝░╚═════╝░░░░╚═╝░░░
//𓂀𝔻𝕆ℂ 𝕊𝕋ℝ𝔸𝕋𝔼𝔾𝕐𓂀
///////////////////////////////////////////////////////////////////////////////////

//@version=5
indicator(title='Stochastic MACD - Slow and Fast', shorttitle='Stochastic MACD - Slow and Fast 𓂀', precision=4, timeframe='')

DOC_Strategy    = "SLOW - STOCHASTIC MACD"
DOC_Strategy_II = "FAST - STOCHASTIC MACD"
ma(source, length, type) =>
     type == "SMA"          ? ta.sma(source, length)    : 
     type == "EMA"          ? ta.ema(source, length)    :
     type == "SMMA (RMA)"   ? ta.rma(source, length)    : 
     type == "WMA"          ? ta.wma(source, length)    :     
     type == "VWMA"         ? ta.vwma(source, length)   :
     type == "HMA"          ? ta.wma(2 * ta.wma(source, length / 2) - ta.wma(source, length), math.floor(math.sqrt(length))) :
     na

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


//Data Entry to Build the MACD Slow Stochastic
fastLength       = input.int(12        , "Fast Length   °°°° ",             inline="MSS #1",  minval=1,                                                   group=DOC_Strategy)
ma1_type         = input.string("EMA"  , "               "    ,             inline="MSS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy)
ma1_source       = input.source(close  , "               "    ,             inline="MSS #1",                                                              group=DOC_Strategy)

slowLength       = input.int(26        , "Slow Length °°°° "  ,             inline="MSS #1",  minval=1,                                                   group=DOC_Strategy)
ma2_type         = input.string("EMA"  , "               "    ,             inline="MSS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy)
ma2_source       = input.source(close  , "               "    ,             inline="MSS #1",                                                              group=DOC_Strategy)

signalLength     = input.int( 9        , "Signal Length °°°"  ,             inline="MSS #1",  minval=1,                                                   group=DOC_Strategy)
ma3_type         = input.string("EMA"  , "               "    ,             inline="MSS #1",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy)

lookback         = input.int(45        , "Look Back      "    ,                               minval=1,                                                   group=DOC_Strategy)

colorLookback    = input.int( 2        , "Color Look Back"    ,                               minval=0,                                                   group=DOC_Strategy)

Color_SSM        = input.color (color.rgb(13, 255, 0),  "Slow Line Color - Stochastic MACD"     ,                                                       group=DOC_Strategy)
ColorSSSM        = input.color (color.rgb(255, 242, 0), "Fast Signal Line Color - Stochastic MACD"   ,                                                  group=DOC_Strategy)

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

//--------------------------------------------------------------------------------------------------------------------------------------------------------------------

//Data Entry to Build the MACD Fast Stochastic
fastLengthXD     = input.int( 6        , "Fast Length   °°°° ",             inline="MFS #2",  minval=1,                                                   group=DOC_Strategy_II)
ma1_typeXD       = input.string("SMA"  , "               "    ,             inline="MFS #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)
ma1_sourceXD     = input.source(close  , "               "    ,             inline="MFS #2",                                                              group=DOC_Strategy_II)

slowLengthXD     = input.int(12        , "Slow Length °°°° "  ,             inline="MFS #2",  minval=1,                                                   group=DOC_Strategy_II)
ma2_typeXD       = input.string("SMA"  , "               "    ,             inline="MFS #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)
ma2_sourceXD     = input.source(close  , "               "    ,             inline="MFS #2",                                                              group=DOC_Strategy_II)

signalLengthXD   = input.int( 3        , "Signal Length °°°"  ,             inline="MFS #2",  minval=1,                                                   group=DOC_Strategy_II)
ma3_typeXD       = input.string("SMA"  , "               "    ,             inline="MFS #2",  options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "HMA"], group=DOC_Strategy_II)

lookbackXD       = input.int(22        , "Look Back      "    ,                               minval=1,                                                   group=DOC_Strategy_II)

colorLookbackXD  = input.int( 2        , "Color Look Back"    ,                               minval=0,                                                   group=DOC_Strategy_II)

Color_FSM        = input.color (color.rgb(0, 13, 255),  "Fast Line Color - Stochastic MACD"     ,                                                       group=DOC_Strategy_II)
ColorSFSM        = input.color (color.rgb(255, 162, 0), "Fast Signal Line Color - Stochastic MACD"   ,                                                  group=DOC_Strategy_II)

//Data Entry to Build the MACD Fast Stochastic Histogram
hist_color0XD    = input.color(color.new(#2f3be9, 37), 'Positive Rising',                                                                               group='Fast Histogram - Stochastic MACD')  // positive rising
hist_color1XD    = input.color(color.new(#000000, 74), 'Positive Falling',                                                                              group='Fast Histogram - Stochastic MACD')  // positive falling
hist_color2XD    = input.color(color.new(#000000, 74), 'Negative Falling',                                                                              group='Fast Histogram - Stochastic MACD')  // negative falling
hist_color3XD    = input.color(color.new(#2f3be9, 37), 'Negative Rising',                                                                               group='Fast Histogram - Stochastic MACD')  // negative rising


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

//Configurable Fast and Slow Parameters to Calculate Slow MACD Stochastic/////////////////////////////
fastMA           = ma(ma1_source, fastLength, ma1_type)
slowMA           = ma(ma2_source, slowLength, ma2_type)

fastAndSlow      = array.from(fastMA, slowMA)
fastAndSlowMax   = array.max(fastAndSlow)
fastAndSlowMin   = array.min(fastAndSlow)

sFastMA          = ta.stoch(fastMA, high, low, lookback)
sSlowMA          = ta.stoch(slowMA, high, low, lookback)

//Parameters to Calculate the MACD Slow Stochastic Line and the MACD Slow Stochastic Signal Line//////
smacd            = sFastMA - sSlowMA
ssignal          = ma(smacd, signalLength, ma3_type)

//Parameters to Calculate the Slow MACD Stochastic Histogram//////////////////////////////////////////
hist             = smacd - ssignal

is_rising        = ta.rising(hist, colorLookback)
is_falling       = ta.falling(hist, colorLookback)
histColor        = colorLookback == 0 ? hist > 0 ? hist_color0 : hist_color3 : hist > 0 ? is_rising ? hist_color0 : hist_color1 : is_falling ? hist_color3 : hist_color2

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

//Configurable Fast and Slow Parameters to Calculate Fast MACD Stochastic/////////////////////////////
fastMAXD         = ma(ma1_sourceXD, fastLengthXD, ma1_typeXD)
slowMAXD         = ma(ma2_sourceXD, slowLengthXD, ma2_typeXD)

fastAndSlowXD    = array.from(fastMAXD, slowMAXD)
fastAndSlowMaxXD = array.max(fastAndSlowXD)
fastAndSlowMinXD = array.min(fastAndSlowXD)

sFastMAXD        = ta.stoch(fastMAXD, high, low, lookbackXD)
sSlowMAXD        = ta.stoch(slowMAXD, high, low, lookbackXD)

//Parameters to Calculate the MACD Fast Stochastic Line and the MACD Fast Stochastic Signal Line//////
smacdXD          = sFastMAXD - sSlowMAXD
ssignalXD        = ma(smacdXD, signalLengthXD, ma3_typeXD)

//Parameters to Calculate the Fast MACD Stochastic Histogram//////////////////////////////////////////
histXD = smacdXD - ssignalXD

is_risingXD      = ta.rising(histXD, colorLookbackXD)
is_fallingXD     = ta.falling(histXD, colorLookbackXD)
histColorXD      = colorLookbackXD == 0 ? histXD > 0 ? hist_color0XD : hist_color3XD : histXD > 0 ? is_risingXD ? hist_color0XD : hist_color1XD : is_fallingXD ? hist_color3XD : hist_color2XD

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

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

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

//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')

//Function to calculate line value////////////////////////////////////////////////////////////////////////////////////////////
Calculate_Line(Period) =>
    float highS = ta.highest(Period)
    float lowS  = ta.lowest(Period)

    int trend = 0
    trend := close > high[1] ? 1 : close < low[1] ? -1 : nz(trend[1])
    trend

//Function to draw line on chart//////////////////////////////////////////////////////////////////////////////////////////////
Draw_Line(Period, Main_Trend) =>
    float highS = ta.highest(Period)
    float lowS  = ta.lowest(Period)

    int trend = 0
    trend := close > high[1] ? 1 : close < low[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)

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

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

//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX//

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

//Histogram Plot, Fast and Slow MACD Stochastic//////////////////////////////////////////////////////

//Fast////////////////////////////////////////////////////
plot(histXD, style=plot.style_columns, color=histColorXD)

//Slow////////////////////////////////////////////////////
plot(hist,   style=plot.style_columns, color=histColor)
//////////////////////////////////////////////////////////

//Plot of the Fast and Slow MACD Stochastic Lines////////////////////////////////////////////////////

//Stochastic MACD Fast////////////////////////////////////
plot(smacdXD,   'Fast Line - Stochastic MACD',        color=Color_FSM, linewidth=2)
plot(ssignalXD, 'Fast Signal Line - Stochastic MACD', color=ColorSFSM, linewidth=2)

//Stochastic MACD Slow////////////////////////////////////
plot(smacd,     'Slow Line - Stochastic MACD',        color=Color_SSM, linewidth=2)
plot(ssignal,   'Slow Signal Line - Stochastic MACD', color=ColorSSSM, linewidth=2)

//Horizontal Line Graph//////////////////////////////////////////////////////////////////////////////
hline( 15,      'Overbought Line',       color=color.rgb(243, 47, 12, 64))
hline(-15,      'Oversold Line',         color=color.rgb(9, 233, 24, 70))


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

//Activate Signals: Long/Buy or Short/Sell///////////////////////////////////////////////////////////////////////////////

Signal_1 = input(false, 'Crossing Slow Lines - Stochastic MACD with Stochastic MACD Signal Line ↑/↓', group='Signal: Long/Buy or Short/Sell')
Signal_2 = input(false, 'Crossing - Fast Line MACD Stochastic with Slow Line MACD Stochastic    ▲/▼', group='Signal: Long/Buy or Short/Sell')

//Cross in Long/Buy or Short/Sell////////////////////////////////////////////////////////////////////////////////////////

LB1 = ta.crossover (smacd, ssignal)    //   Crossing Slow Lines - Stochastic MACD with Stochastic MACD Signal Line
SS1 = ta.crossunder(smacd, ssignal)    //   Crossing Slow Lines - Stochastic MACD with Stochastic MACD Signal Line

LB2 = ta.crossover (smacdXD, smacd)    //   Crossing - Fast Line MACD Stochastic with Slow Line MACD Stochastic
SS2 = ta.crossunder(smacdXD, smacd)    //   Crossing - Fast Line MACD Stochastic with Slow Line MACD Stochastic

//Alert Condition///////////////////////////////////////////////////////////////////////////////////////////////////////

alertcondition((LB1) ,         'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Long/Buy',                 'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Long/Buy')
alertcondition((SS1) ,         'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Short/Sell',               'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Short/Sell')
alertcondition((LB1) or (SS1), 'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Long/Buy or Short/Sell',   'Slow-Sto.MACD/Slow-Sto.MACD.Signal: Long/Buy or Short/Sell')

alertcondition((LB2) ,         'Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy',                        'Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy')
alertcondition((SS2) ,         'Fast-Sto.MACD/Slow-Sto.MACD: Short/Sell',                      'Fast-Sto.MACD/Slow-Sto.MACD: Short/Sell')
alertcondition((LB2) or (SS2), 'Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy or Short/Sell',          'Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy or Short/Sell')

alertcondition((LB1) or (SS1) or (LB2) or (SS2), 'Crosses Totals: Long/Buy or Short/Sell',     'Slow-Sto.MACD/Slow-Sto.MACD.Signal or Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy or Short/Sell')


//Signal Condition//////////////////////////////////////////////////////////////////////////////////////////////////////

plotshape(Signal_1 and (LB1) , title='Slow-Sto.MACD/Slow-Sto.MACD.Signal: Long/Buy',   text='↑', textcolor=color.new(#FFFFFF, 0), style=shape.labelup,   size=size.small, location=location.bottom,  color=color.new(#0ebb9e, 0))
plotshape(Signal_1 and (SS1) , title='Slow-Sto.MACD/Slow-Sto.MACD.Signal: Short/Sell', text='↓', textcolor=color.new(#FFFFFF, 0), style=shape.labeldown, size=size.small, location=location.top,     color=color.new(#cd0a68, 0))

plotshape(Signal_2 and (LB2) , title='Fast-Sto.MACD/Slow-Sto.MACD: Long/Buy',          text='▲', textcolor=color.new(#FFFFFF, 0), style=shape.labelup,   size=size.tiny,  location=location.bottom,  color=color.new(#0ebb9e, 0))
plotshape(Signal_2 and (SS2) , title='Fast-Sto.MACD/Slow-Sto.MACD: Short/Sell',        text='▼', textcolor=color.new(#FFFFFF, 0), style=shape.labeldown, size=size.tiny,  location=location.top,     color=color.new(#cd0a68, 0))

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