KST-Based MACD
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © akikostas

//@version=5
indicator(title="MACD (KST Based)", shorttitle="MACD-KST", timeframe="", timeframe_gaps=true)
// Getting inputs
roclen1 = input.int(10, minval=1, title = "ROC Length #1", group = "KST Settings")
roclen2 = input.int(15, minval=1, title = "ROC Length #2", group = "KST Settings")
roclen3 = input.int(20, minval=1, title = "ROC Length #3", group = "KST Settings")
roclen4 = input.int(30, minval=1, title = "ROC Length #4", group = "KST Settings")
smalen1 = input.int(10, minval=1, title = "SMA Length #1", group = "KST Settings")
smalen2 = input.int(10, minval=1, title = "SMA Length #2", group = "KST Settings")
smalen3 = input.int(10, minval=1, title = "SMA Length #3", group = "KST Settings")
smalen4 = input.int(15, minval=1, title = "SMA Length #4", group = "KST Settings")
source = input(close, title = "Source", group = "KST Settings")
typeMA = input.string(title = "Method", defval = "EMA", options=["SMA", "EMA"], group="KST Settings")
exotic = input.bool(false, title = "Exotic Calculations", group = "KST Settings")
fast_length = input(title="Fast Length", defval=7, group = "MACD Settings")
slow_length = input(title="Slow Length", defval=14, group = "MACD Settings")
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 14, group = "MACD Settings")
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"], group = "MACD Settings")
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"], group = "MACD Settings")
length1 = input.int(5, title="EMA-1 period", minval=1, group = "Ribbon Settings")
length2 = input.int(10, title="EMA-2 period", minval=1, group = "Ribbon Settings")
length3 = input.int(15, title="EMA-3 period", minval=1, group = "Ribbon Settings")
length4 = input.int(20, title="EMA-4 period", minval=1, group = "Ribbon Settings")
length5 = input.int(25, title="EMA-5 period", minval=1, group = "Ribbon Settings")
length6 = input.int(30, title="EMA-6 period", minval=1, group = "Ribbon Settings")
length7 = input.int(35, title="EMA-7 period", minval=1, group = "Ribbon Settings")
length8 = input.int(40, title="EMA-8 period", minval=1, group = "Ribbon Settings")
// Plot colors
col_macd = input(#00bcd4, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#f57c00cc, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#056656, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#d1d4dc, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#d1d4dc, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#e91e63, "Fall", group="Histogram", inline="Below")
// Calculating
logarithmic(src)=> math.sign(src) * math.log(math.abs(src) + 1)

mas(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)

price = source

roc(p, l)=> 100 * (p - p[l]) / p[l]
rocl(p, l)=> 100 * (logarithmic(p) - logarithmic(p[l]))

smaroc(roclen, smalen) => mas(roc(price, roclen), smalen, typeMA)
smarocl(roclen, smalen) => mas(rocl(price, roclen), smalen, typeMA)

kst = smaroc(roclen1, smalen1) + 2 * smaroc(roclen2, smalen2) + 3 * smaroc(roclen3, smalen3) + 4 * smaroc(roclen4, smalen4)
if (exotic)
    kst := smarocl(roclen1, smalen1) + 2 * smarocl(roclen2, smalen2) + 3 * smarocl(roclen3, smalen3) + 4 * smarocl(roclen4, smalen4)

fast_ma = sma_source == "SMA" ? ta.sma(kst, fast_length) : ta.ema(kst, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(kst, slow_length) : ta.ema(kst, slow_length)
macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal

hline(0, "Zero Line", color=color.new(#787B86, 75), linestyle=hline.style_solid)
p8 = plot(ta.ema(macd, length8), title="EMA-8", color=#3c066340, linewidth=2)
p7 = plot(ta.ema(macd, length7), title="EMA-7", color=#4a0a7740, linewidth=2)
p6 = plot(ta.ema(macd, length6), title="EMA-6", color=#5a108f40, linewidth=2)
p5 = plot(ta.ema(macd, length5), title="EMA-5", color=#6818a540, linewidth=2)
p4 = plot(ta.ema(macd, length4), title="EMA-4", color=#8b2fc940, linewidth=2)
p3 = plot(ta.ema(macd, length3), title="EMA-3", color=#ab51e340, linewidth=2)
p2 = plot(ta.ema(macd, length2), title="EMA-2", color=#bd68ee40, linewidth=2)
p1 = plot(ta.ema(macd, length1), title="EMA-1", color=#d283ff40, linewidth=2)
plot(hist, title="Histogram", style=plot.style_area, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
plot(signal, title="Signal", color=col_signal, linewidth=3, display=display.none)
p0 = plot(macd, title="MACD", color=col_macd, linewidth=3)

fill(p8, p7, title="Area 8", color=#3c066320)
fill(p7, p6, title="Area 7", color=#4a0a7720)
fill(p6, p5, title="Area 6", color=#5a108f20)
fill(p5, p4, title="Area 5", color=#6818a520)
fill(p4, p3, title="Area 4", color=#8b2fc920)
fill(p3, p2, title="Area 3", color=#ab51e320)
fill(p2, p1, title="Area 2", color=#bd68ee20)
fill(p1, p0, title="Area 1", color=#d283ff20)