Cup&handle and head&shoulder pattern Dettector / please helps

#1
Dear all Bro!

I love the cup&handle and head&shoulder pattern, if possible, please kindly give me the .alf to find out these pattern in amibroker.

Many many thanks for your helps
 
#2
Here you go.

--- Begin cut ---

*
Cup and Handle exploration written by Jerry Tyliczka
visit us at: http://portals.wallstreettape.com

This code written per specifications found at
http://www.haikulabs.com/mh.htm
Version 1.0

This code calculates the Alpha, Delta, Beta and Gamma values
but they are not used in the filter criteria as specified
by the article which this code was based on.

Look for additional changes as I tweak the below code.
I will also include Scan feature and Backtesting in future release.

Comments, please write to [email protected]
*/
MinPrice = 20;
MinVolume = 100000;

//Left side of Handle formation can occur anywhere from 2-25days - look at the last 25 bars and get the number of bars since condition met.
LH = HHV(Close, 25); // Highest close past 25 days.
BLH = HHVBars(Close, 25); // Tells us # of bars that have past since high reached. Used to determine lowest bar.

BH = LLV(Close, BLH); // Lowest close since the highest value was reached/
BBH = LLVBars(Close, BLH); // number of bars that have past since lowest value.

NBLH = BLH - BBH; // this is the number of bars in the formation of the left side handle. NBLH must be atleast 2 to be a valid handle formation.

// 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).
BC = LLV(Close, BLH + 25); // look at 25 bars since the left side of handle.
BBC = LLVBars(Close, BLH + 25);

// get the left side of the cup.
LC = Ref(HHV(Close, 120), BBC * -1);
BLC = Ref(HHVBars(Close, 120), BBC * -1);

// Get highest value before left side of cup started to form.
KC = Ref(HHV(Close, 30), BLC * -1);
BKC = Ref(HHVBars(Close, 120), BLC * -1);

Delta = LC / KC;

//Calculate the up/down relative price value during time frame RC (Right Cup Formation)
URPV = DRPV = 0;
i = EndValue(BLH);
j = EndValue(BBC);
do
{
URPV = IIf(Ref(Close, i * -1) > Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i * -1)) - Ref(Close, (i + 1) * -1), URPV);
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Alpha = URPV/DRPV; // Should be >1

