Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Anyone know how to code for cup and handle pattern?

I am interested in getting some assistance in writing code to find true cup and handle patterns among stocks in the s&p 500 . . .

5 responses

Do you have a good definition (mathematic or otherwise) of what a cup is?

John:

John, the best approximation I can find is this:

a = 10% // fluctuating toleration
ß = 33% //cup depth criteria ? = 10% //RH,LH d = 15% //handle depth
obsPoint = t, high = t // t > 26, suggest 52 weeks or longer
//start from time t, trace back to find Righ High Point While (a[t-1] >= a[t] || a[t-1] < a[t]&a[t-1]>=(1- a )*high
{ If(a[t-1]>=a[t], then high=t-1; t--; }
Right_High_Point = t; //found Right High
If( |a[Right_High]-a[obsPoint] | > ? * a[Right_High])
Return false; //rule 3
//start from Right High, trace back to find Dip Point low = t
While (a[t-1] <= a[t] || a[t-1] > a[t]&a[t-1]<=(1+ a )*low
{ If(a[t-1]<=a[t], then low=t-1; t--; }
Dip_Point = t; //found Dip Point

If(Right_High-Dip_point < 2 || Right_High-Dip_point > 8
Return false;
//shakeout handle is too long or too short (rule 5) If(a[Right_High]-a[Dip_point]> ß * a[Right_High])
Return false; //because too dip (rule 2)
//start from Dip, trace back to find Left High Point While (a[t-1] >= a[t] || a[t-1] < a[t]&a[t-1]>=(1- a )*high
{ If(a[t-1]>=a[t], then high=t-1; t--; }
Left_High = t; //found Left High
If(Right_High-Left_High <12&&Right_High-Left_High >26)
Return false; //because cup is too narrow or too wide (rule 4)
If(|a[Right_High]-a[Left_High]|> d*a[Right_High])
Return false; //rule 1

@Larry, you can use three back-ticks to tell the editor to treat a block of text as code. Also it is possible to edit posts after submitting. Cheers.

I guess you're talking about the Cup & Handle chart pattern?

http://www.investopedia.com/university/charts/charts3.asp
http://en.wikipedia.org/wiki/Cup_and_handle

Someone implemented it in AmiBroker language: http://www.amibroker.com/library/detail.php?id=306

Of course I cannot vouch for the efficacy of that program. It would be interesting if you could get it working!

It seems to me like cup and handle patterns are pretty rare, so it might require extra effort to get enough action. I attempted a port of that AmiBroker algorithm. It doesn't yet compute alpha and beta as is talked about in http://www.haikulabs.com/mh.htm, though, since I couldn't find any cup and handle patterns even without those extra bounds.

Because I couldn't find any matches for cup and handle patterns (even after searching for a while), I can't say for certain that this works. Can anyone find a stock that it matches? If so, this could be more easily analyzed and could be useful.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.