Stock Portfolio Organizer

The ultimate porfolio management solution.

Shares, Margin, CFD's, Futures and Forex
EOD and Realtime
Dividends and Trust Distributions
And Much More ....
For Portfolio Manager Click Here

WiseTrader Toolbox

#1 Selling Amibroker Plugin featuring:

Advanced Adaptive Indicators
Advanced Pattern Exploration
Neural Networks
And Much More ....
Find Out More Here

Harmonic Pattern with Shark Test123 for Amibroker (AFL)
hbkwarez
almost 8 years ago
Amibroker (AFL)

Rating:
3 / 5 (Votes 3)
Tags:
amibroker, exploration, pattern

Added Shark Bull & Bear to the Harmonic Pattern Detection formula

Indicator / Formula

Copy & Paste Friendly
/*                     HARMONIC PATTERN DETECTION                        


Automatic Detection of Harmonic Patterns - Gartley, Bat, Butterfly and Crab.Shark++

Zig Zag is not used in this AFL. It is based on fractals


*/

showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);


Plot( O, "Open", ParamColor("Color", colorBlack ), styleNoDraw); 
Plot( H, "High", ParamColor("Color", colorBlack ), styleNoDraw); 
Plot( L, "Low", ParamColor("Color", colorBlack ),styleNoDraw);
Plot( C, "Close", ParamColor("Color", colorBlack ),styleNoDraw, 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.92,0.4,1,0.01);
BtCmin = Param("Swing C Min.",0.34,0.3,1,0.01);
BtCmax = Param("Swing C Max.",0.92,0.7,1,0.01);
BtDmin = Param("Swing D Min.(XA)",1.2,1,1.8,0.01);
BtDmax = Param("Swing D Max.(XA)",2.3,1,3.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)",3,1,2,0.01);

_SECTION_END();

///experiment

_SECTION_BEGIN("Shark");

SBmin = Param("Swing B Min.",0.34,0.3,1,0.01);
SBmax = Param("Swing B Max.",0.95,0.4,1,0.01);
SCmin = Param("Swing C Min.",1.0,1.0,1.2,0.01);
SCmax = Param("Swing C Max.",1.7,1,2.7,0.01);
SDmin = Param("Swing D Min.(XA)",0.7,0.7,1.2,0.01);
SDmax = Param("Swing D Max.(XA)",1.34,1.0,2.7,0.01);

_SECTION_END();

_SECTION_BEGIN("Shark2");

SBmin = Param("Swing B Min.",0.34,0.3,1,0.01);
SBmax = Param("Swing B Max.",0.95,0.4,1,0.01);
SCmin = Param("Swing C Min.",1.0,1.0,1.2,0.01);
SCmax = Param("Swing C Max.",1.7,1,2.7,0.01);
SDmin = Param("Swing D Min.(XA)",0.7,0.7,1.2,0.01);
SDmax = Param("Swing D Max.(XA)",1.68,1.0,2.7,0.01);

_SECTION_END();

_SECTION_BEGIN("AB=CD");

abcd_Cmin = Param("Swing C Min.",0.3,		0.3	,	1,		0.01);
abcd_Cmax = Param("Swing C Max.",0.8,		0.8	,	1,		0.01);
abcd_Dmin = Param("Swing D Min.",1.2,		1,		2.7,	0.01);
abcd_Dmax = Param("Swing D Max.",3.7,		1,		4,		0.01);

_SECTION_END();

_SECTION_BEGIN("Patterns");
	
//strength = Param("Strength",5,2,15,1);									// Best use: 3, 4, 5
strength = Param("BARS of each LINE",6,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",1);

bi = Cum(1)-1;

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
//============================================
/*
	Mo hinh Bullish:
	A	=	P1H2
	B	=	V1L1
	C	=	P1H1
	X	=	V1L2

*/

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	); 

BullBat4 			= PTvalid 	AND 	(	myAB_AX > BatBmin ) 		AND (	myAB_AX < BatBmax	)
								AND 	(	myBC_AB > BatCMin ) 		AND (	myBC_AB < BatCMax	); 

BullButterfly4 	= PTvalid 	AND 	(	myAB_AX > BtBmin ) 		AND (	myAB_AX < BtBMax	)
								AND  	(	myBC_AB > BtCmin ) 		AND (	myBC_AB < BtCmin 	);

BullCrab4 			= PTvalid 	AND 	(	myAB_AX > CBmin )	  		AND (	myAB_AX < CBmax 	)
								AND  	(	myBC_AB > CCmin ) 		AND (	myBC_AB < CCmax	);

BullABCD4			= PTvalid AND 	(	myBC_AB > abcd_Cmin) 	AND (	myBC_AB < abcd_Cmax	);

strPattern = "";

//==================================================
//				 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);


//==================================================
// 				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	=		myA - my_d_min;							// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BullGartley,"BULLISH GARTLEY",strPattern);



//==================================================
// 				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	=		myA - my_d_min;							// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BullBat,"BULLISH BAT",strPattern);


//==================================================
// 				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	=		myA - my_d_min;						// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BullCrab ,"BULLISH CRAB",strPattern);



//==================================================
// 				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	=		myA - my_d_min;									// Khoang dich chuyen cua duong Ad con.
	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
strPattern 		=		WriteIf(BullButterfly ,"BULLISH BUTTERFLY",strPattern);


//==================================================
// 				Shark code
//==================================================


BullShark4 = PTvalid AND (P1H2-V1L1)/(P1H2-V1L2) > SBmin AND (P1H2-V1L1)/(P1H2-V1L2) < SBmax
				AND (P1H1-V1L1)/(P1H2-V1L1) > SCMin AND (P1H1-V1L1)/(P1H2-V1L1) < SCMax; 

