Fractals + Alligator + Divergent Bars + Squat Bars
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sakis-me

//@version=5
indicator("Fractals + Alligator + Divergent Bars + Squat Bars", shorttitle="FADS by sakis-me", overlay=true)
showFractals = input.bool(defval=true, title="Show Fractals", group="Fractals")
n = input.int(title="Fractal Periods", defval=2, minval=2, group="Fractals")


// UpFractal
bool upflagDownFrontier = true
bool upflagUpFrontier0 = true
bool upflagUpFrontier1 = true
bool upflagUpFrontier2 = true
bool upflagUpFrontier3 = true
bool upflagUpFrontier4 = true

for i = 1 to n
    upflagDownFrontier := upflagDownFrontier and (high[n-i] < high[n])
    upflagUpFrontier0 := upflagUpFrontier0 and (high[n+i] < high[n])
    upflagUpFrontier1 := upflagUpFrontier1 and (high[n+1] <= high[n] and high[n+i + 1] < high[n])
    upflagUpFrontier2 := upflagUpFrontier2 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+i + 2] < high[n])
    upflagUpFrontier3 := upflagUpFrontier3 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+i + 3] < high[n])
    upflagUpFrontier4 := upflagUpFrontier4 and (high[n+1] <= high[n] and high[n+2] <= high[n] and high[n+3] <= high[n] and high[n+4] <= high[n] and high[n+i + 4] < high[n])
flagUpFrontier = upflagUpFrontier0 or upflagUpFrontier1 or upflagUpFrontier2 or upflagUpFrontier3 or upflagUpFrontier4

upFractal = (upflagDownFrontier and flagUpFrontier)


// downFractal
bool downflagDownFrontier = true
bool downflagUpFrontier0 = true
bool downflagUpFrontier1 = true
bool downflagUpFrontier2 = true
bool downflagUpFrontier3 = true
bool downflagUpFrontier4 = true

for i = 1 to n
    downflagDownFrontier := downflagDownFrontier and (low[n-i] > low[n])
    downflagUpFrontier0 := downflagUpFrontier0 and (low[n+i] > low[n])
    downflagUpFrontier1 := downflagUpFrontier1 and (low[n+1] >= low[n] and low[n+i + 1] > low[n])
    downflagUpFrontier2 := downflagUpFrontier2 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+i + 2] > low[n])
    downflagUpFrontier3 := downflagUpFrontier3 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+i + 3] > low[n])
    downflagUpFrontier4 := downflagUpFrontier4 and (low[n+1] >= low[n] and low[n+2] >= low[n] and low[n+3] >= low[n] and low[n+4] >= low[n] and low[n+i + 4] > low[n])
flagDownFrontier = downflagUpFrontier0 or downflagUpFrontier1 or downflagUpFrontier2 or downflagUpFrontier3 or downflagUpFrontier4

downFractal = (downflagDownFrontier and flagDownFrontier)

plotshape(showFractals and downFractal, style=shape.triangledown, location=location.belowbar, offset=-n, color=#F44336, size = size.small)
plotshape(showFractals and upFractal, style=shape.triangleup,   location=location.abovebar, offset=-n, color=#009688, size = size.small)


showAlligator = input.bool(defval=true, title="Show Alligator", group="Alligator")
smma(src, length) =>
	smma =  0.0
	smma := na(smma[1]) ? ta.sma(src, length) : (smma[1] * (length - 1) + src) / length
	smma
jawLength = input.int(13, minval=1, title="Jaw Length", group="Alligator")
teethLength = input.int(8, minval=1, title="Teeth Length", group="Alligator")
lipsLength = input.int(5, minval=1, title="Lips Length", group="Alligator")
jawOffset = input(8, title="Jaw Offset", group="Alligator")
teethOffset = input(5, title="Teeth Offset", group="Alligator")
lipsOffset = input(3, title="Lips Offset", group="Alligator")
jaw = smma(hl2, jawLength)
teeth = smma(hl2, teethLength)
lips = smma(hl2, lipsLength)
plot(showAlligator ? jaw : na, "Jaw", offset = jawOffset, color=#2962FF)
plot(showAlligator ? teeth : na, "Teeth", offset = teethOffset, color=#E91E63)
plot(showAlligator ? lips : na, "Lips", offset = lipsOffset, color=#66BB6A)


showDivergentBar = input.bool(defval=true, title="Show Divergent Bars", group="Divergent Bars")
ATR_function = ta.rma(ta.tr(true), 14)

Jaw = 0.0
Jaw := na(Jaw[1]) ? ta.sma(close, 13) : (Jaw[1] * (13 - 1) + close) / 13
Jaw_ = Jaw[8]

bear = ((close > hl2 and open > hl2) and (hl2 < (Jaw_ - ATR_function))) and (high - low > ATR_function) ? true : false
bull = ((close < hl2 and open < hl2) and (hl2 > (Jaw_ + ATR_function))) and (high - low > ATR_function) ? true : false

plotshape(showDivergentBar and bull, style=shape.circle, location=location.abovebar, color=color.fuchsia, size = size.tiny, title="Bull")
plotshape(showDivergentBar and bear, style=shape.circle,   location=location.belowbar, color=color.lime, size = size.tiny, title="Bear")

showSquatBars = input.bool(defval=true, title="Show Squat Bars", group="Squat Bars")
r_hl=ta.roc((high-low)/volume,1)
r_v=ta.roc(volume,1)
squat_f=(r_hl < 0) and (r_v > 0)
barcolor(showSquatBars and squat_f ? color.blue : na, title="Squat Bars") 




