Recognizing over 50 Candlestick Patterns with Python
By: Caner Irfanoglu
Tags: Feature Engineering, How To, Python, Algorithmic Trading, Candlestick Patterns

Extracting the patterns using TA-Lib

With TA-Lib, extracting patterns is super simple. We can start by installing the module from https://github.com/mrjbq7/ta-lib. The repository contains easy to follow instructions for the installation process.

After the installation, we start by importing the module:

import talib

Then, we get a list of available patterns by running:

candle_names = talib.get_function_groups()['Pattern Recognition']

“candle_names” list should look like as follows:

candle_names = [

'CDL2CROWS',

'CDL3BLACKCROWS',

'CDL3INSIDE',

'CDL3LINESTRIKE',

.

.

.

]

We are ready to extract candles! We just need a sample dataset with open, high, low, close values.

Sample Bitcoin dataset preparation

Voila! Bitcoin dataset is ready. Let’s extract the OHLC data and create pattern columns.

# extract OHLC

op = df['open']

hi = df['high']

lo = df['low']

cl = df['close'] # create columns for each pattern

for candle in candle_names:

# below is same as;

# df["CDL3LINESTRIKE"] = talib.CDL3LINESTRIKE(op, hi, lo, cl)

df[candle] = getattr(talib, candle)(op, hi, lo, cl)

TA-Lib creates individual columns for each pattern. While 0 corresponds to no pattern, positive values represent bullish patterns and negative values represent bearish patterns.

Candlestick Patterns found on Bitcoin Data

Congratulations! We just obtained our first dataset with algorithmically extracted patterns.

Ranking the patterns

We successfully extracted candlestick patterns using TA-Lib. With few lines of code, we can condense this sparse information into a single column with pattern labels. But first, we need to handle the cases where multiple patterns are found for a given candle. To do that, we need a performance metric to compare patterns. We will use the “Overall performance rank” from the patternsite.

candle_rankings = {

"CDL3LINESTRIKE_Bull": 1,

"CDL3LINESTRIKE_Bear": 2,

"CDL3BLACKCROWS_Bull": 3,

"CDL3BLACKCROWS_Bear": 3,

"CDLEVENINGSTAR_Bull": 4,

"CDLEVENINGSTAR_Bear": 4,

"CDLTASUKIGAP_Bull": 5,

"CDLTASUKIGAP_Bear": 5,

.

.

.

.

}

After some manual scraping, the patterns are combined in “candle_rankings” dictionary. When there exist multiple patterns, we will use the values in the above dictionary to decide best performance pattern. Full dictionary of the patterns and the explanations of the naming and ranking decisions can be found here.

Picking the best performance candle

Here comes the fun part. We will code the logic for creating the labels. We basically have 3 cases.

No Pattern : Fill the cell with “NO_PATTERN”

: Fill the cell with “NO_PATTERN” Single Pattern : Fill the cell with Pattern Name

: Fill the cell with Pattern Name Multiple Patterns: Fill the cell with lowest (best) ranking Pattern Name

Below is the code for creating the pattern labels and found pattern counts.

Logic for picking best pattern for each candle

Visualizing and validating the results

So far, we extracted many candlestick patterns using TA-Lib (supports 61 patterns as of Feb 2020). We ranked them based on the “Overall performance rank” and selected the best performance pattern for each candle. Next, we can validate our results by plotting the candles and visually check against the patterns found. Below is a sample script for visualizing the data using Plotly. The dataset and the plot can be compared side by side and the patterns can be validated easily by matching the indexes.

Code plotly for visualizing candlesticks

Candlesticks chart for bitcoin data using plotly

You may find this Tableau Viz more convenient to inspect the patterns with the annotations quickly.

Tableau Viz for Bitcoin Data

When the patterns found on our dataset are compared to the actual patterns, the results look consistent. We can test on larger datasets as part of the future work. Also, since some patterns only have a single version, ‘Bull’ and ‘Bear’ tags can be removed from them.