BullShark = IIf(LowestSince(BullShark4,L) < ValueWhen(BullShark4,P1H2)-(ValueWhen(BullShark4,P1H2)-ValueWhen(BullShark4,V1L2))*SDmin AND
						LowestSince(bullShark4,L) > ValueWhen(BullShark4,P1H2)-(ValueWhen(BullShark4,P1H2)-ValueWhen(BullShark4,V1L2))*SDmax AND
						HighestSince(BullShark4,H) <= ValueWhen(BullShark4,P1H1  ) AND
						LowestSince(bullShark4,L)==L, True,False);
strPattern 		=		WriteIf(BullShark ,"BULLISH Shark",strPattern);

BullShark = BullShark AND LowestSince(BullShark4,L) < ValueWhen(BullShark4,V1L1);

// Bearish Patterns

PTvalid = (V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2) AND V1;

BearShark4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > SBmin AND (P1H1-V1L2)/(P1H2-V1L2) < SBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > SCmin AND (P1H1-V1L1)/(P1H1-V1L2) < SCmax;
				

BearShark = IIf(HighestSince(bearShark4,H) > ValueWhen(BearShark4,V1L2) + (ValueWhen(BearShark4,P1H2) - ValueWhen(BearShark4,V1L2))*SDmin AND
				 HighestSince(bearShark4,H) < ValueWhen(BearShark4,V1L2) + (ValueWhen(BearShark4,P1H2) - ValueWhen(BearShark4,V1L2))*SDMax AND	
				LowestSince(BearShark4,L) >= ValueWhen(BearShark4,V1L1  ) AND
				HighestSince(BearShark4,H) == H, True, False);

				
BearShark = BearShark AND HighestSince(BearShark4,H) > ValueWhen(BearShark4,P1H1);
strPattern 		=		WriteIf(BearShark ,"Bear Shark",strPattern);


//==================================================

BullHar4 = BullGartley4 OR BullButterfly4 OR BullBat4 OR BullCrab4 OR BullShark4;
BullHar = BullGartley OR BullButterfly OR BullBat OR BullCrab OR BullShark ;

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)),"",colorBlue,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",colorBlue,styleThick);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",colorBlue,styleThick);
Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",colorBlue,styleThick);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",colorBlue,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",colorBlue,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",colorBlue,styleDashed);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",colorBlue,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",colorBlue,styleDashed);



PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,colorBlue);
PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,colorBlue);
PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,colorBlue);
PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,colorBlue);
}







//==========================================================
//   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;

if(LastValue(PlotPattern) AND bu)
{
		ColorX = colorLime;
	// Ve cac duong AB, BC, CD
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",colorLime ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",colorLime ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",colorLime ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Ve cac gia tri Fibo
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX );

	//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
		xlech		=	0;
		ylech 		= 	2;
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	+	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	-	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	+	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	-	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="")  
		{
			myStr			=	"Pattern: BULLISH AB=CD";
			toadoX			=	LastValue(Abar);
			toadoY			=	LastValue(D);

			PlotText(myStr,toadoX,toadoY,ColorX );
		}

}			//	end of Ve duong` bullish abcd



//==========================================================
//   VE DUONG CHO MO HINH BULLISH BAT, GARTLEY, BUTTERFLY, CRAB
//==========================================================


BullHar4 = BullGartley4 OR BullButterfly4 OR BullBat4 OR BullCrab4 OR  BullShark4 ;
BullHar = BullGartley OR BullButterfly OR BullBat OR BullCrab OR  BullShark;

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)
{
			ColorX	= colorBlue;
		// Ve cac duong XA, AB, BC, CD
			Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX,styleThick);
			Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);
			Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX,styleDashed);

		// Ve cac gia tri Fibo
			PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX);
			PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX);
			PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX);
			PlotText(NumToStr(LastValue(BCdCD),1.2),(LastValue(Bbar)+LastValue(Dbar))/2,(LastValue(B)+LastValue(D))/2,ColorX);

		//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
			xlech		=	0;
			ylech 		= 	2;
			PlotText("X",LastValue(Xbar)	+ 	xlech,	LastValue(X)	-	ylech,	ColorX);
			PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	+	ylech,	ColorX);
			PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	-	ylech,	ColorX);
			PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	+	ylech,	ColorX);
			PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	-	ylech,	ColorX);

		//--------- Viet thuyet minh mo hinh: by binhnd--------------
			if (strPattern!="")  
			{
				strPattern 	= 	"Pattern: " + strPattern;
				toadoX			=	(LastValue(Dbar)+LastValue(Xbar))/2;
				toadoY			=	(LastValue(D)+LastValue(X))/2;

				PlotText(strPattern,toadoX,toadoY-2,ColorX);
			}

}			// end of Ve duong cho cac mo hinh Crab, Butterfly, Bat


//=============================================================
//				BEARISH PATTERNS
//=============================================================

PTvalid = (V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2) AND V1;

/*=====================
		X 	= 	P1H2					 Trong mo hinh` bear: Die^m X cao hon diem A. MyAX = X-> A
		A	=	V1L2
		B	=	P1H1
		C	=	V1L1

=======================*/
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	); 

BearBat4 			= PTvalid 	AND 	(	myAB_AX > BatBmin ) 		AND (	myAB_AX < BatBmax	)
								AND 	(	myBC_AB > BatCMin ) 		AND (	myBC_AB < BatCMax	); 

