# How to identify technical analysis chart patterns algorithmically?

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?

## 6 Answers

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}$$

• Presumably this depends on the sampling frequency/resolution? Mar 1, 2013 at 10:48
• Can someone explain what is meant with E1 is a Maximum? Aug 31, 2016 at 15:13
• How is $E_1$ a maximum while $E_3 > E_1$. Can you explain?
– QFi
Nov 25, 2017 at 22:50
• 𝐸1 is a maximum means: Start with a left shoulder core.ac.uk/reader/213966840 So it is not a maximum peak? May 29, 2020 at 12:35
• E1 is a local maximum. It's a mathematics term. It does not mean max(). E3(head) > E1(shoulder) is pretty simple -- head is above shoulder. Same thing for E5(shoulder). Shoulders E1 and E5 should be about level within 1.5% Apr 13, 2021 at 5:26

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.

• 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. Sep 15, 2011 at 22:28
• Worse than random . . . hmm . . . worth shorting? :p Sep 18, 2011 at 14:53
• Umm, anything not random implies some form of arbitrage. Worse than random would be wonderful. Unless of course, the market is actually a malevolent beast looking to analyze what you do and specifically inflict pain on any choice you make. In that case, a lot of people whom I dismiss as paranoid would actually be right. Oct 4, 2016 at 18:09

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.

• "Go real science"... what an hazy statement... Aug 19, 2012 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? Jan 20, 2013 at 19:17
• During the early 2000's I was running a high-speed arb and automated market making firm. One of the investors was sure we could use technical analysis to run our programs. We programmed in about 50 of the most popular technical strategies of the time. Ran them on multiple time frames across multiple market conditions using a lot of data. After adjusting for standard factors and accounting for data mining results we found no indication that technical analysis gave us any info that would raise the returns past noise and trading costs. That was just us, you may do better. Oct 4, 2016 at 18:16

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

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.

• I used Bulkowski's publication as a bible for implementing code to recognise patterns. Unfortunately, the dataset I was working with did not correlate with Bulkowski's examples. I got fired for telling my boss this observation. And quite rightly too - I made it clear that I lost belief in the book. Apr 6, 2018 at 19:40

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.

• Ok, so how does someone identify a pattern algorithmically??? Mar 5, 2013 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. Mar 5, 2013 at 13:36
• And you've done this before? Mar 5, 2013 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. Mar 5, 2013 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" May 20, 2014 at 20:28