/* HARMONIC PATTERN DETECTION
Automatic Detection of Harmonic Patterns - Gartley, Bat, Butterfly and Crab.
Zig Zag is not used in this AFL. It is based on fractals
Contact - [email protected]
*/
_SECTION_BEGIN("Price");
SetChartOptions(0,chartShowArrows|chartShowDates);
_N(Title = StrFormat("{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}", O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));
Plot( C, "Close", ParamColor("Color", colorDefault ), styleNoTitle | ParamStyle("Style") | GetPriceStyle() );
_SECTION_END();
_SECTION_BEGIN("Gartley");
GBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
GBmax = Param("Swing B Max.",0.72,0.4,1,0.01);
GCmin = Param("Swing C Min.",0.38,0.3,1.27,0.01);
GCmax = Param("Swing C Max.",1.0,0.4,1.27,0.01);
GDmin = Param("Swing D Min.(XA)",0.55,0.3,1,0.01);
GDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);
_SECTION_END();
_SECTION_BEGIN("Bat");
BatBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
BatBmax = Param("Swing B Max.",0.55,0.4,1,0.01);
BatCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
BatCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
BatDmin = Param("Swing D Min.(XA)",0.5,0.3,1,0.01);
BatDmax = Param("Swing D Max.(XA)",1.0,0.4,1.0,0.01);
_SECTION_END();
_SECTION_BEGIN("Butterfly");
BtBmin = Param("Swing B Min.",0.55,0.3,1,0.01);
BtBmax = Param("Swing B Max.",0.9,0.4,1,0.01);
BtCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
BtCmax = Param("Swing C Max.",1.27,0.4,1.62,0.01);
BtDmin = Param("Swing D Min.(XA)",1,1,1.8,0.01);
BtDmax = Param("Swing D Max.(XA)",1.38,1,1.8,0.01);
_SECTION_END();
_SECTION_BEGIN("Crab");
CBmin = Param("Swing B Min.",0.38,0.3,1,0.01);
CBmax = Param("Swing B Max.",0.65,0.4,1,0.01);
CCmin = Param("Swing C Min.",0.38,0.3,1.62,0.01);
CCmax = Param("Swing C Max.",1.270,0.4,1.62,0.01);
CDmin = Param("Swing D Min.(XA)",1.25,1,1.8,0.01);
CDmax = Param("Swing D Max.(XA)",1.8,1,2,0.01);
_SECTION_END();
_SECTION_BEGIN("Patterns");
strength = Param("Strength",5,2,15,1);
bu = ParamToggle("Bullish Pattern","Off|On",1);
be = ParamToggle("Bearish Pattern","Off|On",1);
bi = Cum(1)-1;
function GetTop(bars)
{
Top = H == HHV(H,2*bars) AND Ref(HHV(H,bars),bars) < H;
Top = Top AND LastValue(bi)-ValueWhen(Top,bi) > bars;
return Top;
}
function GetValley(bars)
{
Valley = L == LLV(L,2*bars) AND Ref(LLV(L,bars),bars) > L;
Valley = Valley AND LastValue(bi)-ValueWhen(Valley,bi) > bars;
return Valley;
}
// Build fractals array
P1 = GetTop(strength);
V1 = GetValley(Strength);
P1 = IIf(P1,IIf(ValueWhen(P1,bi,2) < ValueWhen(V1,bi),P1,IIf(ValueWhen(P1,H,2) > H,False,P1)),P1);
//P1 = IIf(P1 AND ValueWhen(P1,bi,0) > bi,IIf(ValueWhen(P1,bi,0) < ValueWhen(V1,bi,0),IIf(ValueWhen(P1,H,0) >= H,False,P1),P1),P1);
P1 = IIf(P1 AND ValueWhen(P1,bi,0) > bi AND NOT(ValueWhen(V1,bi,0) > 0 AND ValueWhen(V1,bi,0)<ValueWhen(P1,bi,0)),IIf(ValueWhen(P1,H)<=ValueWhen(P1,H,0),False,P1),P1);
V1 = IIf(V1,IIf(ValueWhen(V1,bi,2) < ValueWhen(P1,bi),V1,IIf(ValueWhen(V1,L,2)<L,False,V1)),V1);
//V1 = IIf(V1 AND ValueWhen(V1,bi,0) > bi ,IIf(ValueWhen(V1,bi,0) < ValueWhen(P1,bi,0),IIf(ValueWhen(V1,L,0) <= L, False,V1),V1),V1);
V1 = IIf(V1 AND ValueWhen(V1,bi,0) > bi AND NOT(ValueWhen(P1,bi,0) > bi AND ValueWhen(P1,bi,0)<ValueWhen(V1,bi,0)) , IIf(ValueWhen(V1,L)>=ValueWhen(V1,L,0),False,V1),V1);
P1H1 = ValueWhen(P1,H);
P1Bar1 = ValueWhen(P1,bi);
P1H2 = ValueWhen(P1,H,2);
P1Bar2 = ValueWhen(P1,bi,2);
V1L1 = ValueWhen(V1,L);
V1Bar1 = ValueWhen(V1,bi);
V1L2 = ValueWhen(V1,L,2);
V1Bar2 = ValueWhen(V1,bi,2);
//Bullish Patterns
PTvalid = (P1Bar1 > V1Bar1 AND V1Bar1 > P1Bar2 AND P1bar2 > V1Bar2) AND P1; // Peaks and troughs are in order
// 4 swings of developing Gartley/Bat etc.
BullGartley4 = PTvalid AND (P1H2-V1L1)/(P1H2-V1L2) > GBmin AND (P1H2-V1L1)/(P1H2-V1L2) < GBmax
AND (P1H1-V1L1)/(P1H2-V1L1) > GCMin AND (P1H1-V1L1)/(P1H2-V1L1) < GCMax;
BullBat4 = PTvalid AND (P1H2-V1L1)/(P1H2-V1L2) > BatBmin AND (P1H2-V1L1)/(P1H2-V1L2) < BatBmax
AND (P1H1-V1L1)/(P1H2-V1L1) > BatCMin AND (P1H1-V1L1)/(P1H2-V1L1) < BatCMax;
BullButterfly4 = PTvalid AND (P1H2-V1L1)/(P1H2-V1L2) > BtBmin AND (P1H2-V1L1)/(P1H2-V1L2) < BtBMax
AND (P1H1-V1L1)/(P1H2-V1L1) > BtCmin AND (P1H1-V1L1)/(P1H2-V1L1) < BtCmax;
BullCrab4 = PTvalid AND (P1H2-V1L1)/(P1H2-V1L2) >CBmin AND (P1H2-V1L1)/(P1H2-V1L2) < CBmax
AND (P1H1-V1L1)/(P1H2-V1L1) > CCmin AND (P1H1-V1L1)/(P1H2-V1L1) < CCmax;
// Bullish Gartley/Bat found. D retacement level is not evaluated
BullGartley = IIf(LowestSince(BullGartley4,L) < ValueWhen(BullGartley4,P1H2)-(ValueWhen(BullGartley4,P1H2)-ValueWhen(BullGartley4,V1L2))*GDmin AND
LowestSince(bullGartley4,L) > ValueWhen(BullGartley4,P1H2)-(ValueWhen(BullGartley4,P1H2)-ValueWhen(BullGartley4,V1L2))*GDmax AND
HighestSince(BullGartley4,H) <= ValueWhen(BullGartley4,P1H1 ) AND
LowestSince(bullGartley4,L)==L, True,False);
BullGartley = BullGartley AND LowestSince(BullGartley4,L) < ValueWhen(BullGartley4,V1L1);
BullBat = IIf(LowestSince(BullBat4,L) < ValueWhen(BullBat4,P1H2)-(ValueWhen(BullBat4,P1H2)-ValueWhen(BullBat4,V1L2))*BatDmin AND
LowestSince(BullBat4,L) > ValueWhen(BullBat4,P1H2)-(ValueWhen(BullBat4,P1H2)-ValueWhen(BullBat4,V1L2))*BatDmax AND
HighestSince(BullBat4,H) <= ValueWhen(BullBat4,P1H1 ) AND
LowestSince(BullBat4,L)==L, True,False);
BullBat = BullBat AND LowestSince(BullCrab4,L) < ValueWhen(BullCrab4,V1L1);
BullCrab = IIf(LowestSince(BullCrab4,L) < ValueWhen(BullCrab4,P1H2)-(ValueWhen(BullCrab4,P1H2)-ValueWhen(BullCrab4,V1L2))*CDmin AND
LowestSince(BullCrab4,L) > ValueWhen(BullCrab4,P1H2)-(ValueWhen(BullCrab4,P1H2)-ValueWhen(BullCrab4,V1L2))*CDmax AND
HighestSince(BullCrab4,H) <= ValueWhen(BullCrab4,P1H1 ) AND
LowestSince(bullGartley4,L)==L, True,False);
BullCrab = BullCrab AND LowestSince(BullCrab4,L) < ValueWhen(BullCrab4,V1L2);
BullButterfly = IIf(LowestSince(BullButterfly4,L) < ValueWhen(BullButterfly4,P1H2)-(ValueWhen(BullButterfly4,P1H2)-ValueWhen(BullButterfly4,V1L2))*BtDMin AND
LowestSince(BullButterfly4,L) > ValueWhen(BullButterfly4,P1H2)-(ValueWhen(BullButterfly4,P1H2)-ValueWhen(BullButterfly4,V1L2))*BtDmax AND
HighestSince(BullButterfly4,H) <= ValueWhen(BullButterfly4,P1H1 ) AND
LowestSince(bullButterfly4,L)==L, True,False);
BullButterfly = BullButterfly AND LowestSince(BullButterfly4,L) < ValueWhen(BullButterfly4,V1L2);
// to remove redumbdent lines
BullHar4 = BullGartley4 OR BullButterfly4 OR BullBat4 OR BullCrab4 ;
BullHar = BullGartley OR BullButterfly OR BullBat OR BullCrab ;
Point4 = IIf(BullHar,ValueWhen(BullHar4,bi),Null);
BullHar = IIf(BullHar, IIf(Point4 == ValueWhen(BullHar,point4,0) AND ValueWhen(BullHar,bi,0) > bi ,False,BullHar),BullHar);
X = ValueWhen(BullHar4,V1L2);
Xbar = ValueWhen(BullHar4,V1Bar2);
A = ValueWhen(BullHar4,P1H2);
Abar = ValueWhen(BullHar4,P1bar2);
B = ValueWhen(BullHar4,V1L1);
Bbar = ValueWhen(BullHar4,V1bar1);
C1 = ValueWhen(BullHar4,P1H1);
C1bar = ValueWhen(BullHar4,P1bar1);
D = ValueWhen(BullHar,L);
Dbar = ValueWhen(BullHar,bi);
ABdXA = (A-B)/(A-X);
BCdAB = (C1-B)/(A-B);
ADdXA = (A-D)/(A-X);
BCdCD = (C1-D)/(C1-B);
PlotPattern = Dbar > C1bar;
if(LastValue(PlotPattern) AND bu)
{
Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",colorbrightgreen,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",colorbrightgreen,styleThick);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",colorbrightgreen,styleThick);
Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",colorbrightgreen,styleThick);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",colorbrightgreen,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",colorbrightgreen,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",colorbrightgreen,styleDashed);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",colorbrightgreen,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",colorbrightgreen,styleDashed);
PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,colorbrightgreen);
PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,colorbrightgreen);
PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,colorbrightgreen);
PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,colorbrightgreen);
}