```
def H = high ;
def L = low ;
def O = open ;
def C = close ;
# Calculate the length of the candle's wicks
def UpperWick = H - Max(O, C);
def LowerWick = Min(O, C) - L;
# Calculate the length of the candle's body
def CandleBody = BodyHeight();
def range = H - L;
def cond = range == Highest(range, 5);
# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = H < H[1] and (LowerWick / CandleBody >= 1.25) and (UpperWick / CandleBody <= 1);
def Shooter = L > L[1] and (UpperWick / CandleBody >= 1.25) and (LowerWick / CandleBody <= 1);
def inside = H < H[1] and L > L[1];
def twodn = H < H[1];
def twoup = L > L[1];
def sequenceCount = 2;
def bn = BarNumber();
def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;
def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;
def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;
def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
#def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];
def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
#def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];
def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def Prev1UpH = GetValue(H, bn - Prev1UpFractalBN); #if bn == Prev1UpFractalBN then H else Prev1UpH[1];
#def Prev2UpH = GetValue(H,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then L else Prev2UpH[1];
def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];
def Prev1DnL = GetValue(L, bn - Prev1DnFractalBN); #if bn == Prev1DnFractalBN then H else Prev1DnH[1];
#def Prev2DnL = GetValue(L,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then L else Prev2DnH[1];
plot upfractalwedge = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
plot DownFractalwedge = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;
upfractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
upfractalwedge.SetDefaultColor(GetColor(3));
upfractalwedge.SetLineWeight(5);
DownFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
DownFractalwedge.SetDefaultColor(GetColor(4));
DownFractalwedge.SetLineWeight(5);
#def Prev2NearTop = C > Prev2UpH and C > Prev1UpH and C > UpH;
#def Prev2NearBottom = C < Prev2DnL and C < Prev1DnL and C < DnL;
#def Prev1NearTop = C > Prev1UpH and C > UpH;
#def Prev1NearBottom = C < Prev1DnL and C < DnL;
def NearTop = C > UpH;
def NearBottom = C < DnL;
def ReversaloffTop = NearTop and (Shooter or inside);
def ReversaloffBottom = NearBottom and (Hammer or inside);
def oldestbn = if UpFractalBN > DnFractalBN then UpFractalBN else DnFractalBN;
def BFdiff = if bn < HighestAll(oldestbn) then Double.NaN else (UpH - DnL) / 2 + DnL;
plot SSS = BFdiff;
SSS.SetStyle(Curve.MEDIUM_DASH);
SSS.SetDefaultColor(Color.GRAY);
SSS.SetLineWeight(1);
input showcloud = yes;
input showbubbles = yes;
AddCloud(if showcloud and bn >= HighestAll( oldestbn) then UpH else Double.NaN, DnL, CreateColor(43, 45, 48 ), CreateColor(50, 90, 59 ));
#AddCloud(if showcloud then UpH else Double.NaN, UpH, createcolor(110, 69, 60 ), createcolor(110, 69, 60 ));
AddChartBubble(if showbubbles and bn == HighestAll(bn - 5) then 1 else 0 , BFdiff, "M SSS", Color.YELLOW);
#Alert(triggerlong or triggershort, getunderlyingsymbol() + " IS GOING OUTSIDE!!", Alert.TICK, Sound.Ding);
#plot triggerlong= L< L[1] and H < H[1] and C >= fiftypct;
#plot triggershort= H> H[1] and L > L[1] and C <= fiftypct;
def FinalBar = fold b=0 to 1000 while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);
def HRise = Prev1UpH - UpH;
def HRun = UpFractalBN - Prev1UpFractalBN;
def HIndeterminateSlope = if HRun == 0 then yes else no;
def HSlope = if !HIndeterminateSlope then HRise / HRun else double.nan;
def FinalHSlope = If FinalBar!=0 then fold e=0 to 1000 while !IsNaN(GetValue(close,-e)) do GetValue(HSlope,-e) else FinalHSlope[1];
def HLine = if bn == 1 then 0 else if bn == Prev1UpFractalBN+(bn-Prev1UpFractalBN) and UpFractalBN!=UpFractalBN[1] then UpH else HLine[1] - FinalHSlope;
Plot HDrawline = if bn >= (FinalBar-bn)+prev1UpFractalBN[-1] then Hline else Double.NaN;
HDrawLine.setpaintingstrategy(paintingStrategy.line);
HDrawLine.SetLineWeight(5);
def Rise = Prev1DnL - DnL;
def Run = DnFractalBN - Prev1DnFractalBN;
def IndeterminateSlope = if Run == 0 then yes else no;
def LSlope = if !IndeterminateSlope then Rise / Run else Double.NaN;
def FinalLSlope = If FinalBar!=0 then fold f=0 to 1000 while !IsNaN(GetValue(close,-f)) do GetValue(LSlope,-f) else FinalLSlope[1];
def Line = if bn == 1 then 0 else if bn == Prev1DnFractalBN+(bn-Prev1DnFractalBN) and DnFractalBN!=DnFractalBN[1] then DnL else Line[1] - FinalLSlope;
Plot LDrawLine = if bn >= (FinalBar-bn)+prev1DnFractalBN[-1] then Line else Double.NaN;
LDrawLine.setpaintingstrategy(paintingStrategy.line);
LDrawLine.SetLineWeight(5);
```