// Calculate Beta
DRPV = 0;
i = EndValue(BBH);
j = EndValue(BLH);
do
{
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Beta = URPV / DRPV;
Gamma = log(Alpha) + log(Beta) + delta;

AddColumn(LH, "Left Handle");
AddColumn(BH, "Bottom Handle");
AddColumn(BC, "Bottom Cup");
AddColumn(LC, "Left Cup");
AddColumn(ALPHA, "Alpha");
AddColumn(DELTA, "Delta");
AddColumn(BETA, "BETA");
AddColumn(GAMMA, "Gamma");

// Filter Criteria as follows:
// 1. Right side of handle must be at least 2 bars. NBHL>2
// 2. Bottom of the cup must be lower than the left top of the cup.
// 3. Left handle must be lower than or equal to the lect cup formation.
// 4. Bottom of the cup must be less than the left handle.
// 5. Bottom of the handle must be > 80% of the left handle + 20% of the bottom cup.
// 6. Start of cup/handle formation must be greater than precedding chart value. LC>LC
// 7. Minimum price and volume you can set any way you like.
Filter = NBLH > 2 AND Close > BH AND BC < LC AND LH <= LC AND BC < LH AND BH < LH AND BH > .8 * LH + .2 * BC AND KC < LC AND Close > MinPrice AND MA(Volume, 30) > MinVolume;

---End cut ---

---Begin cut ---

_SECTION_BEGIN("Head & Shoulders Pattern");
x = Cum(1);
per = 5;
back = 50;
inter = 100;
s11 = H;
s12 = L;

/* H & S */
pR = PeakBars(s11, per, 1) == 0;
endt1 = LastValue(ValueWhen(pR, x, 1));
medt1 = LastValue(ValueWhen(pR, x, 2));
startt1 = LastValue(ValueWhen(pR, x, 3));
dt1 = endt1 - startt1;
C1 = x == endt1 OR x == medt1 OR x == startt1;
endR = LastValue(ValueWhen(pR, s11, 1));
medR = LastValue(ValueWhen(pR, s11, 2));
startR = LastValue(ValueWhen(pR, s11, 3));
Filter1 = medR > endR AND medR > startR AND abs(startR - endR) < 0.02 * (startR + endR) AND dt1 < inter AND endt1 > LastValue(x) - back;
MaxGraph = 10;
Graph1 = C;
Graph1Style = 64;
GraphXSpace = 5;

/*H&S Neck Line*/
Aper = medt1 - startt1;
bper = endt1 - medt1;
La = LastValue(ValueWhen(x == medt1, LLV(L, Aper)));
Lb = LastValue(ValueWhen(x == -1 + endt1, LLV(L, bper)));
Fa = L == La AND x > startt1 AND x < medt1;
Fb = L == Lb AND x > medt1 AND x < endt1;
endt = LastValue(ValueWhen(Fb, x));
startt = LastValue(ValueWhen(Fa, x));
dtS = endt - startt;endS = Lb;
startS = La;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS * (x - endt) + bS;
Graph3 = IIf(Filter1 AND x > startt - 5, trendlineS, -1e10);
Graph3BarColor = 7;

/*Inverted H & S*/
tpR = TroughBars(s12, per, 1) == 0;
tendt1 = LastValue(ValueWhen(tpr, x, 1));
tmedt1 = LastValue(ValueWhen(tpr, x, 2));
tstartt1 = LastValue(ValueWhen(tpr, x, 3));
tdt1 = tendt1 - tstartt1;
C2 = x == tendt1 OR x == tmedt1 OR x == tstartt1;
tendR = LastValue(ValueWhen(tpR, s12, 1));
tmedR = LastValue(ValueWhen(tpR, s12, 2));
tstartR = LastValue(ValueWhen(tpR, s12, 3));
Filter2 = tmedR < tendR AND tmedR < tstartR AND abs(tstartR - tendR) < 0.02 * (tstartR + tendR) AND tdt1 < inter AND tendt1 > LastValue(x) - back;
Graph1BarColor = IIf(C1 AND Filter1, 7, IIf(C2 AND Filter2, 10, 1));

/*Inverted H&S Neck Line*/
tAper = tmedt1 - tstartt1;
tbper = tendt1 - tmedt1;
Ha = LastValue(ValueWhen(x == tmedt1, HHV(H, tAper)));
Hb = LastValue(ValueWhen(x == -1 + tendt1, HHV(H, tbper)));
tFa = H == Ha AND x > tstartt1 AND x < tmedt1;
tFb = H == Hb AND x > tmedt1 AND x < tendt1;
Rendt = LastValue(ValueWhen(tFb, x));
Rstartt = LastValue(ValueWhen(tFa, x));
Rdt = Rendt - Rstartt;
endR = Hb;
startR = Ha;
aR = (endR - startR) / Rdt;
bR = endR;
trendlineR = aR * (x - Rendt) + bR;
Graph4 = IIf(Filter2 AND x > Rstartt - 5, trendlineR, -1e10);
Graph4BarColor = 10;
Filter = Filter1 OR Filter2;
NumColumns = 2;
/*Graph2 = x == -1 + tendt1;*/
Column0 = Filter1;
Column1 = Filter2;
Column0Format = Column1Format = 1.0;
Column0Name = "H&S";
Column1Name = "inv H&S";
_SECTION_END();

--- End cut ---

Regards.
 
#4
Here you go.

--- Begin cut ---

*
Cup and Handle exploration written by Jerry Tyliczka
visit us at: http://portals.wallstreettape.com

This code written per specifications found at
http://www.haikulabs.com/mh.htm
Version 1.0

This code calculates the Alpha, Delta, Beta and Gamma values
but they are not used in the filter criteria as specified
by the article which this code was based on.

Look for additional changes as I tweak the below code.
I will also include Scan feature and Backtesting in future release.

Comments, please write to [email protected]
*/
MinPrice = 20;
MinVolume = 100000;

//Left side of Handle formation can occur anywhere from 2-25days - look at the last 25 bars and get the number of bars since condition met.
LH = HHV(Close, 25); // Highest close past 25 days.
BLH = HHVBars(Close, 25); // Tells us # of bars that have past since high reached. Used to determine lowest bar.

BH = LLV(Close, BLH); // Lowest close since the highest value was reached/
BBH = LLVBars(Close, BLH); // number of bars that have past since lowest value.

NBLH = BLH - BBH; // this is the number of bars in the formation of the left side handle. NBLH must be atleast 2 to be a valid handle formation.

// 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).
BC = LLV(Close, BLH + 25); // look at 25 bars since the left side of handle.
BBC = LLVBars(Close, BLH + 25);

// get the left side of the cup.
LC = Ref(HHV(Close, 120), BBC * -1);
BLC = Ref(HHVBars(Close, 120), BBC * -1);

// Get highest value before left side of cup started to form.
KC = Ref(HHV(Close, 30), BLC * -1);
BKC = Ref(HHVBars(Close, 120), BLC * -1);

Delta = LC / KC;

//Calculate the up/down relative price value during time frame RC (Right Cup Formation)
URPV = DRPV = 0;
i = EndValue(BLH);
j = EndValue(BBC);
do
{
URPV = IIf(Ref(Close, i * -1) > Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i * -1)) - Ref(Close, (i + 1) * -1), URPV);
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Alpha = URPV/DRPV; // Should be >1

