SUPER Alligator[Gabbo]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Gabbo1064

//@version=5
indicator("SUPER Alligator[Gabbo]", "Alligator", true, precision=2)

// ══════════════════════════   Input {
jawLength           =   input.int(      13,         "Jaw Length",       1,      100,        1,      inline="1",     group="Alligator")
jawOffset           =   input.int(      8,          "Jaw Offset",       1,      100,        1,      inline="1",     group="Alligator")
teethLength         =   input.int(      8,          "Teeth Length",     1,      100,        1,      inline="2",     group="Alligator")
teethOffset         =   input.int(      5,          "Teeth Offset",     1,      100,        1,      inline="2",     group="Alligator")
lipsLength          =   input.int(      5,          "Lips Length",      1,      100,        1,      inline="3",     group="Alligator")
lipsOffset          =   input.int(      3,          "Lips Offset",      1,      100,        1,      inline="3",     group="Alligator")
sourceAllig         =   input.source(   hl2,        "Source",                                       inline="4",     group="Alligator")
maSlowType          =   input.string(   "EMA",      "Type Line", ["SMA", "EMA", "WMA", "RMA", "HMA", "JMA", "DEMA", "TEMA", "LSMA", "VWMA", "SMMA", "KAMA", "ALMA", "FRAMA", "VIDYA"], inline="4", group="Alligator")
phaseSlow           =   input.int(      50,         "Phase",            1,      1000,       1,      inline="5",     group="Alligator",      tooltip="FOR JMA")
powerSlow           =   input.int(      2,          "Power",            1,      1000,       1,      inline="5",     group="Alligator",      tooltip="FOR JMA")
offsetSlow          =   input.float(    0.85,       "Offset",           0,      1000,       1,      inline="6",     group="Alligator",      tooltip="FOR ALMA")
sigmaSlow           =   input.float(    6,          "Sigma",            0,      1000,       1,      inline="6",     group="Alligator",      tooltip="FOR ALMA")
FCSlow              =   input.int(      1,          "Lower shift (FC)", 1,      1000,       1,      inline="7",     group="Alligator",      tooltip="FOR FRAMA") 
SCSlow              =   input.int(      198,        "Upper shift (SC)", 1,      1000,       1,      inline="7",     group="Alligator",      tooltip="FOR FRAMA")
fixCMOSlow          =   input.bool(     true,       "Fixed CMO Length (9)?",                        inline="8",     group="Alligator",      tooltip="FOR VIDYA")
selectSlow          =   input.bool(     true,       "Calculation Method: CMO/StDev?",               inline="8",     group="Alligator",      tooltip="FOR VIDYA")

timeCandles         =   input(          "",         "Timeframe",                                    inline="9",     group="Alligator")
alligStrong         =   input.bool(     false,      "Use Different Source???",                      inline="9",     group="Alligator")

Alligsource1        =   input.bool(     true,       "Source 1",                                     inline="10",     group="Alligator")
alligsource1        =   input.source(   close,      "",                                             inline="10",     group="Alligator")
Alligsource2        =   input.bool(     true,       "Source 2",                                     inline="10",     group="Alligator")
alligsource2        =   input.source(   open,       "",                                             inline="10",     group="Alligator")
Alligsource3        =   input.bool(     true,       "Source 3",                                     inline="11",     group="Alligator")
alligsource3        =   input.source(   hl2,        "",                                             inline="11",     group="Alligator")
Alligsource4        =   input.bool(     true,       "Source 4",                                     inline="11",     group="Alligator")
alligsource4        =   input.source(   ohlc4,      "",                                             inline="11",     group="Alligator")
Alligsource5        =   input.bool(     false,      "Source 5",                                     inline="12",     group="Alligator")
alligsource5        =   input.source(   hlcc4,      "",                                             inline="12",     group="Alligator")