BearButterfly4 	= PTvalid 	AND 	(	myAB_AX > BtBmin ) 		AND (	myAB_AX < BtBMax	)
								AND  	(	myBC_AB > BtCmin ) 		AND (	myBC_AB < BtCmin 	);

BearCrab4 			= PTvalid 	AND 	(	myAB_AX > CBmin )	  		AND (	myAB_AX < CBmax 	)
								AND  	(	myBC_AB > CCmin ) 		AND (	myBC_AB < CCmax	);

BearABCD4			= PTvalid AND 	(	myBC_AB > abcd_Cmin) 	AND (	myBC_AB < abcd_Cmax	);

strPattern = "";



//==========================================================
//				 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);

//=============================================================
//				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	=		myA 	+ 	my_d_min;					// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BearGartley ,"BEARISH GARTLEY",strPattern);

//=============================================================
//				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	=		myA 	+ 	my_d_min;					// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BearBat ,"BEARISH BAT",strPattern);


//=============================================================
//				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	=		myA 	+ 	my_d_min;						// Khoang dich chuyen cua duong Ad con.
	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
strPattern		=		WriteIf(BearButterfly ,"BEARISH BUTTERFLY",strPattern);



//=============================================================
//				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	=		myA 	+ 	my_d_min;						// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BearCrab ,"BEARISH CRAB",strPattern);




//==========================================================
//   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;

//--------- Ve duong ------------------
if(LastValue(Plotpattern) AND be)
{
		ColorX = colorYellow;
	// Ve duong AB, BC
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Viet cac gia tri Fibo tren duong AB, BC
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );

	//---------- Viet cac diem A, B, C, D: by binhnd---------------------
		xlech		=	-1;
		ylech 		= 	1;
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	-	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	+	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	-	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	+	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="") 
			{
				myStr			=	"Pattern: BEARISH AB=CD";
				toadoaX		=	LastValue(Abar);
				toadoY			=	LastValue(D);

				PlotText(myStr,toadoaX,toadoY+1,ColorX );
			}
	
}			// end of VE DUONG CHO MO HINH BEARISH ABCD


//==========================================================
//   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;

//--------- Ve duong ------------------
if(LastValue(Plotpattern) AND be)
{
		ColorX = colorRed;
	// Ve duong XA, AB, BC
		Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",ColorX ,styleThick);
		Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);
		Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",ColorX ,styleDashed);

	// Viet cac gia tri Fibo tren duong XA, AB, BC
		PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,ColorX );
		PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,ColorX );
		PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,ColorX );

	//---------- Viet cac diem X, A, B, C, D: by binhnd---------------------
		xlech		=	-1;
		ylech 		= 	1;
		PlotText("X",LastValue(Xbar)	+ 	xlech,	LastValue(X)	+	ylech,	ColorX );
		PlotText("A",LastValue(Abar)	+ 	xlech,	LastValue(A)	-	ylech,	ColorX );
		PlotText("B",LastValue(Bbar)	+ 	xlech,	LastValue(B)	+	ylech,	ColorX );
		PlotText("C",LastValue(C1bar)	+ 	xlech,	LastValue(C1)	-	ylech,	ColorX );
		PlotText("D",LastValue(Dbar)	+ 	xlech,	LastValue(D)	+	ylech,	ColorX );

	//--------- Viet thuyet minh mo hinh: by binhnd--------------
		if (strPattern!="") 
			{
				strPattern 	= 	"Pattern: " + strPattern;
				toadoaX		=	(LastValue(Dbar)+LastValue(Xbar))/2;
				toadoY			=	(LastValue(D)+LastValue(X))/2;

				PlotText(strPattern,toadoaX,toadoY+1,ColorX );
			}
	
}			// end of VE DUONG CHO MO HINH BEARISH BAT, GARTLEY, BUTTERFLY, CRAB




//=================================
// Show diem ho^~ tro. va` khang' cu. ko?
//=================================




