Identifying Candlestick Patterns Using Deep Learning
Identifying Candlestick Patterns Using Deep Learning
towardsdatascience.com/identifying-candlestick-patterns-using-deep-learning-b7d706726874
1/4
Some of the heatmaps generated using the method described below
(Image by author).
I am calling this an “Experiment” to emphasize that it is one and to make it very clear
that you should not take any financial decisions based on the content given here!
Collecting the daily historical stock data for multiple stocks using the python library
nsepy.
Converting the data into Candlestick charts.
Labelling the Candlestick charts as having an “Up” or “Down” movement based on
percentage movement (of close price) in a particular direction.
Training a Deep Learning Model on the data.
Getting the areas of interest (based on the values of activations)and visualizing them
using heat maps.
You can find the GitHub repo containing the Jupyter Notebooks for this project here. We
will be requiring the following libraries as we move ahead :
nsepy
pandas
2/4
numpy
matplotlib
fastai
In the above piece of code we have defined functions to obtain the historical stock data
and to plot Candlestick charts for it. A brief overview :
The function “obtain_data” takes the ticker symbol and the start and end dates as
input and gives out a Pandas Dataframe containing the stock data.
The function “plot_candles” (which was originally written by Daniel Treiman to
which I have made minor changes) takes in the Pandas Dataframe and outputs a
Candlestick chart.
Sorry for the long output but I wanted it to be so, to give you an idea of how the code is working.
Obtaining the historic stock data (for the past two years) using the “obtain_data”
function.
Determine whether a particular chart should be classified into “Up” or “Down”
based on movement in the upcoming five days.
Create a Candlestick chart for a time period of 20 days.
Save the Candlestick chart to the respective folder (Up/Down).
Repeat for a number of different stocks.
We are now done with data gathering; let’s start training the model!
In the above piece of code we are setting up the data such that we can use it train a deep
learning model. We are completing the following tasks :
Used transfer learning to train a pre-trained neural network on our charts using an
appropriate learning rate.
Increased the size of our images (for the earlier training cycles it was 224 by 224 and
then we increased it to 352 by 352)
Used transfer learning again to train the previous network on the new larger images
using an appropriate learning rate.
3/4
Now we will be creating heatmaps highlighting areas which were of interest to the neural
net using the activations. Let’s do so !
Created a list of all the charts to be analyzed (the ones which were correctly
predicted with a high degree of certainty).
Got a particular image from the above list and transferred it to the GPU.
Used hooks to hook into the model during the forward pass to obtain the
activations.
Used the activations to create a heatmap using matplotlib.
Saved the heatmap.
And Voila ! We managed to train a neural network and use it to identify patterns in a
candlestick chart.
I would like to emphasize this again that please don’t use any of the content given here
to take any financial decisions!
P.S.
Please feel free to connect with me on LinkedIn for any questions or suggestions.
P.P.S.
I have generated a lot heatmaps of candlestick charts, feel free to contact me if you would
like to have a look at them.
4/4