Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 1 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... Open in app Get started Published in Analytics Vidhya Caner Irfanoglu Follow Feb 12, 2020 · 5 min read · Listen Save Recognizing over 50 Candlestick Patterns with Python An easy to follow guide for leveraging candlestick patterns for ML Live implementation can be found at www.cryptodashapp.com Image Credit. Photo by Austin Distel on Unsplash W hen making trading decisions, we can utilize several different information sources on our technical analysis. One of these sources is OHLC (open, high, low, close) data. Candlestick charts can be plotted to extract patterns from OHLC data 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 2 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... for any tradable instrument. Open in app Get started A candlestick pattern is a movement in prices shown graphically on a candlestick chart that some believe can predict a particular market movement.¹ Image Credit The full list of simple and complex candlestick patterns with visual examples can be found in this Wikipedia article. Candlestick patterns are great candidates to train Machine Learning models for attempting to predict future prices. In this article, we will go over the feature engineering steps of creating a predictor using candlestick patterns and then visualize our results. We will use python, TA-Lib module and the performance rankings from the www.thepatternsite.com Three steps of the process are: 1. Extract the patterns using TA-Lib 2. Rank the patterns using “Overall performance rank” from the patternsite 3. Pick the best performance candle Extracting the patterns using TA-Lib With TA-Lib, extracting patterns is super simple. We can start by installing the module 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 3 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... from https://github.com/mrjbq7/ta-lib. The repository contains easy to follow instructions for the installation process. Open in app Get started 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. 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 4 of 10 1 import requests 2 import pandas as pd https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... Open in app Get started 3 4 # link for Bitcoin Data 5 link = "https://min-api.cryptocompare.com/data/histoday?fsym=BTC&tsym=USD&limit=365&aggregate=1" 6 7 # API request historical 8 historical_get = requests.get(link) 9 10 # access the content of historical api request 11 historical_json = historical_get.json() 12 13 # extract json data as dictionary 14 historical_dict = historical_json['Data'] 15 16 # extract Final historical df 17 df = pd.DataFrame(historical_dict, 18 columns=['close', 'high', 'low', 'open', 'time', 'volumefrom', 19 dtype='float64') 20 21 # time column is converted to "YYYY-mm-dd hh:mm:ss" ("%Y-%m-%d %H:%M:%S") 22 posix_time = pd.to_datetime(df['time'], unit='s') 23 24 # append posix_time 25 df.insert(0, "Date", posix_time) 26 27 # drop unix time stamp 28 df.drop("time", axis = 1, inplace = True) data_prep.py hosted with ❤ by GitHub view raw 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) 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 5 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... TA-Lib creates individual columns for each pattern. While 0 corresponds to no pattern, Open in app Get started 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, . . . . } 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 6 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... After some manual scraping, the patterns are combined in “candle_rankings” dictionary. Open in app Get started 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” • Single Pattern: 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. 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 7 of 10 1 df['candlestick_pattern'] = np.nan 2 df['candlestick_match_count'] = np.nan 3 for index, row in df.iterrows(): https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... Open in app Get started 4 5 # no pattern found 6 if len(row[candle_names]) - sum(row[candle_names] == 0) == 0: 7 df.loc[index,'candlestick_pattern'] = "NO_PATTERN" 8 df.loc[index, 'candlestick_match_count'] = 0 9 10 # single pattern found elif len(row[candle_names]) - sum(row[candle_names] == 0) == 1: 11 # bull pattern 100 or 200 12 if any(row[candle_names].values > 0): 13 pattern = list(compress(row[candle_names].keys(), row[candle_names].values != 14 df.loc[index, 'candlestick_pattern'] = pattern 15 df.loc[index, 'candlestick_match_count'] = 1 16 # bear pattern -100 or -200 17 else: 18 pattern = list(compress(row[candle_names].keys(), row[candle_names].values != 19 df.loc[index, 'candlestick_pattern'] = pattern 20 df.loc[index, 'candlestick_match_count'] = 1 21 # multiple patterns matched -- select best performance 22 else: 23 # filter out pattern names from bool list of values 24 patterns = list(compress(row[candle_names].keys(), row[candle_names].values != 0 25 container = [] 26 for pattern in patterns: 27 if row[pattern] > 0: 28 29 container.append(pattern + '_Bull') else: 30 container.append(pattern + '_Bear') 31 rank_list = [candle_rankings[p] for p in container] 32 if len(rank_list) == len(container): 33 rank_index_best = rank_list.index(min(rank_list)) 34 df.loc[index, 'candlestick_pattern'] = container[rank_index_best] 35 df.loc[index, 'candlestick_match_count'] = len(container) 36 # clean up candle columns 37 df.drop(candle_names, axis = 1, inplace = True) 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 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 8 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... visualizing the data using Plotly. The dataset and the plot can be compared side by side Open in app and the patterns can be validated easily by matching the indexes. 1 from plotly.offline import plot 2 import plotly.graph_objs as go 3 import pandas as pd Get started 4 5 df = pd.read_csv('./btc_data.csv') 6 7 o = df['open'].astype(float) 8 h = df['high'].astype(float) 9 l = df['low'].astype(float) 10 c = df['close'].astype(float) 11 12 trace = go.Candlestick( 13 open=o, 14 high=h, 15 low=l, 16 close=c) 17 data = [trace] 18 19 plot(data, filename='go_candle1.html') 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. 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 9 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... Open in app Get started Sign up for Analytics Vidhya News Bytes By Analytics Vidhya Tableau Viz for Bitcoin Data Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look. When the patterns found on our dataset are compared to the actual patterns, the results email We can test on larger datasets as part of the future work. Also, since lookYour consistent. some patterns only have a single version, ‘Bull’ and ‘Bear’ tags can be removed from them. Get this newsletter By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices. All scripts and contents of this post including the recognize_candlestick function, can be found at https://github.com/CanerIrfanoglu/medium. You can also see candlestick recognition in action as part of the crypto currency technical analysis dashboard “Crypto Dash” at https://youtu.be/HS3gAmtET9k?t=121. Content here is mainly based on the work of the creators of the TA-Lib module and Thomas Bulkowsi’s long time studies on candlestick patterns. I would like to thank them for making their work publicly available. If you enjoyed or found my work valuable, please make sure to stay synced and feel free to connect on linkedin. I would be delighted to hear your comments and suggestions. Cheers! References 3/31/2022, 1:23 AM Recognizing over 50 Candlestick Patterns with Python | by Caner Irfan... 10 of 10 https://medium.com/analytics-vidhya/recognizing-over-50-candlestick-... [1]Candlestick pattern. (n.d.). In Wikipedia. Retrieved February 11, 2020 from https://en.wikipedia.org/wiki/Candlestick_pattern Open in app Get started 3/31/2022, 1:23 AM