[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: Cup & Handle doesn't work



PureBytes Links

Trading Reference Links


I modified the script as follows. If you look at the new script, you 
may know why the original one may have trouble finding matches.

I am using it with hourly time frame. You may need to modify it for 
daily quotes.

Charles

/*
** cupAndHandle.afl
** Cup with handle explorer (work on daily and hourly time frame)
**		Searches for cup up to 50 bar (10 weeks) long, 
handle up to 15 bar (3 weeks) long
*/

MinPrice	= 10;

dailyChk	= Param("isDaily", 1, 0, 1, 1);

if(Interval() <= inHourly) {
	MinVolume		= 20000;		/* used for 
average volume in 50 bars */

	if(dailyChk) {
		TimeFrameSet(inDaily);
	}

	MinUpTrend		= 0.08;
	relAtr			= 100 * ATR(20)/MA(C, 20);
	MaxDnHndl		= 2 * relAtr;
}
else {
	MinVolume		= 100000;		/* used for 
average volume in 50 bars */
	MinUpTrend		= 0.3;
	MaxDnHndl		= 15;
	relAtr			= 100 * ATR(20)/MA(C, 20);
}

MXHdlBar	= 5;		/* 15, max length of handle */
MXBefCupBar	= 265;
MinHdlBar	= 1;

Pr0		= C;
Pr1		= H;
Pr2		= L;

MXBotCupBar	= 15;		/* 60, max length of bottom of cup 
in bar numbers */
do {
MXLtCupBar	= MXBotCupBar;		/* 30, max length of left 
high of cup in bars */

/*
** Left side of Handle formation can occur anywhere from 2-15 days 
** 		look at the last 15 bars AND get the number of bars 
since condition met.
*/

LtHdl 		= HHV(Pr1, MXHdlBar); 		// Highest close 
past 15 days.
BarLtHdl	= HHVBars(Pr1, MXHdlBar); 	// Tells us # of 
bars that have past since High reached. Used to determine Lowest bar.

/* bottom of handle */
BotHdl		= LLV(Pr2, BarLtHdl); 	// Lowest close since the 
highest value was reached.
BarBotHdl	= LLVBars(Pr2, BarLtHdl);	// number of bars 
that have past since Lowest value.

/*
** this is the number of bars in the formation of the left side 
handle:
**		from left top to left bottome in the left handle
*/
NumBarLtHdl	= BarLtHdl - BarBotHdl; 	/* NumBarLtHdl must 
be atleast 2 to be a valid handle formation */ 

/* 
** Handle requirements:
**		bottom of handle should be in 15% of left of handle 
(high)
**		last Close should be within 5% of Highest High in 
handle (Close < 1.05 * HHV(H, BarLtHdl))
**			This is guaranteed since hhvbars = 0 if hhv 
is today and hhvbars = 0 is not allowed in handle
**		handle is above 50 MA 
**		handle must be downtrending
*/
handle = NumBarLtHdl >= MinHdlBar AND Close > BotHdl AND BotHdl < 
LtHdl AND Close > MA(Close, 50);
Handle = Handle AND 100 * (LtHdl - BotHdl)/LtHdl < MaxDnHndl AND 
BarBotHdl <= (BarLtHdl/2);

/* 
** Now lets get the cup formation. 
** Cup formation can occur anywhere from 23 to 145 days. 
** The left side of the cup can be from 20-120 days AND the right 
side can be anywhere from 3-25 days.
*/

// get the right side of the cup (low).

BotCup		= LLV(Pr2, BarLtHdl + MXBotCupBar); // look at 25 
bars since the left side of handle.
BarBotCup	= LLVBars(Pr2, BarLtHdl + MXBotCupBar);
RtCup		= Ref(HHVBars(Pr1, BarBotCup), -BarLtHdl);

// get the left side of the cup (high).
MXLtCupBar	= MXBotCupBar - BarBotCup;			
			// must be longer than 2 x handle
BarBotCH	= BarBotCup;					
					// bars to bottom of cup + 
handle length
LtCup		= Ref(HHV(Pr1, MXLtCupBar), -BarBotCH);		// 
up to 50 bars from bottom of cup
BarLtCup	= Ref(HHVBars(Pr1, MXLtCupBar), -BarBotCH);

/*
** Additional handle requirements with regard to cup
** 		If high handle, length must be > 1 week.
**		high handle must be within 10% of left cup high
*/
MinHdlBars = IIf(LtHdl > LtCup * 1.02, 5, 0);

Handle = Handle AND Pr0 < LtCup * 1.15 AND BarLtHdl >= MinHdlBars;

/*
** Cup requirements
**		left side of cup should be at least 35 bars (7 
weeks) long
** 		For low handles
**			Bottom of handle must be higher than 23.6% 
of right side cup FIB retracement
**			Bottom of handle must be higher than 38% of 
left side cup FIB retracement
**		Bottom of cup should be in 38% of left side cup high
**		For high handles:
**			handle can only be 15% higher than left cup 
at most
**			handle must be less than 30% of price of 
left side cup - bottom  cup price
**			make sure there is no embedded handle 
between cup low and the high handle
**		Left cup low must be higher than bottom of cup
**		In the right side of the cup, there should be no 
point with close higher than left side handle
**		The interval between left cup high and cup bottom 
must be at least 2 x handle long
**	
**		There should be at least a 30% increase to left side 
of cup in one year
*/
hdlRetr	= BotHdl > (LtHdl - (LtHdl - BotCup) * 0.382) AND BotHdl > 
(LtCup - (LtCup - BotCup) * 0.382);

cup			= (BarLtCup + BarBotCup) > 30 AND hdlRetr 
AND BotCup > (1 - 0.38) * LtCup;
Cup			= Cup AND	LtHdl < (1 + MaxDnHndl/100) 
* LtCup AND Close < (LtCup + 0.3 * (LtCup - BotCup));
Cup			= Cup AND	RtCup <= LtHdl AND BarLtCup 
>= 2 * BarLtHdl;

// Get lowes value before left side of cup started to form.

BotBefCup	= Ref(LLV(Pr2, MXBefCupBar), -BarLtCup-BarBotCup);
		// up to 120 bar from left high of cup
BarBotBefC	= Ref(LLVBars(Pr2, MXBefCupBar), -BarLtCup-
BarBotCup);	// up to 120 bar from left high of cup

upBefCup	= (LtCup - BotBefCup)/BotBefCup > MinUpTrend AND 
BarBotBefC > 1;
cup			= cup AND upBefCup;

/* Left Cup (high) must be at least 5% of before cup high */
TopBefCup = Ref(HHV(Pr1, MXBefCupBar), -BarLtCup-BarBotCup);
Cup			= Cup AND (100 * (TopBefCup - 
LtCup)/TopBefCup <= 5);

cupHandle	= handle AND cup;
MXBotCupBar = MXBotCupBar + 5;
MXHdlBar	= MXHdlBar + 2;

/*
** Search Cup & Handle of duration from 2 * (3 to 12) weeks with 1 
week increment
*/
CupLenLooked= MXBotCupBar * 2/5;
done		= LastValue(cupHandle OR (MXBotCupBar > 60));
//done		= 1;
} while (NOT done);

if(Interval() <= inHourly) {
	if(dailyChk) {
		TimeFrameRestore();

		cup		= TimeFrameExpand(cup, inDaily, 
expandFirst);
		handle	= TimeFrameExpand(handle, inDaily, 
expandFirst);

		LtHdl	= TimeFrameExpand(LtHdl, inDaily, 
expandFirst);
		BotHdl	= TimeFrameExpand(BotHdl, inDaily, 
expandFirst);
		BotCup	= TimeFrameExpand(BotCup, inDaily, 
expandFirst);
		LtCup	= TimeFrameExpand(LtCup, inDaily, 
expandFirst);
		BotBefCup	= TimeFrameExpand(BotBefCup, 
inDaily, expandFirst);
		CupLenLooked= TimeFrameExpand(CupLenLooked, inDaily, 
expandFirst);

		// for debug
		BarLtHdl	= TimeFrameExpand(BarLtHdl, inDaily, 
expandFirst);
		BarBotHdl	= TimeFrameExpand(BarBotHdl, 
inDaily, expandFirst);
		BarBotCup	= TimeFrameExpand(BarBotCup, 
inDaily, expandFirst);
		BarLtCup	= TimeFrameExpand(BarLtCup, inDaily, 
expandFirst);
		BarBotBefC	= TimeFrameExpand(BarBotBefC, 
inDaily, expandFirst);
		BarBotCH	= TimeFrameExpand(BarBotCH, inDaily, 
expandFirst);
		MXLtCupBar	= TimeFrameExpand(MXLtCupBar, 
inDaily, expandFirst);
		upBefCup	= TimeFrameExpand(upBefCup, inDaily, 
expandFirst);
		hdlRetr	= TimeFrameExpand(hdlRetr, inDaily, 
expandFirst);
	}
}

SetOption("nodefaultcolumns", True);
AddTextColumn(Name(),"Ticker");
AddTextColumn(IndustryID(1),"Industry");
AddColumn(CupLenLooked,"CupLenLooked");

AddColumn(LtHdl,"Left Handle");
AddColumn(BotHdl,"Bottom Handle");
AddColumn(BotCup,"Bottom Cup");
AddColumn(LtCup,"Left Cup");
AddColumn(BotBefCup,"BotBeforeCup");

// debug purpose
AddColumn(BarLtHdl, "BarLtHdl");
AddColumn(BarBotHdl, "BarBotHdl");
AddColumn(BarBotCup, "BarBotCup");
AddColumn(BarLtCup, "BarLtCup");
AddColumn(BarBotBefC, "BarBotBefC");

AddColumn(BarBotCH, "BarBotCH");
AddColumn(MXLtCupBar, "MXLtCupBar");

AddColumn(Handle, "handle");
AddColumn(Cup, "cup");
AddColumn(upBefCup, "upBefCup");
AddColumn(hdlRetr, "hdlRetr");


Filter= cupHandle AND Close > MinPrice AND MA(Volume,50) > MinVolume;
//Filter = 1;

--- In amibroker@xxxxxxxxxxxxxxx, "bulletbob19380" 
<robert.mansk@xxxx> wrote:
> 
> 
> Tell me what you mean when you say that it does not work?  
> 
> I have used it through the Automatic Analysis feature of AB.  
Press 
> the 
> Explore button after loading the script.  
> 
> I will say this about the script, it finds matches rarely.  I have 
> used it recently on a daily basis aginst all US stocks and most 
days 
> it has found zero current matches. 
> 
> I got a hit earlier this week with the stock HOTT





------------------------ Yahoo! Groups Sponsor --------------------~--> 
What would our lives be like without music, dance, and theater?
Donate or volunteer in the arts today at Network for Good!
http://us.click.yahoo.com/Tcy2bD/SOnJAA/cosFAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/