Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a small application that will provide some charts and graphs to be used for technical analysis. I'm new to TA but I'm wondering if there is a way to algorithmically identify the formation of certain patterns. In most of the TA literature I've read the authors explain how to identify these patterns visually. Is there a way to algorithmically determine these patterns so that I could, for example, examine the prices in code and identify a possible Head and Shoulders pattern?

share|improve this question
up vote 22 down vote accepted

As mentioned elsewhere on this site, Lo, Mamaysky, and Wang (2000) do exactly what you're talking about, namely algorithmic detection of head and shoulders patterns. Their definition:

Head-and-shoulders (HS) and inverted head-and-shoulders (IHS) patterns are characterized by a sequence of five consecutive local extrema $E_1,...,E_5$ such that

$$ HS \equiv \begin{cases} E_1 \text{ is a maximum} \\ E_3 > E_1, E_3 > E_5 \\ E_1\text{ and }E_5\text{ are within 1.5 percent of their average} \\ E_2\text{ and }E_4\text{ are within 1.5 percent of their average,} \end{cases} $$

$$ IHS \equiv \begin{cases} E_1\text{ is a minimum} \\ E_3<E_1, E_3 < E_5 \\ E_1\text{ and }E_5\text{ are within 1.5 percent of their average} \\ E_2\text{ and }E_4\text{ are within 1.5 percent of their average.} \end{cases} $$

share|improve this answer
Presumably this depends on the sampling frequency/resolution? – Phil H Mar 1 '13 at 10:48

I would recommend that you read "Evidence-Based Technical Analysis" by David Aronson.

Firstly, I am mentioning it because it is a highly worthwhile book.

Secondly, on pp151--161 he attempts to "objectify subjective TA", using the head-and-shoulders pattern as an example.

share|improve this answer
Another good reference which has been mentioned in the previous questions. On head-and-shoulders, Aronson writes "in a word, the pattern hailed as a cornerstone of charting is a bust." Trading based on head-and-shoulders proved to be worse than random. Aronson also cites the paper by Lo et al in that section. – Tal Fishman Sep 15 '11 at 22:28
Worse than random . . . hmm . . . worth shorting? :p – Vishal Belsare Sep 18 '11 at 14:53

Dynamic Time Warping, recursive, time-delayed feedforward neural networks, wavelets, empirical mode decomposition, ..., there's plenty of it.

BUT If you want my advice, don't go this way, I wasted too much time doing things like that. Neither big nor small players (profitably and consistently) trade this way and for a good reason. Technical analysis is a technology of prehistoric pre-computer era and those patterns are only there after the fact. All those websites, books etc. on that is just a way of incapable people trying to make money on the market in a secondary way. There's few empirical reasons for anyone to share his trading knowledge if it works. Once you have your own stuff, you surely won't be giving it away. And if, why would you as a successful trader try to sell expensive books or trading recommendations? Most of the stuff, starting with technical analysis, is basically a scam or useless spam. You certainly won't make money if you do things too many people know about, that's in the nature of what market is. Go real science.

share|improve this answer
"Go real science"... what an hazy statement... – Stephane Rolland Aug 19 '12 at 15:26
i would second the statement of this answer.. you only have an edge if your method is SUCCESSFUL and UNKNOWN to the majority.. if everybody starts winning, whose loss are they going to capitalize on? – AweSIM Jan 20 '13 at 19:17

Regarding trading, it depends upon one's style and temperament. Don't rely solely on Aronson's book and his views and a phrase quoted by Andrew Lo in his study. The formula posted by Tal Fishman of Head and Shoulders as quoted by Lo, Mamaysky and Wang (2000) is not exhaustive. There is a lot of scope for further improvement.

However, there are many studies which have proved that profits can be made trading the patterns. You can read them. They are as follows:

  1. Bulkowski, T.N. (2000) Encyclopedia of Chart Patterns, John Wiley and Sons, NewYork.

  2. Laedermann, S. (2000), “Head-and-Shoulders Accuracies and How to Trade Them.” IFTA Journal, Vol. 2000 Edition, pp 14-21

  3. Lo, A., Mamaysky, H. and Wang, J. (2000), “Foundations of Technical Analysis: Computational Algorithms, Statistical Inference, and Empirical Implementation.” Journal of Finance, Vol. 55, pp. 1705-1765

  4. Osler, C. L., and Chang, P. H. K., (1995) “Head and Shoulders: Not Just a Flaky Pattern.” Federal Reserve Bank of New York, Staff Reports, Report No. 4.

share|improve this answer

Well pattern recognition and image processing is so developed these days. This is cutting edge in CS now and if we could identify cancer or brain tumor on a hazy image or a suspect face on an industry cam then recognizing head and shoulders on a chart is really really easy.

Support Vector Machines or entropy come to mind but there is a myriad of technologies and they are easily available and so is the processing power for the job.

However there is a big leap between recognizing the pattern and using it as a base for successful trading. You could put a lot of effort and computer power (mind the CO2 it generates) into vain.

Very educational approach though. At least for me it was.

share|improve this answer
Ok, so how does someone identify a pattern algorithmically??? – chrisaycock Mar 5 '13 at 11:42
gather few hundred thousand examples of H&S, pre-process your data, select cost function (1 - formation of H&S resulted in market move of certain magnitude, 0 - otherwise), train SVM at time t(0)-k (before H&S were fully formed), connect to your trading environment. – Robert Jakubowski Mar 5 '13 at 13:36
And you've done this before? – chrisaycock Mar 5 '13 at 13:37
Is this a tricky question? I have done similar and I know that this is possible and there are thousands of smart people who could do a project like that. Probably some do. – Robert Jakubowski Mar 5 '13 at 15:23
Sadly, most topics of SVM and machine learning seem to place so much focus on "classification". When you throw in "time" element, I hear DTW / Markov model terms flying in the sky...and then hear crickets when it comes to actual "code samples" – Antony May 20 '14 at 20:28

One idea is Dynamic time warping (DTW).

There is an R package for that: dtw

Here is the vignette:
Computing and Visualizing Dynamic Time Warping Alignments in R: The dtw Package
by Toni Giorgino

And here is an example from Systematic Investor with full code:
Time Series Matching with Dynamic Time Warping

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.