// Calculate Beta
DRPV = 0;
i = EndValue(BBH);
j = EndValue(BLH);
do
{
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Beta = URPV / DRPV;
Gamma = log(Alpha) + log(Beta) + delta;

AddColumn(LH, "Left Handle");
AddColumn(BH, "Bottom Handle");
AddColumn(BC, "Bottom Cup");
AddColumn(LC, "Left Cup");
AddColumn(ALPHA, "Alpha");
AddColumn(DELTA, "Delta");
AddColumn(BETA, "BETA");
AddColumn(GAMMA, "Gamma");

// Filter Criteria as follows:
// 1. Right side of handle must be at least 2 bars. NBHL>2
// 2. Bottom of the cup must be lower than the left top of the cup.
// 3. Left handle must be lower than or equal to the lect cup formation.
// 4. Bottom of the cup must be less than the left handle.
// 5. Bottom of the handle must be > 80% of the left handle + 20% of the bottom cup.
// 6. Start of cup/handle formation must be greater than precedding chart value. LC>LC
// 7. Minimum price and volume you can set any way you like.
Filter = NBLH > 2 AND Close > BH AND BC < LC AND LH <= LC AND BC < LH AND BH < LH AND BH > .8 * LH + .2 * BC AND KC < LC AND Close > MinPrice AND MA(Volume, 30) > MinVolume;

---End cut ---

---Begin cut ---

_SECTION_BEGIN("Head & Shoulders Pattern");
x = Cum(1);
per = 5;
back = 50;
inter = 100;
s11 = H;
s12 = L;

/* H & S */
pR = PeakBars(s11, per, 1) == 0;
endt1 = LastValue(ValueWhen(pR, x, 1));
medt1 = LastValue(ValueWhen(pR, x, 2));
startt1 = LastValue(ValueWhen(pR, x, 3));
dt1 = endt1 - startt1;
C1 = x == endt1 OR x == medt1 OR x == startt1;
endR = LastValue(ValueWhen(pR, s11, 1));
medR = LastValue(ValueWhen(pR, s11, 2));
startR = LastValue(ValueWhen(pR, s11, 3));
Filter1 = medR > endR AND medR > startR AND abs(startR - endR) < 0.02 * (startR + endR) AND dt1 < inter AND endt1 > LastValue(x) - back;
MaxGraph = 10;
Graph1 = C;
Graph1Style = 64;
GraphXSpace = 5;

/*H&S Neck Line*/
Aper = medt1 - startt1;
bper = endt1 - medt1;
La = LastValue(ValueWhen(x == medt1, LLV(L, Aper)));
Lb = LastValue(ValueWhen(x == -1 + endt1, LLV(L, bper)));
Fa = L == La AND x > startt1 AND x < medt1;
Fb = L == Lb AND x > medt1 AND x < endt1;
endt = LastValue(ValueWhen(Fb, x));
startt = LastValue(ValueWhen(Fa, x));
dtS = endt - startt;endS = Lb;
startS = La;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS * (x - endt) + bS;
Graph3 = IIf(Filter1 AND x > startt - 5, trendlineS, -1e10);
Graph3BarColor = 7;

/*Inverted H & S*/
tpR = TroughBars(s12, per, 1) == 0;
tendt1 = LastValue(ValueWhen(tpr, x, 1));
tmedt1 = LastValue(ValueWhen(tpr, x, 2));
tstartt1 = LastValue(ValueWhen(tpr, x, 3));
tdt1 = tendt1 - tstartt1;
C2 = x == tendt1 OR x == tmedt1 OR x == tstartt1;
tendR = LastValue(ValueWhen(tpR, s12, 1));
tmedR = LastValue(ValueWhen(tpR, s12, 2));
tstartR = LastValue(ValueWhen(tpR, s12, 3));
Filter2 = tmedR < tendR AND tmedR < tstartR AND abs(tstartR - tendR) < 0.02 * (tstartR + tendR) AND tdt1 < inter AND tendt1 > LastValue(x) - back;
Graph1BarColor = IIf(C1 AND Filter1, 7, IIf(C2 AND Filter2, 10, 1));

/*Inverted H&S Neck Line*/
tAper = tmedt1 - tstartt1;
tbper = tendt1 - tmedt1;
Ha = LastValue(ValueWhen(x == tmedt1, HHV(H, tAper)));
Hb = LastValue(ValueWhen(x == -1 + tendt1, HHV(H, tbper)));
tFa = H == Ha AND x > tstartt1 AND x < tmedt1;
tFb = H == Hb AND x > tmedt1 AND x < tendt1;
Rendt = LastValue(ValueWhen(tFb, x));
Rstartt = LastValue(ValueWhen(tFa, x));
Rdt = Rendt - Rstartt;
endR = Hb;
startR = Ha;
aR = (endR - startR) / Rdt;
bR = endR;
trendlineR = aR * (x - Rendt) + bR;
Graph4 = IIf(Filter2 AND x > Rstartt - 5, trendlineR, -1e10);
Graph4BarColor = 10;
Filter = Filter1 OR Filter2;
NumColumns = 2;
/*Graph2 = x == -1 + tendt1;*/
Column0 = Filter1;
Column1 = Filter2;
Column0Format = Column1Format = 1.0;
Column0Name = "H&S";
Column1Name = "inv H&S";
_SECTION_END();

--- End cut ---

Regards.
bro it is showing error.....
 

Attachments

#5
Here you go.

--- Begin cut ---

*
Cup and Handle exploration written by Jerry Tyliczka
visit us at: http://portals.wallstreettape.com

This code written per specifications found at
http://www.haikulabs.com/mh.htm
Version 1.0

This code calculates the Alpha, Delta, Beta and Gamma values
but they are not used in the filter criteria as specified
by the article which this code was based on.

Look for additional changes as I tweak the below code.
I will also include Scan feature and Backtesting in future release.

Comments, please write to [email protected]
*/
MinPrice = 20;
MinVolume = 100000;

//Left side of Handle formation can occur anywhere from 2-25days - look at the last 25 bars and get the number of bars since condition met.
LH = HHV(Close, 25); // Highest close past 25 days.
BLH = HHVBars(Close, 25); // Tells us # of bars that have past since high reached. Used to determine lowest bar.

BH = LLV(Close, BLH); // Lowest close since the highest value was reached/
BBH = LLVBars(Close, BLH); // number of bars that have past since lowest value.

NBLH = BLH - BBH; // this is the number of bars in the formation of the left side handle. NBLH must be atleast 2 to be a valid handle formation.

// 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).
BC = LLV(Close, BLH + 25); // look at 25 bars since the left side of handle.
BBC = LLVBars(Close, BLH + 25);

// get the left side of the cup.
LC = Ref(HHV(Close, 120), BBC * -1);
BLC = Ref(HHVBars(Close, 120), BBC * -1);

// Get highest value before left side of cup started to form.
KC = Ref(HHV(Close, 30), BLC * -1);
BKC = Ref(HHVBars(Close, 120), BLC * -1);

Delta = LC / KC;

//Calculate the up/down relative price value during time frame RC (Right Cup Formation)
URPV = DRPV = 0;
i = EndValue(BLH);
j = EndValue(BBC);
do
{
URPV = IIf(Ref(Close, i * -1) > Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i * -1)) - Ref(Close, (i + 1) * -1), URPV);
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Alpha = URPV/DRPV; // Should be >1

