#----------------------------------------------------------------#
#============== Syracusepro Harmonics and Fractals ==============#
#----------------------------------------------------------------#
#===== This program was made with the intention of educational #
#===== purposes. It contains fractal and harmonic patterns. #
#===== On the zigzags, the Fibonacci sequence numbers are also #
#===== included, and shows what makes each patterns valid with #
#===== their correct names. #
#=========== I hope people interested can make a great program #
#=========== out of this material. Thanks. ======================#
#----------------------------------------------------------------#
#---------------- Total of 12 Harmonic Patterns -----------------#
#================= Http://www.thinkscriptcode.com ===============#
#---------------------------------------#
#===== Irregular fractals function =====#
#---------------------------------------#
###### mcdon030 addded tolerance per Larry Pesavento
##### and roughly 17 more patterns.
script isRegularFractal {
input mode=0;
plot ret = if mode == 1 then high[4] < high[3] and high[3] < high[2] and high[2] > high[1] and high[1] > high[0] else if mode == -1 then low[4] > low[3] and low[3] > low[2] and low[2] < low[1] and low[1] < low[0] else 0;
}
#---------------------------------------#
#===== BWilliams fractals function =====#
#---------------------------------------#
script isBWFractal {
input mode=0;
plot ret = if mode == 1 then high[4] < high[2] and high[3] <= high[2] and high[2] >= high[1] and high[2] > high[0] else if mode == -1 then low[4] > low[2] and low[3] >= low[2] and low[2] <= low[1] and low[2] < low[0] else 0;
}
input showPatterns=Yes;
input showBarColors=No;
input filterBW=No;
input ShowHHLL=No;
input ShowTimeFractals1=No;
input showArrows = No;
input tolerance =.06;
input timeframe1={MIN, TWO_MIN, THREE_MIN, FOUR_MIN, FIVE_MIN, TEN_MIN, FIFTEEN_MIN, TWENTY_MIN, THIRTY_MIN, HOUR, TWO_HOURS, FOUR_HOURS,default DAY, TWO_DAYS, THREE_DAYS, FOUR_DAYS, WEEK, MONTH, OPT_EXP};
input showchannel1=No;
input showchannel2=No;
input showchannel3=No;
input showZigZag=Yes;
def bn=barNumber();
def h2=high[2];
def l2=low[2];
def filteredtopf = if filterBW then isRegularFractal(1) else isBWFractal(1);
def filteredbotf = if filterBW then isRegularFractal(-1) else isBWFractal(-1);
plot TopFractals=filteredtopf[-2];
plot BottomFractals=filteredbotf[-2];
#def TopFractals=filteredtopf[-2];
#def BottomFractals=filteredbotf[-2];
TopFractals.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
TopFractals.SetDefaultColor(color.RED);
BottomFractals.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
BottomFractals.SetDefaultColor(color.GREEN );
def b1_0=if bn==0 then -1 else if filteredtopf == 0 then 0 else if b1_0[1]>-1 then b1_0[1]+1 else -1;
def b1_1=b1_0+Getvalue(b1_0,b1_0+1,0)+1;
def b1_2=b1_1+Getvalue(b1_0,b1_1+1,0)+1;
def b2_0=if bn==0 then -1 else if filteredbotf == 0 then 0 else if b2_0[1]>-1 then b2_0[1]+1 else -1;
def b2_1=b2_0+Getvalue(b2_0,b2_0+1,0)+1;
def b2_2=b2_1+Getvalue(b2_0,b1_1+1,0)+1;
def higherhigh = if filteredtopf == 0 or b1_2==b1_1 then 0 else GetValue(high[2],b1_1,0) < GetValue(high[2],b1_0,0) and GetValue(high[2],b1_2,0) < GetValue(high[2],b1_0,0);
def lowerhigh = if filteredtopf == 0 or b1_2==b1_1 then 0 else GetValue(high[2],b1_1,0) > GetValue(high[2],b1_0,0) and GetValue(high[2],b1_2,0) > GetValue(high[2],b1_0,0);
def higherlow = if filteredbotf == 0 or b2_2==b2_1 then 0 else GetValue(low[2],b2_1,0) < GetValue(low[2],b2_0,0) and GetValue(low[2],b2_2,0) < GetValue(low[2],b2_0,0);
def lowerlow = if filteredbotf == 0 or b2_2==b2_1 then 0 else GetValue(low[2],b2_1,0) > GetValue(low[2],b2_0,0) and GetValue(low[2],b2_2,0) > GetValue(low[2],b2_0,0);
AddChartBubble(ShowHHLL and higherhigh,high[-2],"[HH]",color.CYAN,yes);
AddChartBubble(ShowHHLL and lowerhigh,high[-2],"[LH]",color.CYAN,yes);
AddChartBubble(ShowHHLL and higherlow,low[-2],"[HL]",color.GREEN);
AddChartBubble(ShowHHLL and lowerlow,low[-2],"[LL]",color.GREEN);
def hh=if bn==0 then -1 else if higherhigh == 1 then 0 else if hh[1]>-1 then hh[1]+1 else -1;
def ll=if bn==0 then -1 else if lowerlow == 1 then 0 else if ll[1]>-1 then ll[1]+1 else -1;
def higherhhigh = if higherhigh == 0 or hh==-1 then 0 else GetValue(high[2],hh,0) >= high(period=timeframe1) ;
def lowerllow = if lowerlow == 0 or ll==-1 then 0 else GetValue(low[2],ll,0) <= low(period=timeframe1);
AddChartBubble(ShowTimeFractals1 and higherhhigh , high[-2],"[TL]",color.GREEN,yes);
AddChartBubble(ShowTimeFractals1 and lowerllow, low[-2],"[TL]",color.GREEN);
plot TopChannel1=if showchannel1 and filteredtopf then h2[-2] else Double.NaN;
plot BottomChannel1=if showchannel1 and filteredbotf then l2[-2] else Double.NaN;
TopChannel1.SetDefaultColor(color.green);
BottomChannel1.SetDefaultColor(color.red);
TopChannel1.enableApproximation();
BottomChannel1.enableApproximation();
plot TopChannel2=if showchannel2 and higherhigh then h2[-2] else Double.NaN;
plot BottomChannel2=if showchannel2 and lowerlow then l2[-2] else Double.NaN;
TopChannel2.SetDefaultColor(color.BLUE);
BottomChannel2.SetDefaultColor(color.BLUE);
TopChannel2.enableApproximation();
BottomChannel2.enableApproximation();
plot TopChannel3=if showchannel3 and higherhhigh then h2[-2] else Double.NaN;
plot BottomChannel3=if showchannel3 and lowerllow then l2[-2] else Double.NaN;
TopChannel3.SetDefaultColor(color.VIOLET);
BottomChannel3.SetDefaultColor(color.VIOLET);
TopChannel3.enableApproximation();
BottomChannel3.enableApproximation();
addcloud(TopChannel3, BottomChannel3, color.green, color.red);
def istop = if ShowTimeFractals1 then (if higherhhigh then 1 else 0) else (if filteredtopf then 1 else 0);
def isbot = if ShowTimeFractals1 then (if lowerllow then 1 else 0) else (if filteredbotf then 1 else 0);
def topcount0 = if istop then bn else topcount0[1];
def botcount0 = if isbot then bn else botcount0[1];
def topcount=bn-topcount0;
def botcount=bn-botcount0;
def zigzag = if istop and topcount[1] > botcount[1] then h2 else if isbot and topcount[1] < botcount[1] then l2 else Double.NaN;
#def zz=if showZigZag then zigzag else Double.NaN;
#plot zz=if showZigZag then zigzag else Double.NaN;
plot zz=if showZigZag then zigzag[-2] else Double.NaN;
zz.SetDefaultColor(color.WHITE);
zz.enableApproximation();
AssignPriceColor(if showBarColors and isNaN(zigzag) == 0 then(if h2==zigzag then color.blue else if l2==zigzag then color.CYAN else color.CURRENT) else color.CURRENT);
def z_0=if bn==0 then -1 else if isNaN(zigzag) == 0 then 0 else if z_0[1]>-1 then z_0[1]+1 else -1;
def z_1=z_0+Getvalue(z_0,z_0+1,0)+1;
def z_2=z_1+Getvalue(z_0,z_1+1,0)+1;
def z_3=z_2+Getvalue(z_0,z_2+1,0)+1;
def z_4=z_3+Getvalue(z_0,z_3+1,0)+1;
def z_5=z_4+Getvalue(z_0,z_4+1,0)+1;
#plot rz0=z_0;
#plot rz1=z_1;
#plot rz2=z_2;
#rz0.SetDefaultColor(color.RED);
#plot rgv=Getvalue(z_0,(z_1+1),0);
#rgv.SetDefaultColor(color.GREEN);
#======= Preparing the XABCD Patterns =======#
def x1=GetValue(zigzag,z_5,0);
def x=GetValue(zigzag,z_4,0);
def a=GetValue(zigzag,z_3,0);
def b=GetValue(zigzag,z_2,0);
def c=GetValue(zigzag,z_1,0);
def d=GetValue(zigzag,z_0,0);
#plot rx=x;
#plot ra=a;
#plot rb=b;
#plot rc=c;
#plot rd=d;
#====== Combination to create the patterns ======#
def xab = (AbsValue(b-a)/AbsValue(x-a));
def xad = (AbsValue(a-d)/AbsValue(x-a));
def abc = (AbsValue(b-c)/AbsValue(a-b));
def bcd = (AbsValue(c-d)/AbsValue(b-c));
def xabc = (AbsValue(b-c)/AbsValue(x-a));
def x1xcd = (AbsValue(d-c)/AbsValue(x1-x));## wave5
def x1xab = (AbsValue(b-a)/AbsValue(x1-x));## wave3
def x1xxa = (AbsValue(x-a)/AbsValue(x1-x));## wave2
#===== Checking to see if there is a pattern functions =====#
#---------------------------------------#
#= ==== Butterfly Pattern Function =====#
#---------------------------------------#
script isButterfly {
input _mode = 0;
input xab = 0;
input xad = 0;
input abc = 0;
input bcd = 0;
input d = 0;
input c = 0;
def _xab = xab <= 0.786;
def _abc = abc >= 0.382 and abc <= 0.886;
def _bcd = bcd >= 1.618 and bcd <= 2.618;
def _xad = xad >= 1.27 and xad <= 1.618;
plot ret = _xab and _abc and _bcd and _xad and (if _mode == 1 then d < c else d > c);
}
def u_bfly1 = showPatterns and isButterfly(-1, xab, xad, abc, bcd, d, c) and isButterfly(-1, xab, xad, abc, bcd, d, c)[1] == 0;
AddChartBubble(u_bfly1[-2] , high, "Bear Butterfly", Color.RED, yes);
#============ End Syracusepro Harmonics and Fractals ============#