//==============================================
// DAT DIEU KIEN cho TIM KIEM BULL
//==============================================
dkBull = False;
ListBull 		= 	ParamList("Type of Bullish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 6);
	if 	(	ListBull == "None"		)		dkBull = 	True;
	if (	ListBull =="AB=CD"		) 		dkBull	=	BullABCD ;
	if (	ListBull =="Gartley"		) 		dkBull	=	BullGartley ;
	if (	ListBull =="Butterfly"	) 		dkBull	=	BullButterfly ;
	if (	ListBull =="Bat"			) 		dkBull	=	BullBat ;
	if (	ListBull =="Crab"			) 		dkBull	=	BullCrab ;
	if (	ListBull =="All Patterns") 		dkBull	=	(BullABCD) OR (BullGartley) OR (BullButterfly ) OR (BullBat ) OR (BullCrab);

//==============================================
// DAT DIEU KIEN cho TIM KIEM BEAR
//==============================================
dkBear = False;
ListBear 		= 	ParamList("Type of Bearish", "None|AB=CD|Gartley|Butterfly|Bat|Crab|All Patterns", 6);
	if 	(	ListBear == "None"		)		dkBear = 	True;
	if (	ListBear =="AB=CD"		) 		dkBear	=	BearABCD ;
	if (	ListBear =="Gartley"		) 		dkBear	=	BearGartley ;
	if (	ListBear =="Butterfly"	) 		dkBear	=	BearButterfly ;
	if (	ListBear =="Bat"			) 		dkBear	=	BearBat ;
	if (	ListBear =="Crab"			) 		dkBear =	BearCrab ;
	if (	ListBear =="All Patterns") 		dkBear =	(BearABCD ) OR (BearGartley ) OR (BearButterfly ) OR (BearBat ) OR (BearCrab );
//===============================

AddColumn(V,"Volume",1.0);
Filter = (dkBull) AND (dkBear);

plotFractals = ParamToggle("Plot Fractals","Off|On",1);
if(PlotFractals)
{
	PlotShapes(shapeSmallCircle*P1,colorRed,0,H,10);
	PlotShapes(shapeSmallCircle*V1,colorBlue,O,L,-10);
}

Filter = BullHar OR BearHar;
Bull = BullHar; Bear = BearHar;
Color = IIf(Bull,colorGreen,colorRed);
AddColumn(IIf(Bull,1,IIf(Bear,-1,Null)),"Pattern",1.0,colorRed,Color);


Buy=Bull;


//topi

LatPattern = bullHar OR bearHar;
Lastpattern = LastValue(ValueWhen(LatPattern,IIf(BullGartley,1,IIf(BearGartley,-1,IIf(BullBat,2,IIf(BearBat,-2,
				IIf(BullButterfly,3,IIf(BearButterfly,-3,IIf(BullCrab,4,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"))))))));


plotFractals = ParamToggle("Plot Fractals","Off|On",0);
if(PlotFractals)
{
	PlotShapes(shapeSmallCircle*P1,colorRed,0,H,10);
	PlotShapes(shapeSmallCircle*V1,colorBlue,O,L,-10);
}

GfxSetTextColor(IIf(LastPattern > 0, colorGreen,colorRed));

//////////////////DELETE



// Bearish Patterns

PTvalid = (V1Bar1 > P1Bar1 AND P1Bar1 > V1Bar2 AND V1Bar2 > P1Bar2) AND V1;

// Swing 4

BearGartley4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > GBmin AND (P1H1-V1L2)/(P1H2-V1L2) < GBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > GCmin AND (P1H1-V1L1)/(P1H1-V1L2) < GCmax;



BearShark4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > SBmin AND (P1H1-V1L2)/(P1H2-V1L2) < SBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > SCmin AND (P1H1-V1L1)/(P1H1-V1L2) < SCmax;



BearBat4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > BatBmin AND (P1H1-V1L2)/(P1H2-V1L2) < BatBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > BatCmin AND (P1H1-V1L1)/(P1H1-V1L2) < BatCmax;

BearButterfly4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > BtBmin AND (P1H1-V1L2)/(P1H2-V1L2) < BtBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > BtCmin AND (P1H1-V1L1)/(P1H1-V1L2) < BtCmax;

BearCrab4 = PTvalid AND (P1H1-V1L2)/(P1H2-V1L2) > CBmin AND (P1H1-V1L2)/(P1H2-V1L2) < CBmax AND
				(P1H1-V1L1)/(P1H1-V1L2) > CCmin AND (P1H1-V1L1)/(P1H1-V1L2) < CCmax;

// Poin D

BearGartley = IIf(HighestSince(bearGartley4,H) > ValueWhen(BearGartley4,V1L2) + (ValueWhen(BearGartley4,P1H2) - ValueWhen(BearGartley4,V1L2))*GDmin AND
				 HighestSince(bearGartley4,H) < ValueWhen(BearGartley4,V1L2) + (ValueWhen(BearGartley4,P1H2) - ValueWhen(BearGartley4,V1L2))*GDMax AND	
				LowestSince(BearGartley4,L) >= ValueWhen(BearGartley4,V1L1  ) AND
				HighestSince(BearGartley4,H) == H, True, False);
BearGartley = BearGartley AND HighestSince(BearGartley4,H) > ValueWhen(BearGartley4,P1H1);



BearShark = IIf(HighestSince(bearShark4,H) > ValueWhen(BearShark4,V1L2) + (ValueWhen(BearShark4,P1H2) - ValueWhen(BearShark4,V1L2))*SDmin AND
				 HighestSince(bearShark4,H) < ValueWhen(BearShark4,V1L2) + (ValueWhen(BearShark4,P1H2) - ValueWhen(BearShark4,V1L2))*SDMax AND	
				LowestSince(BearShark4,L) >= ValueWhen(BearShark4,V1L1  ) AND
				HighestSince(BearShark4,H) == H, True, False);
BearShark = BearShark AND HighestSince(BearShark4,H) > ValueWhen(BearShark4,P1H1);



BearBat = IIf((HighestSince(BearBat4,H) > ValueWhen(BearBat4,V1L2) + (ValueWhen(BearBat4,P1H2) - ValueWhen(BearBat4,V1L2))*BatDmin AND
				 HighestSince(BearBat4,H) < ValueWhen(BearBat4,V1L2) + (ValueWhen(BearBat4,P1H2) - ValueWhen(BearBat4,V1L2))*BatDMax AND	
				LowestSince(BearBat4,L) >= ValueWhen(BearBat4,V1L1 ) AND
				HighestSince(BearBat4,H) == H), True, False);
BearBat = BearBat AND HighestSince(BearBat4,H) > ValueWhen(BearBat4,P1H1);

BearButterfly = IIf(HighestSince(BearButterfly4,H) > ValueWhen(BearButterfly4,V1L2) + (ValueWhen(BearButterfly4,P1H2) - ValueWhen(BearButterfly4,V1L2))*BtDmin AND
				 HighestSince(BearButterfly4,H) < ValueWhen(BearButterfly4,V1L2) + (ValueWhen(BearButterfly4,P1H2) - ValueWhen(BearButterfly4,V1L2))*BtDMax AND	
				LowestSince(BearButterfly4,L) >= ValueWhen(BearButterfly4,V1L1 ) AND
				HighestSince(BearButterfly4,H) == H, True, False);
BearButterfly = BearButterfly AND HighestSince(BearButterfly4,H) > ValueWhen(BearButterfly4,P1H2);

BearCrab = IIf(HighestSince(BearCrab4,H) > ValueWhen(BearCrab4,V1L2) + (ValueWhen(BearCrab4,P1H2) - ValueWhen(BearCrab4,V1L2))*CDmin AND
				 HighestSince(BearCrab4,H) < ValueWhen(BearCrab4,V1L2) + (ValueWhen(BearCrab4,P1H2) - ValueWhen(BearCrab4,V1L2))*CDMax AND	
				LowestSince(BearCrab4,L) >= ValueWhen(BearCrab4,V1L1 ) AND
				HighestSince(BearCrab4,H) == H, True, False);
BearCrab =BearCrab AND HighestSince(BearCrab4,H) > ValueWhen(BearCrab4,P1H2);


BearHar4 = BearGartley4 OR BearButterfly4 OR BearBat4 OR BearCrab4 OR BearShark4 ;
BearHar = BearGartley OR BearButterfly OR BearBat OR BearCrab OR BearShark;

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);


//////////////////dhamaka

BearBat4 			= PTvalid 	AND 	(	myAB_AX > BatBmin ) 		AND (	myAB_AX < BatBmax	)
								AND 	(	myBC_AB > BatCMin ) 		AND (	myBC_AB < BatCMax	); 

//=============================================================
//				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	=		myA 	+ 	my_d_min;					// Khoang dich chuyen cua duong Ad con.
	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
strPattern 	=		WriteIf(BearBat ,"BEARISH BAT",strPattern);


//////////////////////////////////



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;

if(LastValue(Plotpattern) AND be)

{
Plot( LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",colorRed,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(Bbar),LastValue(B)),"",colorRed,styleThick);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(C1bar),LastValue(C1)),"",colorRed,styleThick);
Plot(LineArray(LastValue(C1bar),LastValue(C1),LastValue(Dbar),LastValue(D)),"",colorRed,styleThick);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Bbar),LastValue(B)),"",colorRed,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Abar),LastValue(A)),"",colorRed,styleThick);
Plot(LineArray(LastValue(Abar),LastValue(A),LastValue(C1bar),LastValue(C1)),"",colorRed,styleDashed);
Plot(LineArray(LastValue(Bbar),LastValue(B),LastValue(Dbar),LastValue(D)),"",colorRed,styleDashed);
Plot(LineArray(LastValue(Xbar),LastValue(X),LastValue(Dbar),LastValue(D)),"",colorRed,styleDashed);

PlotText(NumToStr(LastValue(ABdXA),1.2),(LastValue(Bbar)+LastValue(Xbar))/2,(LastValue(B)+LastValue(X))/2,colorRed);
PlotText(NumToStr(LastValue(BCdAB),1.2),(LastValue(C1bar)+LastValue(Abar))/2,(LastValue(C1)+LastValue(A))/2,colorRed);
PlotText(NumToStr(LastValue(BCdCD),1.2) ,(LastValue(Dbar)+LastValue(Bbar))/2,(LastValue(D)+LastValue(B))/2,colorRed);
PlotText(NumToStr(LastValue(ADdXA),1.2) ,(LastValue(Dbar)+LastValue(Xbar))/2,(LastValue(D)+LastValue(X))/2,colorRed);

}



//Previous Days HI LO //

DayH = TimeFrameGetPrice("H", inDaily, -1);	 DayHI = LastValue (DayH,1);// yesterdays high
DayL = TimeFrameGetPrice("L", inDaily, -1);	 DayLI = LastValue (DayL,1);	// yesterdays low
DayC = TimeFrameGetPrice("C", inDaily, -1);	 	// yesterdays close
DayO = TimeFrameGetPrice("O", inDaily);			// current day open
WeekH= TimeFrameGetPrice("H", inWeekly, 1);  WeekHI = LastValue (WeekH,1); // One Week before high
WeekL= TimeFrameGetPrice("L", inWeekly, 1);  WeekLI = LastValue (WeekL,1);  // One Week before low
MonthH= TimeFrameGetPrice("H", inMonthly, 1);  MonthHI = LastValue (MonthH,1);  // One Month before high
MonthL= TimeFrameGetPrice("L", inMonthly, 1);  MonthLI = LastValue (MonthL,1);  // One Month before low

numbars = LastValue(Cum(Status("barvisible")));
hts  = -33.5;


TDBHL = ParamToggle("Week And Month HI LO","Hide|Show",0);
if(TDBHL==1) {
  Plot(WeekL,"WeekL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(WeekH,"WeekH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(MonthL,"MonthL",colorTurquoise,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  Plot(MonthH,"MonthH",colorOrange,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
  PlotText(" WeekH " , 	LastValue(BarIndex())-(numbars/Hts), WeekHI, colorOrange);
  PlotText(" WeekL " , 	LastValue(BarIndex())-(numbars/Hts), WeekLI, colorTurquoise);
  PlotText(" MonthH " , 	LastValue(BarIndex())-(numbars/Hts), MonthHI, colorOrange);
  PlotText(" MonthL " , 	LastValue(BarIndex())-(numbars/Hts), MonthLI, colorTurquoise);
}

//  Levels //
PP = (DayL + DayH + DayC)/3;  PPI = LastValue (PP,1);   // Pivot
R1  =  (PP * 2) - DayL;       R1I = LastValue (R1,1);   // Resistance 1
S1  =  (PP * 2)  - DayH;      S1I = LastValue (S1,1);   // Support 1
R2  =  PP + R1 - S1;          R2I = LastValue (R2,1);   // Resistance 2
S2  =  PP - R1 + S1;          S2I = LastValue (S2,1);   // Support 2
R3  =  PP + R2 - S1;          R3I = LastValue (R3,1);   // Resistance 3
S3  =  PP - R2 + S1;          S3I = LastValue (S3,1);   // Support 3

// pivot
ppl = ParamToggle("Pivot Levels","Hide|Show",1);
if(ppl==1)  {
//Plot(PP, "PP",colorBrown,styleStaircase|styleNoRescale);
//  Plot(R1, "R1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
 // Plot(S1, "S1",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//  Plot(R2, "R2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//  Plot(S2, "S2",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//  Plot(R3, "R3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);
//  Plot(S3, "S3",colorViolet,styleDots|styleNoLine|styleNoRescale|styleNoTitle);

  PlotText(" Pivot ",  LastValue(BarIndex())-(numbars/Hts), PPI, colorYellow);
  PlotText(" R1 " ,    LastValue(BarIndex())-(numbars/Hts), R1I, colorViolet);
  PlotText(" S1 " ,    LastValue(BarIndex())-(numbars/Hts), S1I, colorViolet);
  PlotText(" R2 " ,    LastValue(BarIndex())-(numbars/Hts), R2I, colorViolet);
  PlotText(" S2 " ,    LastValue(BarIndex())-(numbars/Hts), S2I, colorViolet);
  PlotText(" R3 " ,    LastValue(BarIndex())-(numbars/Hts), R3I, colorViolet);
  PlotText(" S3 " ,    LastValue(BarIndex())-(numbars/Hts), S3I, colorViolet);
}



// Current Days Hi Lo //
THL = ParamToggle("Todays Hi Lo","Hide|Show",0);
if(THL==1)  {
  isRth = TimeNum() >= 084500 & TimeNum() <= 085959;
  isdRth = TimeNum() >= 084500 & TimeNum() <= 160000;
  aRthL = IIf(isRth, L, 1000000);
  aRthH = IIf(isdRth, H, Null);
  aRthLd = IIf(isdRth, L, 1000000);
  DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
  DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
  DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
  DayL = TimeFrameExpand( DayL, inDaily, expandFirst );
  Bars = BarsSince(TimeNum() >= 94500 AND TimeNum() < 095959);//,BarIndex(),1); // AND DateNum()==LastValue(DateNum());
  x0 = BarCount-LastValue(Bars);
  x1 = BarCount-1;
  DayHline=LineArray(x0,LastValue(DayH),x1,LastValue (DayH),0); 
  DayLline=LineArray(x0,LastValue(DayL),x1,LastValue (DayL),0); 
  DayHlineI = LastValue (DayHline,1);
  DayLlineI = LastValue (DayLline,1);
  Plot(DayHline,"DayH",colorGold,styleDashed|styleNoRescale);
  Plot(DayLline,"DayL",colorBlue,styleDashed|styleNoRescale|styleNoTitle);
  PlotText(" Day Hi " , 	LastValue(BarIndex())-(numbars/Hts), DayHlineI  +0.05, colorYellow);
  PlotText(" Day Lo " , 	LastValue(BarIndex())-(numbars/Hts), DayLlineI  +0.05, colorYellow);
 }
 


_SECTION_BEGIN("TREND");
SetBarsRequired(100000,0);
GraphXSpace = 15;

ea = EMA (C,10);
eb = EMA (C,20);
SetBarFillColor( IIf( ea > eb, colorGreen, colorRed ) ); 

Buy = ea > eb AND TimeNum() > 092000 AND TimeNum() < 150000;
Sell = eb > ea OR TimeNum() > 150000;
Short = 0;
Cover = 0;
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);


SetTradeDelays(1,1,1,1);


Factor=Param("Factor",6,1,10,1);
Pd=Param("ATR Periods",7,1,100,1);
Up=(H+L)/2+(Factor*ATR(Pd));
Dn=(H+L)/2-(Factor*ATR(Pd));
iATR=ATR(Pd);
TrendUp=TrendDown=Null;
trend[0]=1;
changeOfTrend=0;
flag=flagh=0;

for (i = 1; i <BarCount; i++) {
TrendUp[i] = Null;
TrendDown[i] = Null;

trend[i]=1;


if (Close[i]>Up[i-1]) {
trend[i]=1;
if (trend[i-1] == -1) changeOfTrend = 1;

}
else if (Close[i]<Dn[i-1]) {
trend[i]=-1;
if (trend[i-1] == 1) changeOfTrend = 1;
}
else if (trend[i-1]==1) {
trend[i]=1;
changeOfTrend = 0; 
}
else if (trend[i-1]==-1) {
trend[i]=-1;
changeOfTrend = 0;
}

if (trend[i]<0 && trend[i-1]>0) {
flag=1;
}
else {
flag=0;
}

if (trend[i]>0 && trend[i-1]<0) {
flagh=1;
}
else {
flagh=0;
}

if (trend[i]>0 && Dn[i]<Dn[i-1]){
Dn[i]=Dn[i-1];
}

if (trend[i]<0 && Up[i]>Up[i-1])
{ Up[i]=Up[i-1];
}

if (flag==1)
{ Up[i]=(H[i]+L[i])/2+(Factor*iATR[i]);;
} 
if (flagh==1)
{ Dn[i]=(H[i]+L[i])/2-(Factor*iATR[i]);;
}
if (trend[i]==1) {
TrendUp[i]=Dn[i];
if (changeOfTrend == 1) {
TrendUp[i-1] = TrendDown[i-1];
changeOfTrend = 0;
}
}
else if (trend[i]==-1) {
TrendDown[i]=Up[i];
if (changeOfTrend == 1) {
TrendDown[i-1] = TrendUp[i-1];
changeOfTrend = 0;
}
}
} 
//paplu
Plot(TrendUp,"Trend",colorlime ,styleLine |styleNoRescale | styleNoLabel );
Plot(TrendDown,"Down",colorRed,styleLine |styleNoRescale | styleNoLabel);

Buy = trend==1;
Sell=trend==-1;

Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=Sell;
Cover=Buy;


BuyPrice=ValueWhen(Buy,C);
SellPrice=ValueWhen(Sell,C);
ShortPrice=ValueWhen(Short,C);
CoverPrice=ValueWhen(Cover,C);



PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorGreen, 0, L, Offset=-40);
PlotShapes(IIf(Buy, shapeSquare, shapeNone),colorLime, 0,L, Offset=-50); 
PlotShapes(IIf(Buy, shapeUpArrow, shapeNone),colorWhite, 0,L, Offset=-45); 
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorRed, 0, H, Offset=40);
PlotShapes(IIf(Short, shapeSquare, shapeNone),colorOrange, 0,H, Offset=50); 
PlotShapes(IIf(Short, shapeDownArrow, shapeNone),colorWhite, 0,H, Offset=-45);

TrendSL=IIf(trend==1,TrendUp,TrendDown);

for(i=BarCount-1;i>1;i--)
{
if(Buy[i] == 1)
{
entry = C[i];
sig = "BUY";
sl = TrendSL[i];
tar1 = entry + (entry * .0050);
tar2 = entry + (entry * .0092);
tar3 = entry + (entry * .0179);

bars = i;
i = 0;
}
if(Sell[i] == 1)
{
sig = "SELL";
entry = C[i];
sl = TrendSL[i];
tar1 = entry - (entry * .0050);
tar2 = entry - (entry * .0112);
tar3 = entry - (entry * .0212);


bars = i;
i = 0;
}
}
Offset = 20;
sig = "BUY";
Clr = IIf(sig == "BUY", colorLime, colorRed);
ssl = IIf(bars == BarCount-1, TrendSL[BarCount-1], Ref(TrendSL, -1));
sl = ssl[BarCount-1];
_SECTION_END();




// TO ADD//










_SECTION_BEGIN("Title");


TimeFrameSet(inDaily);
TOP_ = Open;
PDH_ = Ref(High,-1);
PDL_ = Ref(Low,-1);
PDO_ = Ref(Open,-1);
PDC_ = Ref(Close,-1);
PDM_ = (PDH_+PDL_)/2;
TimeFrameRestore();

isAll = True;
isRth = TimeNum() >= 095500 AND TimeNum() <= 103000;
isdRth = TimeNum() >= 095500 AND TimeNum() <= 235900;

aRthL = IIf(isRth, L, 1000000);
aRthH = IIf(isdRth, H, Null);
aRthLd = IIf(isdRth, L, 1000000);

TOP = TimeFrameExpand(TOP_,inDaily,expandFirst);
PDH = TimeFrameExpand(PDH_,inDaily,expandFirst);
PDL = TimeFrameExpand(PDL_,inDaily,expandFirst);
PDO = TimeFrameExpand(PDO_,inDaily,expandFirst);
PDC = TimeFrameExpand(PDC_,inDaily,expandFirst);
PDM = TimeFrameExpand(PDM_,inDaily,expandFirst);

FHL = TimeFrameCompress( aRthL, inDaily, compressLow );
FHL = TimeFrameExpand( FHL, inDaily, expandFirst );
DayH = TimeFrameCompress( aRthH, inDaily, compressHigh );
DayH = TimeFrameExpand( DayH, inDaily, expandFirst );
DayL = TimeFrameCompress( aRthLd, inDaily, compressLow );
DayL = TimeFrameExpand( DayL, inDaily, expandFirst );



DDayO = TimeFrameGetPrice("O", inDaily);
DHiDay = TimeFrameGetPrice("H", inDaily);
DLoDay = TimeFrameGetPrice("L", inDaily);
gfr = TimeFrameGetPrice("C", inDaily, -1);//close



Title = EncodeColor(colorCustom14)+" * Livermore x * "+ Interval(2) +EncodeColor(ColorRGB(200,180,250)) + Date() +
EncodeColor(colorLime) +
" *                     Prevvious Close : " + EncodeColor(colorBrightGreen) + gfr +EncodeColor(colorCustom12)+
"              ToDay Open : " +DDayO + EncodeColor(colorRed)+ "            Previous DayLow=   "+WriteVal(PDL,1)+ ",\n                           "+ 
EncodeColor(colorTurquoise) +"Previous DayHigh="+WriteVal(PDH,1)+", Today High=  "+ EncodeColor(coloryellow)+ DHiDay+ 
"  , ||  Todays Low= "+DLoDay;


_SECTION_END();


_SECTION_BEGIN("ema");
Lk = EMA(Close,22);




GfxSetOverlayMode(0); 
GfxSelectFont("Tahoma", Status("pxheight")/36); 
GfxSetTextAlign( 6 );// center alignment 
GfxSetTextColor( colorLightGrey ); 
GfxSetBkMode(0); // transparent 
GfxTextOut( Name(), Status("pxwidth")/2, Status("pxheight")/15 );

cx=Param("cxposn",105,0,500,1);
cy=Param("cyposn",49,0,500,1);
GfxSetBkColor(ColorRGB(200,50,100));
GfxSelectFont( "tohomabold",24,30, False);
GfxSetTextColor( colorYellow);
GfxSetTextColor( ColorHSB( 100, 10, 400) );
GfxTextOut("LTP. "+C+" ", cx, cy );



Plot(Close,"Close",colorWhite,styleCandle);
Plot(MA(Close,200), "Shifted MA", colorRed, styleThick| styleNoRescale,Null, Null, 10 ); 

EMA12=EMA(C,12);
Plot(EMA12,"EMA12", colorWhite,styleThick|styleNoRescale | styleNoLabel);

    Plot(MA(C, 55), "55 bar MA", colorTurquoise, styleThick+styleNoRescale);
_SECTION_END();

 Dayo = TimeFrameGetPrice("O", inDaily);			// current day open

   Plot( DayO, "DO", colorCustom12,  styleThick | styleSwingDots | styleNoRescale  );
  _SECTION_END();
  





_SECTION_BEGIN("short signal");
HaClose=(O+H+L+C)/4;
HaOpen = AMA( Ref( HaClose, -1 ), 0.5 );
HaHigh = Max( H, Max( HaClose, HaOpen ) );
HaLow = Min( L, Min( HaClose, HaOpen ) );
BG2=HHV(LLV(Low,4)+ATR(4),8);
BR2=LLV(HHV(High,4)-ATR(4),8);
SetBarFillColor( IIf(O <C, colorSeaGreen,colorOrange) );
k = Optimize("K",Param("K",2.75,1,5,0.25),1,5,0.25);

Per= Optimize("atr",Param("atr",30,3,30,1),3,30,1);
j=HaClose;
nm= (H-L);
rfsctor = WMA(nm, PER);
revers = K * rfsctor;
Trend = 1;
NW[0] = 0;
for(i = 1; i < BarCount; i++)
{
if(Trend[i-1] == 1)
{
if(j[i] < NW[i-1])
{
Trend[i] = -1;
NW[i] = j[i] + Revers[i];
}
else
{
Trend[i] = 1;
if((j[i] - Revers[i]) > NW[i-1])
{
NW[i] = j[i] - Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
if(Trend[i-1] == -1)
{
if(j[i] > NW[i-1])
{
Trend[i] = 1;
NW[i] = j[i] - Revers[i];
}
else
{
Trend[i] = -1;
if((j[i] + Revers[i]) < NW[i-1])
{
NW[i] = j[i] + Revers[i];
}
else
{
NW[i] = NW[i-1];
}
}
}
}

Plot(NW, "", IIf(Trend == 1, 6, 4), 4 |styleNoRescale  | styleNoLabel );

Buy=Cross(j,nw);
Short=Cross(nw,j);
Sell=Cross(nw,j);
Cover=Cross(j,nw);
Buy=ExRem(Buy,Sell);
Sell=ExRem(Sell,Buy);
Short=ExRem(Short,Cover);
Cover=ExRem(Cover,Short);
//dist = 1.5*ATR(15);
for( i = 0; i < BarCount; i++ )
{
if( Buy[i] ) {};
if( Sell[i] ) {};

if( Buy[i] ) PlotText( "" + O[ i ], i, L[ i ]-Trend[i], colorAqua, colorBlue );
if( Sell[i] ) PlotText(    " " +H[ i ], i-4, L[ i ]+Trend[i], colorBlack, colorOrange );
}

_SECTION_BEGIN("BBands");
P = ParamField("Price field",3);
Periods = Param("Periods", 15, 2, 100, 1 );
Width = Param("Width", 2, 0, 10, 0.05 );
Color = ParamColor("Color", colorLightGrey );
Color = ColorBlend( Color,  GetChartBkColor(), 0.5 );
Style = ParamStyle("Style", styleLine | styleNoLabel ) | styleNoRescale;;
Plot( bbt = BBandTop( P, Periods, Width ), "BBTop" + _PARAM_VALUES(), Color, Style ); 
Plot( bbb = BBandBot( P, Periods, Width ), "BBBot" + _PARAM_VALUES(), Color, Style ); 
_SECTION_END();


showsl = ParamToggle("Stop Loss Line", "Show|Hide", 0);
no=10;
res=HHV(H,no);
sup=LLV(L,no);
avd=IIf(C>Ref(res,-1),1,IIf(C<Ref(sup,-1),-1,0));
avn=ValueWhen(avd!=0,avd,1);
s5d=IIf(avn==1,sup,res);
 
if (showsl == 0)
{Plot(s5d,"Stop Loss",colorSkyblue,styleDots| styleNoRescale);}

4 comments

1. vinasdaq

Oh! Very Good System!
Good Job!
Thank you very much.

2. NIRAV

GREAT JOB DONE HERE
BUT WILL U PLEASE DESCRIBE THE IMPORTANCE OF THE A B C D AND THE LINES JOINING IT..
IS THERE ANY MANUAL AVAILABLE TO USE IT VERY SMOOTHLY..

HOPE FOR BEST REPLY..

3. Sadik Shaik

hi creator of this AFL, looking it was great useful but

after seeing this afl on amibroker i got confused where to look ?

how to use it bro. please comment.

i have to follow only buy sell arrows ?

can i ignore blue red dots below/above candles ?

what about other lines ?

please explain little bit. as newbie i can understand how to use this AFL.

anyone from this site also can help me.

4. Aviator1123

Great work ..But better to keep away Pivot bb ema and trend from this. Actually will be helpfull if exploration is added to this.
But thanks for ur contribution.

Leave Comment

Please login here to leave a comment.

Back