plotLine            =   input.bool(     true,       "Jaw/ Teeth/ Lips ???",                         inline="1",      group="Plot")
fillLine            =   input.bool(     true,       "Fill Line???",                                 inline="1",      group="Plot")
fillBack            =   input.bool(     false,      "Fill Background???",                           inline="1",      group="Plot")
fillLineLong        =   input.color(    color.new(#58E600, 90), "Fill Line Long",                 inline="2",      group="Plot")
fillLineShort       =   input.color(    color.new(#FA2878, 90), "Fill Line Short",                inline="2",      group="Plot")
fillLineNeut        =   input.color(    color.new(color.yellow, 90), "Fill Line Neutral",         inline="2",      group="Plot")
fillBackLong        =   input.color(    color.new(#58E600, 96), "BG Long",                        inline="3",      group="Plot")
fillBackShort       =   input.color(    color.new(#FA2878, 96), "BG Short",                       inline="3",      group="Plot")
fillBackNeut        =   input.color(    color.new(color.yellow, 96), "BG Neutral",                inline="3",      group="Plot")
// }

// ══════════════════════════   Calculations {
sourceNum1          =   Alligsource1? 1: 0
sourceNum2          =   Alligsource2? 1: 0
sourceNum3          =   Alligsource3? 1: 0
sourceNum4          =   Alligsource4? 1: 0
sourceNum5          =   Alligsource5? 1: 0
sourceNum           =   sourceNum1 + sourceNum2 + sourceNum3 + sourceNum4 + sourceNum5
sourceSomm          =   (Alligsource1? alligsource1: 0) + (Alligsource2? alligsource2: 0) + (Alligsource3? alligsource3: 0) + (Alligsource4? alligsource4: 0) + (Alligsource5? alligsource5: 0)
source              =   alligStrong? sourceSomm / sourceNum: sourceAllig

hma(source, lenght) => ta.wma(2 * ta.wma(source, lenght / 2) -ta.wma(source, lenght), math.round(math.sqrt(lenght)))
jma(phase, lenght, power, source) =>
    phaseRatio      =   phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5
    beta            =   0.45 * (lenght - 1) / (0.45 * (lenght - 1) + 2)
    alpha           =   math.pow(beta, power)
    jma             =   0.0
    e0              =   0.0
    e0              :=  (1 - alpha) * source + alpha * nz(e0[1])
    e1              =   0.0
    e1              :=  (source - e0) * (1 - beta) + beta * nz(e1[1])
    e2              =   0.0
    e2              :=  (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
    jma             :=  e2 + nz(jma[1])
dema(source, lenght) => 2 * (ta.ema(source, lenght)) - (ta.ema(ta.ema(source, lenght), lenght))
tema(source, lenght) => 3 * ((ta.ema(source, lenght)) - (ta.ema(ta.ema(source, lenght), lenght))) + (ta.ema(ta.ema(ta.ema(source, lenght), lenght), lenght))
smma(source, lenght) =>
    smma1           =   0.0000
    smma11          =   ta.sma(source, lenght)
    smma1           :=  na(smma1[1]) ? smma11 : ((smma1[1] * (lenght - 1) + source) / lenght)
kama(source, lenght) =>
    xvnoise         =   math.abs(source - source[1])
    nAMA            =   0.0
    nfastend        =   0.666
    nslowend        =   0.0645
    nsignal         =   math.abs(source - source[lenght])
    nnoise          =   math.sum(xvnoise, lenght)
    nefratio        =   nnoise != 0? nsignal / nnoise: 0
    nsmooth         =   math.pow(nefratio * (nfastend - nslowend) + nslowend, 2) 
    nAMA            :=  nz(nAMA[1]) + nsmooth * (source - nz(nAMA[1]))
frama(source, length, FC, SC) =>
    HL              =   (ta.highest(high, length) - ta.lowest(low, length)) / length
    HL1             =   (ta.highest(high, length/2) - ta.lowest(low, length/2)) / (length/2)
    HL2             =   (ta.highest(high, length/2)[length/2] - ta.lowest(low, length/2)[length/2]) / (length/2)
    D               =   (math.log(HL1+HL2) - math.log(HL)) / math.log(2)
    dim             =   HL1>0 and HL2>0 and HL>0? D: nz(D[1])
    w               =   math.log(2/(SC+1))
    alpha           =   math.exp(w*(dim-1))
    alpha1          =   alpha>1 ? 1 : (alpha<0.01 ? 0.01 : alpha)
    oldN            =   (2-alpha1) / alpha1
    newN            =   (((SC-FC) * (oldN-1)) / (SC-1)) + FC
    newalpha        =   2/(newN+1)
    newalpha1       =   newalpha<2/(SC+1) ? 2/(SC+1) : (newalpha>1 ? 1 : newalpha)
    frama           =   0.0
    frama           :=  (1-newalpha1) * nz(frama[1]) + newalpha1*source
vidya(source, lenght, bool1, bool2) =>
    alpha           =   2/(lenght+1)
    momm            =   ta.change(source)
    m1              =   momm >= 0.0 ? momm : 0.0
    m2              =   momm >= 0.0 ? 0.0 : -momm
    sm1             =   bool1 ? math.sum(m1, 9) : math.sum(m1, lenght)
    sm2             =   bool1 ? math.sum(m2, 9) : math.sum(m2, lenght)
    chandeMO        =   nz(100 * (sm1 - sm2) / (sm1 + sm2))
    k               =   bool2 ? math.abs(chandeMO)/100 : ta.stdev(source,lenght)
    VIDYA           =   0.0
    VIDYA           :=  nz(alpha*k*source)+(1-alpha*k)*nz(VIDYA[1])


jawInput            =   maSlowType == "SMA"? ta.sma(source, jawLength): maSlowType == "EMA"? ta.ema(source, jawLength): maSlowType == "WMA"? ta.wma(source, jawLength): maSlowType == "DEMA"? 
   dema(source, jawLength): maSlowType == "TEMA"? tema(source, jawLength): maSlowType == "VWMA"? ta.vwma(source, jawLength): maSlowType == "RMA"? ta.rma(source, jawLength): maSlowType == "LSMA"? 
   ta.linreg(source, jawLength, 0): maSlowType == "SMMA"? smma(source, jawLength): maSlowType == "HMA"? hma(source, jawLength): maSlowType == "ALMA"? ta.alma(source, jawLength, offsetSlow, sigmaSlow): 
   maSlowType == "JMA"? jma(phaseSlow, jawLength, powerSlow, source): maSlowType == "KAMA"? kama(source, jawLength): maSlowType == "VIDYA"? vidya(source, jawLength, fixCMOSlow, selectSlow): 
   frama(source, jawLength, FCSlow, SCSlow)

jaw                 =   request.security(syminfo.tickerid, timeCandles, jawInput)

teethInput          =   maSlowType == "SMA"? ta.sma(source, teethLength): maSlowType == "EMA"? ta.ema(source, teethLength): maSlowType == "WMA"? ta.wma(source, teethLength): maSlowType == "DEMA"? 
   dema(source, teethLength): maSlowType == "TEMA"? tema(source, teethLength): maSlowType == "VWMA"? ta.vwma(source, teethLength): maSlowType == "RMA"? ta.rma(source, teethLength): maSlowType == "LSMA"? 
   ta.linreg(source, teethLength, 0): maSlowType == "SMMA"? smma(source, teethLength): maSlowType == "HMA"? hma(source, teethLength): maSlowType == "ALMA"? ta.alma(source, teethLength, offsetSlow, sigmaSlow): 
   maSlowType == "JMA"? jma(phaseSlow, teethLength, powerSlow, source): maSlowType == "KAMA"? kama(source, teethLength): maSlowType == "VIDYA"? vidya(source, teethLength, fixCMOSlow, selectSlow): 
   frama(source, teethLength, FCSlow, SCSlow)

teeth               =   request.security(syminfo.tickerid, timeCandles, teethInput)

lipsInput           =   maSlowType == "SMA"? ta.sma(source, lipsLength): maSlowType == "EMA"? ta.ema(source, lipsLength): maSlowType == "WMA"? ta.wma(source, lipsLength): maSlowType == "DEMA"? 
   dema(source, lipsLength): maSlowType == "TEMA"? tema(source, lipsLength): maSlowType == "VWMA"? ta.vwma(source, lipsLength): maSlowType == "RMA"? ta.rma(source, lipsLength): maSlowType == "LSMA"? 
   ta.linreg(source, lipsLength, 0): maSlowType == "SMMA"? smma(source, lipsLength): maSlowType == "HMA"? hma(source, lipsLength): maSlowType == "ALMA"? ta.alma(source, lipsLength, offsetSlow, sigmaSlow): 
   maSlowType == "JMA"? jma(phaseSlow, lipsLength, powerSlow, source): maSlowType == "KAMA"? kama(source, lipsLength): maSlowType == "VIDYA"? vidya(source, lipsLength, fixCMOSlow, selectSlow): 
   frama(source, lipsLength, FCSlow, SCSlow)

lips                =   request.security(syminfo.tickerid, timeCandles, lipsInput)
// }

// ══════════════════════════   Plot {
alligatorLong       =   jaw[jawOffset] <= teeth[teethOffset] and jaw[jawOffset] <= lips[lipsOffset] and teeth[teethOffset] <= lips[lipsOffset]
alligatorShort      =   jaw[jawOffset] > teeth[teethOffset] and jaw[jawOffset] > lips[lipsOffset] and teeth[teethOffset] > lips[lipsOffset]

jawPlot             =   plot(jaw[jawOffset], "Jaw", #2962FF, display=plotLine? display.all: display.none)
teethPlot           =   plot(teeth[teethOffset], "Teeth", #E91E63, display=plotLine? display.all: display.none)
lipsPlot            =   plot(lips[lipsOffset], "Lips", #66BB6A, display=plotLine? display.all: display.none)

fill(jawPlot, lipsPlot, alligatorLong? fillLineLong: alligatorShort? fillLineShort: fillLineNeut, title="Fill Line", display=fillLine? display.all: display.none)
bgcolor(alligatorLong? fillBackLong: alligatorShort? fillBackShort: fillBackNeut, display=fillBack? display.all: display.none)
// }