A simple algorithm to detect complex chart patterns

Finding complex chart patterns has never been an easy task. This article will give you a simple algorithm and a ready to use indicator for complex chart pattern recognition. You will have the freedom to detect any pattern with any pattern length. It has been described as Fréchet distance in literature. This article shows a simple adaptation for chart pattern analysis.

Defining a chart pattern

I am using a simple string definition of a pattern. See the example below:

Pattern definition sample

Pattern definition sample

The V-Top pattern over 11 bars (shown above) can be defined as a simple string. The absolute values of the pattern definition are of no importance. If your pattern runs from 1 to 6 or 1 to 60 will not make any difference. But you surely will detect different patterns if you define the V-Top over 5 bars instead over 11 bars. (5-Bar V-Top: 1,2,3,2,1).

You could use any length and any pattern complexity you would want to have.

With the Tradesignal programming language Equilla the pattern will be defined as an array holding the pattern definition. To get things started I defined some simple show patterns: V-Top, V-Bottom, a rising market and a falling market pattern.

sample pattern definition tradesignal equilla

sample pattern definition tradesignal equilla

Detecting the chart pattern

To actually find the given pattern on the chart some simple calculations have to be done by the given algorithm.

First the chart compressed to fit the pattern. On the chart below the V-Bottom pattern is selected, and as the definition goes from +10 to 1 and back to +9, the chart is compressed to fit the pattern volatility and price range.

In a second step the difference between the chart and the pattern definition is calculated. I use the squared distances between the individual closing prices and the pattern to calculate a fitting factor. This factor is shown underneath the chart.

A perfect fit would return a fitting factor of zero, but this will never happen in reality. Therefore the pattern recognition indicator has got a “fuzzyfactor” input. It defines the minimum accuracy which will be needed to detect the pattern.

A second input to this chart pattern recognition indicator is the minimum volatility of the pattern. As mentioned above it does not come with the pattern definition, but it can be defined later on. On the chart below I set a minimum volatility of 1% between the pattern high and pattern low. This helps to get rid of useless patterns when the market is not moving.

Pattern recognition overlay

Pattern recognition overlay

As soon as the fitting factor (the difference between the pattern and the chart) is below the “fuzzyfactor”, the pattern is detected and shown on the chart.

And now it’s your task: stay creative, define the strings for your favourite patterns and use the algorithm to find your favourable patterns in any market.

 

Tradesignal indicator code

Please click the link to see the Tradesignal implementation of this indicator. You are free to use it for non commercial purposes as stated in the imprint. If this is fine for you then type iconsent as password to the code page.

Thanks to Tradesignal for the great software to accomplish this task, thanks to Refinitiv for the great data

another article on candlestick pattern recognition can be found here

23 thoughts on “A simple algorithm to detect complex chart patterns

  1. This is really interesting. Much more elegant than ways I have tried pattern scanning using volatility changes within time periods. How can I read the tradesignal code in order to mess around with it? Seems to be an error page of sorts after entering the password.

  2. Hi Kahler Philip!
    Thank you very much for the information. I tried to convert the code into javascript and wasn’t able after several attempt.
    Can you help me with the conversion as you understand both the concept and the code?

  3. Thanks for your excellent coding I am able to convert it to easylanguage which I will use to create scanner to scan for stocks at VBottom .
    Do you think we can do this similar coding for pennant , M and W , as well as triangle patterns ?
    Thank you
    Regards
    Tony

    • sure. Just fill the array with the numbers needed or create a different array length.
      For patterns over several bars you might want to have several definitions. (a M pattern can go over 20 to 50 bars, do a 20, 35 &50 bar definition to find most of them…)

Leave a Reply

Your email address will not be published.