// Calculate Beta
DRPV = 0;
i = EndValue(BBH);
j = EndValue(BLH);
do
{
DRPV = IIf(Ref(Close, i * -1) < Ref(Close, (i + 1) * -1), Ref(Volume, (i * -1)) * Ref(Close, (i + 1) * -1) - Ref(Close, (i * -1)), DRPV);
i++;
}
while (i < j);
Beta = URPV / DRPV;
Gamma = log(Alpha) + log(Beta) + delta;

AddColumn(LH, "Left Handle");
AddColumn(BH, "Bottom Handle");
AddColumn(BC, "Bottom Cup");
AddColumn(LC, "Left Cup");
AddColumn(ALPHA, "Alpha");
AddColumn(DELTA, "Delta");
AddColumn(BETA, "BETA");
AddColumn(GAMMA, "Gamma");

// Filter Criteria as follows:
// 1. Right side of handle must be at least 2 bars. NBHL>2
// 2. Bottom of the cup must be lower than the left top of the cup.
// 3. Left handle must be lower than or equal to the lect cup formation.
// 4. Bottom of the cup must be less than the left handle.
// 5. Bottom of the handle must be > 80% of the left handle + 20% of the bottom cup.
// 6. Start of cup/handle formation must be greater than precedding chart value. LC>LC
// 7. Minimum price and volume you can set any way you like.
Filter = NBLH > 2 AND Close > BH AND BC < LC AND LH <= LC AND BC < LH AND BH < LH AND BH > .8 * LH + .2 * BC AND KC < LC AND Close > MinPrice AND MA(Volume, 30) > MinVolume;

