i added these points where the pattern is valid. See parameter window
showDotsAtD = ParamToggle( "Show Dots at D", "Off|On", 0 );
Chart below shows illustration for the Butterfly Pattern. At the dots the pattern becomes valid.
I added code that you can explore for these points. You have to make sure that you turn the pattern on in the Analysis window. Because the parameters are set separately in the Analysis window.
_SECTION_BEGIN( "Harmonic Patterns" );
/* 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 - joy.edakad@gmail.com
https://forum.amibroker.com/t/harmonic-pattern-gartley/35520
*/
GfxSetZOrder( -5 );
GfxSetCoordsMode( 1 );
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", colorWhite ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );
bi = BarIndex();
fvb = FirstVisibleValue( bi );
lvb = LastVisibleValue( bi );
// general parameters
strength = Param( "BARS of each LINE", 5, 2, 15, 1 ); // So luong bar cho moi duong XA, AB, BC,
bu = ParamToggle( "Bullish Pattern", "Off|On", 1 ); // So bar/lines se quyet dinh. mo^ hinh` duoc ve the' nao`
be = ParamToggle( "Bearish Pattern", "Off|On", 0 );
showABCD = ParamToggle( "Show ABCD", "Off|On", 1 );
showGartley = ParamToggle( "Show Gartley", "Off|On", 0 );
showBat = ParamToggle( "Show Bat", "Off|On", 0 );
showButterfly = ParamToggle( "Show Butterfly", "Off|On", 0 );
showCrab = ParamToggle( "Show Crab", "Off|On", 0 );
ft = ParamList( "Font Type", "Tahoma|Arial Black|Verdana|Courier New|Times New Roman|Open Sans|Segoe UI|DejaVu Sans", 6 );
sz = Param( "Font Size", 20, 8, 30, 1 );
st = Param( "Separation of Letters", 3, 1, 10, 1 );;
showDotsAtD = ParamToggle( "Show Dots at D", "Off|On", 0 );
plotFractals = ParamToggle( "Plot Fractal Pivot Points", "Off|On", 1 );
GBmin = Param( "Gartley Swing B Min.", 0.55, 0.3, 1, 0.01 );
GBmax = Param( "Gartley Swing B Max.", 0.72, 0.4, 1, 0.01 );
GCmin = Param( "Gartley Swing C Min.", 0.38, 0.3, 1.27, 0.01 );
GCmax = Param( "Gartley Swing C Max.", 1.0, 0.4, 1.27, 0.01 );
GDmin = Param( "Gartley Swing D Min.(XA)", 0.55, 0.3, 1, 0.01 );
GDmax = Param( "Gartley Swing D Max.(XA)", 1.0, 0.4, 1.0, 0.01 );
BatBmin = Param( "Bat Swing B Min.", 0.38, 0.3, 1, 0.01 );
BatBmax = Param( "Bat Swing B Max.", 0.55, 0.4, 1, 0.01 );
BatCmin = Param( "Bat Swing C Min.", 0.38, 0.3, 1.62, 0.01 );
BatCmax = Param( "Bat Swing C Max.", 1.27, 0.4, 1.62, 0.01 );
BatDmin = Param( "Bat Swing D Min.(XA)", 0.5, 0.3, 1, 0.01 );
BatDmax = Param( "Bat Swing D Max.(XA)", 1.0, 0.4, 1.0, 0.01 );
BtBmin = Param( "Butterfly Swing B Min.", 0.55, 0.3, 1, 0.01 );
BtBmax = Param( "Butterfly Swing B Max.", 0.9, 0.4, 1, 0.01 );
BtCmin = Param( "Butterfly Swing C Min.", 0.38, 0.3, 1.62, 0.01 );
BtCmax = Param( "Butterfly Swing C Max.", 1.27, 0.4, 1.62, 0.01 );
BtDmin = Param( "Butterfly Swing D Min.(XA)", 1, 1, 1.8, 0.01 );
BtDmax = Param( "Butterfly Swing D Max.(XA)", 1.8, 1, 1.8, 0.01 ); // Max XA of Butterfly = (1.0 - 1.618)
CBmin = Param( "Crab Swing B Min.", 0.38, 0.3, 1, 0.01 );
CBmax = Param( "Crab Swing B Max.", 0.65, 0.4, 1, 0.01 );
CCmin = Param( "Crab Swing C Min.", 0.38, 0.3, 1.62, 0.01 );
CCmax = Param( "Crab Swing C Max.", 1.270, 0.4, 1.62, 0.01 );
CDmin = Param( "Crab Swing D Min.(XA)", 1.25, 1, 1.8, 0.01 );
CDmax = Param( "Crab Swing D Max.(XA)", 1.8, 1, 2, 0.01 );
abcd_Cmin = Param( "ABCD Swing C Min.", 0.3, 0.3 , 1, 0.01 );
abcd_Cmax = Param( "ABCD Swing C Max.", 0.8, 0.8 , 1, 0.01 );
abcd_Dmin = Param( "ABCD Swing D Min.", 1.2, 1, 2.7, 0.01 );
abcd_Dmax = Param( "ABCD Swing D Max.", 3.7, 1, 4, 0.01 );
function GetTop( bars ) // Lay' gia' tri cao nhat' = di?nh
{
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 ) // La'y gia tri thap' nhat' = day'
{
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 ); // so' bar cho 1 duong` XA, AB, BC, CD
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 );
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 );
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
myAX = P1H2 - V1L2;
myAB = P1H2 - V1L1;
myBC = P1H1 - V1L1;
myAB_AX = myAB / myAX;
myBC_AB = myBC / myAB;
BullGartley4 = PTvalid AND( myAB_AX > GBmin ) AND( myAB_AX < GBmax )
AND( myBC_AB > GCMin ) AND( myBC_AB < GCMax ) AND showGartley;
BullBat4 = PTvalid AND( myAB_AX > BatBmin ) AND( myAB_AX < BatBmax )
AND( myBC_AB > BatCMin ) AND( myBC_AB < BatCMax ) AND showBat;
BullButterfly4 = PTvalid AND( myAB_AX > BtBmin ) AND( myAB_AX < BtBMax )
AND( myBC_AB > BtCmin ) AND( myBC_AB < BtCMax ) AND showButterfly;
BullCrab4 = PTvalid AND( myAB_AX > CBmin ) AND( myAB_AX < CBmax )
AND( myBC_AB > CCmin ) AND( myBC_AB < CCmax ) AND showCrab;
BullABCD4 = PTvalid AND( myBC_AB > abcd_Cmin ) AND( myBC_AB < abcd_Cmax ) AND showABCD;
//==================================================
// BULLISH ABCD
// Bullish pattern found. D retracement level is not evaluated
//==================================================
dHigh = HighestSince( BullABCD4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BullABCD4, L );
myC = ValueWhen( BullABCD4, P1H1 );
myB = ValueWhen( BullABCD4, V1L1 );
myA = ValueWhen( BullABCD4, P1H2 );
myX = ValueWhen( BullABCD4, V1L2 );
myCB = myC - myB;
my_d_min = myCB * abcd_DMin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myCB * abcd_DMax ;
my_Cd_min = myC - my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Cd_max = myC - my_d_max;
BullABCD = IIf( ( dLow < my_Cd_min ) AND( dLow > my_Cd_max )
AND( dHigh <= myC ) AND( dLow == L ),
True, False
);
BullABCD = BullABCD AND( dLow < myB );
if( bu AND showABCD AND showDotsAtD )
PlotShapes( IIf( BullABCD, shapeSmallCircle, Null ), colorBrightGreen, 0, L, -20 );
//==================================================
// BULLISH GARTLEY
//==================================================
dHigh = HighestSince( BullGartley4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BullGartley4, L );
myC = ValueWhen( BullGartley4, P1H1 );
myB = ValueWhen( BullGartley4, V1L1 );
myA = ValueWhen( BullGartley4, P1H2 );
myX = ValueWhen( BullGartley4, V1L2 );
myAX = myA - myX;
my_d_min = myAX * GDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * GDMax;
my_Ad_min_GB = my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_GB = my_Ad_max = myA - my_d_max;
BullGartley = IIf( ( dLow < my_Ad_min ) AND( dLow > my_Ad_max )
AND( dHigh <= myC ) AND( dLow == L ),
True, False
);
BullGartley = BullGartley AND( dLow < myB ); // diem D thap' hon B
if( bu AND showGartley AND showDotsAtD )
PlotShapes( IIf( BullGartley, shapeSmallCircle, Null ), colorAqua, 0, L, -20 );
//==================================================
// BULLISH BAT
//==================================================
dHigh = HighestSince( BullBat4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BullBat4, L );
myC = ValueWhen( BullBat4, P1H1 );
myB = ValueWhen( BullBat4, V1L1 );
myA = ValueWhen( BullBat4, P1H2 );
myX = ValueWhen( BullBat4, V1L2 );
myAX = myA - myX;
my_d_min = myAX * BatDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * BatDmax ;
my_Ad_min_Bat = my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_Bat = my_Ad_max = myA - my_d_max;
BullBat = IIf( ( dLow < my_Ad_min ) AND( dLow > my_Ad_max )
AND( dHigh <= myC ) AND( dLow == L ),
True, False
);
BullBat = BullBat AND( dLow < myB ); // diem d thap hon diem B
if( bu AND showBat AND showDotsAtD )
PlotShapes( IIf( BullBat, shapeSmallCircle, Null ), colorAqua, 0, L, -20 );
//==================================================
// BULLISH BUTTTERFLY
//==================================================
dHigh = HighestSince( BullButterfly4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BullButterfly4, L );
myC = ValueWhen( BullButterfly4, P1H1 );
myB = ValueWhen( BullButterfly4, V1L1 );
myA = ValueWhen( BullButterfly4, P1H2 );
myX = ValueWhen( BullButterfly4, V1L2 );
myAX = myA - myX;
my_d_min = myAX * BtDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * BtDmax ;
my_Ad_min_Bt = my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_Bt = my_Ad_max = myA - my_d_max;
BullButterfly = IIf( ( dLow < my_Ad_min ) AND( dLow > my_Ad_max )
AND( dHigh <= myC ) AND( dLow == L ),
True, False
);
BullButterfly = BullButterfly AND( dLow < myX ); // diem D thap' hon X
if( bu AND showButterfly AND showDotsAtD )
PlotShapes( IIf( BullButterfly, shapeSmallCircle, Null ), colorAqua, 0, L, -20 );
//==================================================
// BULLISH CRAB - CUA
//==================================================
dHigh = HighestSince( BullCrab4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BullCrab4, L );
myC = ValueWhen( BullCrab4, P1H1 );
myB = ValueWhen( BullCrab4, V1L1 );
myA = ValueWhen( BullCrab4, P1H2 );
myX = ValueWhen( BullCrab4, V1L2 );
myAX = myA - myX;
my_d_min = myAX * CDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * CDmax ;
my_Ad_min_CB = my_Ad_min = myA - my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_CB = my_Ad_max = myA - my_d_max;
BullCrab = IIf( ( dLow < my_Ad_min ) AND( dLow > my_Ad_max )
AND( dHigh <= myC ) AND( dLow == L ),
True, False
);
BullCrab = BullCrab AND( dLow < myX ); // diem D thap' hon X
if( bu AND showCrab AND showDotsAtD )
PlotShapes( IIf( BullCrab, shapeSmallCircle, Null ), colorAqua, 0, L, -20 );
//==========================================================
// VE DUONG CHO MO HINH BULLISH ABCB
//==========================================================
BullHar4 = BullABCD4;
BullHar = BullABCD;
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 );
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 );
BCdAB = ( C1 - B ) / ( A - B );
BCdCD = ( C1 - D ) / ( C1 - B );
PlotPattern = Dbar > C1bar;
// Bullish ABCD pattern
for( i = lvb; i > fvb; i-- )
{
bullcolor = colorGreen;
linethick = 1;
if( BullHar[i] AND bu )
{
Plot( LineArray( Abar[i], A[i], Bbar[i], B[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], C1bar[i], C1[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( C1bar[i], C1[i], Dbar[i], D[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], C1bar[i], C1[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], Dbar[i], D[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
PlotTextSetFont( NumToStr( BCdAB[i], 1.2 ), ft, sz, ( C1bar[i] + Abar[i] ) / 2, ( C1[i] + A[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdCD[i], 1.2 ), ft, sz, ( Bbar[i] + Dbar[i] ) / 2, ( B[i] + D[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( "A", ft, sz, Abar[i] - st, A[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "B", ft, sz, Bbar[i] - st, B[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "C", ft, sz, C1bar[i] + st, C1[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "D", ft, sz, Dbar[i] + st, D[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "Bullish ABCD", ft, sz, Abar[i], A[i], bullcolor, colorDefault, 20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Cd_min[i];
x1 = Dbar[i];
y1 = my_Cd_max[i];
GfxSelectPen( ColorRGB( 0, 80, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 0, 10, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
}
//==========================================================
// VE DUONG CHO MO HINH BULLISH BAT, GARTLEY, BUTTERFLY, CRAB
//==========================================================
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;
LatPattern = bullHar;
Lastpattern = ValueWhen( LatPattern, IIf( BullGartley, 1, IIf( BullBat, 2, IIf( BullButterfly, 3, IIf( BullCrab, 4, Null ) ) ) ) );
PatternName = WriteIf( LastPattern == 1, "Bullish Gartley", WriteIf( LastPattern == -1, "Bearish Gartley", WriteIf( LastPattern == 2, "Bullish Bat",
WriteIf( LastPattern == -2, "Bearish Bat", WriteIf( LastPattern == 3, "Bullish Butterfly", WriteIf( LastPattern == -3, "Bearish Butterfly",
WriteIf( LastPattern == 4, "Bullish Crab", WriteIf( LastPattern == -4, "Bearish Crab", "None" ) ) ) ) ) ) ) );
// HARMONIC PATTERNS
for( i = lvb; i > fvb; i-- )
{
bullcolor = colorBlue;
linethick = 1;
if( BullHar[i] AND bu )
{
Plot( LineArray( Xbar[i], X[i], Abar[i], A[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], Bbar[i], B[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], C1bar[i], C1[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( C1bar[i], C1[i], Dbar[i], D[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Bbar[i], B[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Abar[i], A[i] ), "", bullcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], C1bar[i], C1[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], Dbar[i], D[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Dbar[i], D[i] ), "", bullcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
PlotTextSetFont( NumToStr( ABdXA[i], 1.2 ), ft, sz, ( Bbar[i] + Xbar[i] ) / 2, ( B[i] + X[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdAB[i], 1.2 ), ft, sz, ( C1bar[i] + Abar[i] ) / 2, ( C1[i] + A[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( ADdXA[i], 1.2 ), ft, sz, ( Dbar[i] + Xbar[i] ) / 2, ( D[i] + X[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdCD[i], 1.2 ), ft, sz, ( Bbar[i] + Dbar[i] ) / 2, ( B[i] + D[i] ) / 2, bullcolor, colorDefault, 0 );
PlotTextSetFont( "X", ft, sz, Xbar[i] - st, X[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "A", ft, sz, Abar[i] - st, A[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "B", ft, sz, Bbar[i] - st, B[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "C", ft, sz, C1bar[i] + st, C1[i], bullcolor, colorDefault, 0 );
PlotTextSetFont( "D", ft, sz, Dbar[i] + st, D[i], bullcolor, colorDefault, 0 );
lp = Lastpattern[i];
GfxSelectPen( ColorRGB( 0, 0, 120 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 0, 0, 30 ) );
if( lp == 1 )
{
PlotTextSetFont( "Bullish Gartley", ft, sz, Xbar[i], X[i], bullcolor, colorDefault, -20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_GB[i];
x1 = Dbar[i];
y1 = my_Ad_max_GB[i];
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == 2 )
{
PlotTextSetFont( "Bullish Bat", ft, sz, Xbar[i], X[i], bullcolor, colorDefault, -20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_Bat[i];
x1 = Dbar[i];
y1 = my_Ad_max_Bat[i];
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == 3 )
{
PlotTextSetFont( "Bullish Butterfly", ft, sz, Xbar[i], X[i], bullcolor, colorDefault, -20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_Bt[i];
x1 = Dbar[i];
y1 = my_Ad_max_Bt[i];
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == 4 )
{
PlotTextSetFont( "Bullish Crab", ft, sz, Xbar[i], X[i], bullcolor, colorDefault, -20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_CB[i];
x1 = Dbar[i];
y1 = my_Ad_max_CB[i];
GfxRectangle( x0, y0, x1, y1 );
}
}
}
//=============================================================
// BEARISH PATTERNS
//=============================================================
PTvalid = ( V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2 ) AND V1;
myAX = P1H2 - V1L2;
myAB = P1H1 - V1L2;
myBC = P1H1 - V1L1;
myAB_AX = myAB / myAX;
myBC_AB = myBC / myAB;
BearGartley4 = PTvalid AND( myAB_AX > GBmin ) AND( myAB_AX < GBmax )
AND( myBC_AB > GCMin ) AND( myBC_AB < GCMax ) AND showGartley;
BearBat4 = PTvalid AND( myAB_AX > BatBmin ) AND( myAB_AX < BatBmax )
AND( myBC_AB > BatCMin ) AND( myBC_AB < BatCMax ) AND showBat;
BearButterfly4 = PTvalid AND( myAB_AX > BtBmin ) AND( myAB_AX < BtBMax )
AND( myBC_AB > BtCmin ) AND( myBC_AB < BtCMax ) AND showButterfly;
BearCrab4 = PTvalid AND( myAB_AX > CBmin ) AND( myAB_AX < CBmax )
AND( myBC_AB > CCmin ) AND( myBC_AB < CCmax ) AND showCrab;
BearABCD4 = PTvalid AND( myBC_AB > abcd_Cmin ) AND( myBC_AB < abcd_Cmax ) AND showABCD;
//==========================================================
// BEARISH ABCD
// Bearish pattern found. D retracement level is not evaluated
//==========================================================
dHigh = HighestSince( BearABCD4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BearABCD4, L );
myA = ValueWhen( BearABCD4, V1L2 );
myB = ValueWhen( BearABCD4, P1H1 );
myC = ValueWhen( BearABCD4, V1L1 );
myCB = myB - myC;
my_d_min = myCB * abcd_DMin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myCB * abcd_DMax ;
my_Cd_min = myC + my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Cd_max = myC + my_d_max;
BearABCD = IIf( ( dHigh > my_Cd_min ) AND( dHigh < my_Cd_max )
AND( dLow >= myC ) AND( dHigh == H ),
True, False
);
BearABCD = BearABCD AND( dHigh > myB );
if( be AND showABCD AND showDotsAtD )
PlotShapes( IIf( BearABCD, shapeSmallCircle, Null ), colorGold, 0, H, 20 );
//=============================================================
// BEARISH GARTLEY
//=============================================================
dHigh = HighestSince( BearGartley4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BearGartley4, L );
myX = ValueWhen( BearGartley4, P1H2 );
myA = ValueWhen( BearGartley4, V1L2 );
myAX = myX - myA;
myB = ValueWhen( BearGartley4, P1H1 );
myC = ValueWhen( BearGartley4, V1L1 );
my_d_min = myAX * GDmin; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * GDMax;
my_Ad_min_GB = my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_GB = my_Ad_max = myA + my_d_max;
BearGartley = IIf( ( dHigh > my_Ad_min ) AND( dHigh < my_Ad_max )
AND( dLow >= myC ) AND( dHigh == H ),
True, False
);
BearGartley = BearGartley AND( dHigh > myB ); // diem D cao hon B
if( be AND showGartley AND showDotsAtD )
PlotShapes( IIf( BearGartley, shapeSmallCircle, Null ), colorOrange, 0, H, 20 );
//=============================================================
// BEARISH BAT
//=============================================================
dHigh = HighestSince( BearBat4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BearBat4, L );
myX = ValueWhen( BearBat4, P1H2 );
myA = ValueWhen( BearBat4, V1L2 );
myAX = myX - myA;
myB = ValueWhen( BearBat4, P1H1 );
myC = ValueWhen( BearBat4, V1L1 );
my_d_min = myAX * BatDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * BatDMax ;
my_Ad_min_Bat = my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_Bat = my_Ad_max = myA + my_d_max;
BearBat = IIf( ( dHigh > my_Ad_min ) AND( dHigh < my_Ad_max )
AND( dLow >= myC ) AND( dHigh == H ),
True, False
);
BearBat = BearBat AND( dHigh > myB ); // diem D cao hon B
if( be AND showBat AND showDotsAtD )
PlotShapes( IIf( BearBat, shapeSmallCircle, Null ), colorOrange, 0, H, 20 );
//=============================================================
// BEARISH BUTTERFLY
//=============================================================
dHigh = HighestSince( BearButterfly4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BearButterfly4, L );
myX = ValueWhen( BearButterfly4, P1H2 );
myA = ValueWhen( BearButterfly4, V1L2 );
myAX = myX - myA;
myB = ValueWhen( BearButterfly4, P1H1 );
myC = ValueWhen( BearButterfly4, V1L1 );
my_d_min = myAX * BtDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * BtDmax ;
my_Ad_min_Bt = my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_Bt = my_Ad_max = myA + my_d_max;
BearButterfly = IIf( ( dHigh > my_Ad_min ) AND( dHigh < my_Ad_max )
AND( dLow >= myC ) AND( dHigh == H ),
True, False
);
BearButterfly = BearButterfly AND( dHigh > myX ); // diem D cao hon X
if( be AND showButterfly AND showDotsAtD )
PlotShapes( IIf( BearButterfly, shapeSmallCircle, Null ), colorOrange, 0, H, 20 );
//=============================================================
// BEARISH CRAB
//=============================================================
dHigh = HighestSince( BearCrab4, H ); // Tinh' gia' tri min, max cua duong Ad. Duong Ad la duong con cua AD
dLow = LowestSince( BearCrab4, L );
myX = ValueWhen( BearCrab4, P1H2 );
myA = ValueWhen( BearCrab4, V1L2 );
myAX = myX - myA;
myB = ValueWhen( BearCrab4, P1H1 );
myC = ValueWhen( BearCrab4, V1L1 );
my_d_min = myAX * CDmin ; // Tinh' gia' tri cua duong Ad con. Khi gia' giam? tu` tre^n xuong' thi` max -> min
my_d_max = myAX * CDmax ;
my_Ad_min_CB = my_Ad_min = myA + my_d_min; // Khoang dich chuyen cua duong Ad con.
my_Ad_max_CB = my_Ad_max = myA + my_d_max;
BearCrab = IIf( ( dHigh > my_Ad_min ) AND( dHigh < my_Ad_max )
AND( dLow >= myC ) AND( dHigh == H ),
True, False
);
BearCrab = BearCrab AND( dHigh > myX ); // diem D cao hon X
if( be AND showCrab AND showDotsAtD )
PlotShapes( IIf( BearCrab, shapeSmallCircle, Null ), colorOrange, 0, H, 20 );
//==========================================================
// VE DUONG CHO MO HINH BEARISH ABCD
//==========================================================
BearHar4 = BearABCD4;
BearHar = BearABCD;
Point4 = IIf( BearHar, ValueWhen( BearHar4, bi ), Null );
BearHar = IIf( BearHar, IIf( Point4 == ValueWhen( BearHar, point4, 0 ) AND ValueWhen( BearHar, bi, 0 ) > bi , False, BearHar ), BearHar );
A = ValueWhen( BearHar4, V1L2 );
Abar = ValueWhen( BearHar4, V1bar2 );
B = ValueWhen( BearHar4, P1H1 );
Bbar = ValueWhen( BearHar4, P1bar1 );
C1 = ValueWhen( BearHar4, V1L1 );
C1bar = ValueWhen( BearHar4, V1bar1 );
D = ValueWhen( BearHar, H );
Dbar = ValueWhen( BearHar, bi );
BCdAB = ( B - C1 ) / ( B - A );
BCdCD = ( D - C1 ) / ( B - C1 );
PlotPattern = Dbar > C1bar;
// Bearish ABCD
for( i = lvb; i > fvb; i-- )
{
bearcolor = colorYellow;
linethick = 1;
if( BearHar[i] AND be )
{
Plot( LineArray( Abar[i], A[i], Bbar[i], B[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], C1bar[i], C1[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( C1bar[i], C1[i], Dbar[i], D[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], C1bar[i], C1[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], Dbar[i], D[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
PlotTextSetFont( NumToStr( BCdAB[i], 1.2 ), ft, sz, ( C1bar[i] + Abar[i] ) / 2, ( C1[i] + A[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdCD[i], 1.2 ), ft, sz, ( Dbar[i] + Bbar[i] ) / 2, ( D[i] + B[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( "A", ft, sz, Abar[i] - st, A[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "B", ft, sz, Bbar[i] - st, B[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "C", ft, sz, C1bar[i] + st, C1[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "D", ft, sz, Dbar[i] + st, D[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "Bearish ABCD", ft, sz, Abar[i] - 2, A[i], bearcolor, colorDefault, -20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Cd_min[i];
x1 = Dbar[i];
y1 = my_Cd_max[i];
GfxSelectPen( ColorRGB( 80, 80, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 10, 10, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
}
//==========================================================
// VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB
//==========================================================
BearHar4 = BearGartley4 OR BearButterfly4 OR BearBat4 OR BearCrab4 ;
BearHar = BearGartley OR BearButterfly OR BearBat OR BearCrab ;
Point4 = IIf( BearHar, ValueWhen( BearHar4, bi ), Null );
BearHar = IIf( BearHar, IIf( Point4 == ValueWhen( BearHar, point4, 0 ) AND ValueWhen( BearHar, bi, 0 ) > bi , False, BearHar ), BearHar );
X = ValueWhen( BearHar4, P1H2 );
Xbar = ValueWhen( BearHar4, P1Bar2 );
A = ValueWhen( BearHar4, V1L2 );
Abar = ValueWhen( BearHar4, V1bar2 );
B = ValueWhen( BearHar4, P1H1 );
Bbar = ValueWhen( BearHar4, P1bar1 );
C1 = ValueWhen( BearHar4, V1L1 );
C1bar = ValueWhen( BearHar4, V1bar1 );
D = ValueWhen( BearHar, H );
Dbar = ValueWhen( BearHar, bi );
ABdXA = ( B - A ) / ( X - A );
BCdAB = ( B - C1 ) / ( B - A );
ADdXA = ( D - A ) / ( X - A );
BCdCD = ( D - C1 ) / ( B - C1 );
PlotPattern = Dbar > C1bar;
LatPattern = bullHar OR bearHar;
Lastpattern = ValueWhen( LatPattern, IIf( BearGartley, -1, IIf( BearBat, -2,
IIf( BearButterfly, -3, IIf( BearCrab, -4, Null ) ) ) ) );
PatternName = WriteIf( LastPattern == 1, "Bullish Gartley", WriteIf( LastPattern == -1, "Bearish Gartley", WriteIf( LastPattern == 2, "Bullish Bat",
WriteIf( LastPattern == -2, "Bearish Bat", WriteIf( LastPattern == 3, "Bullish Butterfly", WriteIf( LastPattern == -3, "Bearish Butterfly",
WriteIf( LastPattern == 4, "Bullish Crab", WriteIf( LastPattern == -4, "Bearish Crab", "None" ) ) ) ) ) ) ) );
for( i = lvb; i > fvb; i-- )
{
bearcolor = colorRed;
linethick = 1;
if( BearHar[i] AND be )
{
Plot( LineArray( Xbar[i], X[i], Abar[i], A[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], Bbar[i], B[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], C1bar[i], C1[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( C1bar[i], C1[i], Dbar[i], D[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Bbar[i], B[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Abar[i], A[i] ), "", bearcolor, styleThick | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Abar[i], A[i], C1bar[i], C1[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Bbar[i], B[i], Dbar[i], D[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
Plot( LineArray( Xbar[i], X[i], Dbar[i], D[i] ), "", bearcolor, styleDashed | styleNoRescale, Null, Null, 0, 10, linethick );
PlotTextSetFont( NumToStr( ABdXA[i], 1.2 ), ft, sz, ( Bbar[i] + Xbar[i] ) / 2, ( B[i] + X[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdAB[i], 1.2 ), ft, sz, ( C1bar[i] + Abar[i] ) / 2, ( C1[i] + A[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( BCdCD[i], 1.2 ), ft, sz, ( Dbar[i] + Bbar[i] ) / 2, ( D[i] + B[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( NumToStr( ADdXA[i], 1.2 ), ft, sz, ( Dbar[i] + Xbar[i] ) / 2, ( D[i] + X[i] ) / 2, bearcolor, colorDefault, 0 );
PlotTextSetFont( "X", ft, sz, Xbar[i] - st, X[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "A", ft, sz, Abar[i] - st, A[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "B", ft, sz, Bbar[i] - st, B[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "C", ft, sz, C1bar[i] + st, C1[i], bearcolor, colorDefault, 0 );
PlotTextSetFont( "D", ft, sz, Dbar[i] + st, D[i], bearcolor, colorDefault, 0 );
lp = Lastpattern[i];
if( lp == -1 )
{
PlotTextSetFont( "Bearish Gartley", ft, sz, Xbar[i], X[i], bearcolor, colorDefault, 20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_GB[i];
x1 = Dbar[i];
y1 = my_Ad_max_GB[i];
GfxSelectPen( ColorRGB( 120, 0, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 30, 0, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == -2 )
{
//PlotText( "Bullish Bat", Xbar[i], X[i], bearcolor, colorDefault, 20 );
PlotTextSetFont( "Bearish Bat", ft, sz, Xbar[i], X[i], bearcolor, colorDefault, 20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_Bat[i];
x1 = Dbar[i];
y1 = my_Ad_max_Bat[i];
GfxSelectPen( ColorRGB( 120, 0, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 30, 0, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == -3 )
{
//PlotText( "Bullish Butterfly", Xbar[i], X[i], bearcolor, colorDefault, 20 );
PlotTextSetFont( "Bearish Butterfly", ft, sz, Xbar[i], X[i], bearcolor, colorDefault, 20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_Bt[i];
x1 = Dbar[i];
y1 = my_Ad_max_Bt[i];
GfxSelectPen( ColorRGB( 120, 0, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 30, 0, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
if( lp == -4 )
{
//PlotText( "Bullish Crab", Xbar[i], X[i], bearcolor, colorDefault, 20 );
PlotTextSetFont( "Bearish Crab", ft, sz, Xbar[i], X[i], bearcolor, colorDefault, 20 );
// draw rectangle
x0 = C1bar[i];
y0 = my_Ad_min_CB[i];
x1 = Dbar[i];
y1 = my_Ad_max_CB[i];
GfxSelectPen( ColorRGB( 120, 0, 0 ), 2, 0 );
GfxSelectSolidBrush( ColorRGB( 30, 0, 0 ) );
GfxRectangle( x0, y0, x1, y1 );
}
}
}
//=================================
// Show diem ho^~ tro. va` khang' cu. ko?
//=================================
if( PlotFractals )
{
PlotShapes( shapeSmallCircle * P1, colorRed, 0, H, 10 );
PlotShapes( shapeSmallCircle * V1, colorBlue, 0, L, -10 );
}
if( Status( "action" ) == actionExplore )
{
Filter = (
BullBat OR BearBat OR
BullGartley OR BearGartley OR
BullButterfly OR BearButterfly OR
BullCrab OR BearCrab
OR BullABCD OR BearABCD );
PatternDirection =
IIf( BullGartley, 1,
IIf( BearGartley, -1,
IIf( BullBat, 2,
IIf( BearBat, -2,
IIf( BullButterfly, 3,
IIf( BearButterfly, -3,
IIf( BullCrab, 4,
IIf( BearCrab, -4,
IIf( BullABCD, 5,
IIf( BearABCD, -5, 0 ) ) ) ) ) ) ) ) ) );
TextColor = IIf( patternDirection > 0, ColorRGB( 0, 255, 0 ), ColorRGB( 255, 0, 0 ) );
AddColumn( PatternDirection, "Pattern Number", 1.0, TextColor, colorWhite );
PatternValue =
IIf( BullGartley, 1,
IIf( BearGartley, 2,
IIf( BullBat, 3,
IIf( BearBat, 4,
IIf( BullButterfly, 5,
IIf( BearButterfly, 6,
IIf( BullCrab, 7,
IIf( BearCrab, 8,
IIf( BullABCD, 9,
IIf( BearABCD, 10, 0 ) ) ) ) ) ) ) ) ) );
PatternList = "None\nBullish Gartley\nBearish Gartley\nBullish Bat\nBearish Bat\nBullish Butterfly\nBearish Butterfly\nBullish Crab\nBearish Crab\nBullish ABCD\nBearish ABCD";
AddMultiTextColumn( PatternValue, PatternList, "Pattern Name", 1.0, TextColor, colorWhite, 125 ); // version 6.2 and Higher
}
_SECTION_END();