---End cut ---

---Begin cut ---

_SECTION_BEGIN("Head & Shoulders Pattern");
x = Cum(1);
per = 5;
back = 50;
inter = 100;
s11 = H;
s12 = L;

/* H & S */
pR = PeakBars(s11, per, 1) == 0;
endt1 = LastValue(ValueWhen(pR, x, 1));
medt1 = LastValue(ValueWhen(pR, x, 2));
startt1 = LastValue(ValueWhen(pR, x, 3));
dt1 = endt1 - startt1;
C1 = x == endt1 OR x == medt1 OR x == startt1;
endR = LastValue(ValueWhen(pR, s11, 1));
medR = LastValue(ValueWhen(pR, s11, 2));
startR = LastValue(ValueWhen(pR, s11, 3));
Filter1 = medR > endR AND medR > startR AND abs(startR - endR) < 0.02 * (startR + endR) AND dt1 < inter AND endt1 > LastValue(x) - back;
MaxGraph = 10;
Graph1 = C;
Graph1Style = 64;
GraphXSpace = 5;

/*H&S Neck Line*/
Aper = medt1 - startt1;
bper = endt1 - medt1;
La = LastValue(ValueWhen(x == medt1, LLV(L, Aper)));
Lb = LastValue(ValueWhen(x == -1 + endt1, LLV(L, bper)));
Fa = L == La AND x > startt1 AND x < medt1;
Fb = L == Lb AND x > medt1 AND x < endt1;
endt = LastValue(ValueWhen(Fb, x));
startt = LastValue(ValueWhen(Fa, x));
dtS = endt - startt;endS = Lb;
startS = La;
aS = (endS - startS) / dtS;
bS = endS;
trendlineS = aS * (x - endt) + bS;
Graph3 = IIf(Filter1 AND x > startt - 5, trendlineS, -1e10);
Graph3BarColor = 7;

/*Inverted H & S*/
tpR = TroughBars(s12, per, 1) == 0;
tendt1 = LastValue(ValueWhen(tpr, x, 1));
tmedt1 = LastValue(ValueWhen(tpr, x, 2));
tstartt1 = LastValue(ValueWhen(tpr, x, 3));
tdt1 = tendt1 - tstartt1;
C2 = x == tendt1 OR x == tmedt1 OR x == tstartt1;
tendR = LastValue(ValueWhen(tpR, s12, 1));
tmedR = LastValue(ValueWhen(tpR, s12, 2));
tstartR = LastValue(ValueWhen(tpR, s12, 3));
Filter2 = tmedR < tendR AND tmedR < tstartR AND abs(tstartR - tendR) < 0.02 * (tstartR + tendR) AND tdt1 < inter AND tendt1 > LastValue(x) - back;
Graph1BarColor = IIf(C1 AND Filter1, 7, IIf(C2 AND Filter2, 10, 1));

/*Inverted H&S Neck Line*/
tAper = tmedt1 - tstartt1;
tbper = tendt1 - tmedt1;
Ha = LastValue(ValueWhen(x == tmedt1, HHV(H, tAper)));
Hb = LastValue(ValueWhen(x == -1 + tendt1, HHV(H, tbper)));
tFa = H == Ha AND x > tstartt1 AND x < tmedt1;
tFb = H == Hb AND x > tmedt1 AND x < tendt1;
Rendt = LastValue(ValueWhen(tFb, x));
Rstartt = LastValue(ValueWhen(tFa, x));
Rdt = Rendt - Rstartt;
endR = Hb;
startR = Ha;
aR = (endR - startR) / Rdt;
bR = endR;
trendlineR = aR * (x - Rendt) + bR;
Graph4 = IIf(Filter2 AND x > Rstartt - 5, trendlineR, -1e10);
Graph4BarColor = 10;
Filter = Filter1 OR Filter2;
NumColumns = 2;
/*Graph2 = x == -1 + tendt1;*/
Column0 = Filter1;
Column1 = Filter2;
Column0Format = Column1Format = 1.0;
Column0Name = "H&S";
Column1Name = "inv H&S";
_SECTION_END();

--- End cut ---

Regards.
Dear Bro!

If possible, please give me an explaination about head&shoulder alf like cup&handle explaination. if have me understanding the code and may help me can change somethings in the future

many many thanks
 

Broker Special Offers

Intraday Higher Leverage

Save up to 90% in brokerage and get higher leverage for intraday trades.

Name:Phone:
Email:City:
State:
Are you a day trader?