Candlestick Charts are one of the best ways to visualize stock data because hey give us very detailed information about the evolution of share prices. The programming language Python is a great choice for financial analysis and the module matplotlib, additionally offers a lot of tools for advanced data visualization.
In this tutorial, we are going to write a little script that plots a professional candlestick chart for a specific company at the stock market. If you are not familiar with the concept of that diagram type, take a look at the following image.
As you can see, a candlestick can be either positive (green) or negative (red). The former means that the close price is higher than the open price and the latter means the opposite. We can gather four different values out of one candlestick:
- The highest share price of that day (top point of the black line)
- The lowest share price of that day (bottom point of the black line)
- The share price when the market opened (top point of the red area or bottom point of the green area)
- The share price when the market closed (top point of the green area or bottom point of the red area)
Additionally, we can also look at the price span of the respective day which is visualized in the colored area. Thus, this type of chart provides a lot of information.
For our script we will need to import a couple of libraries. Most of them are external and need to be installed.
Let us take a quick look at each of these:
- datetime: We will use this one to define our desired time span
- matplotlib.dates: This library will convert our dates into the necessary number format
- matplotlib.pyplot: Will be used for displaying our chart in the end
- pandas_datareader: The module that will load the desired stock data
- candlestick_ohlc from mpl_finance: Our main library for plotting
Except for the datetime module, none of these libraries is included in Core Python. This means that you will probably need to install them with pip.
Preparing The Data
In order to plot our data properly, we will first need to load it and to set it up. We will start by defining our desired time span.
Our start date is January 1st, 2010 and our end date is defined as the current date. This means that we are looking at the data from our start date up until now. The next step is the definition of our data reader.
Here, we specify that we want to use the Yahoo Finance API in order to download the data for the ticker symbol AAPL (which is Apple). The time span we are looking at is defined by start and end. What we get is a data frame that contains our requested values. We can print out the first few rows to see the structure.
Right now, we have two columns that we don’t need for our chart. These are Volume and Adj Close. For our candlestick chart, we need the values Open, High, Low, and Close in that exact order. Also, we will need Date for our x-axis. In pandas, it is quite simple to select and reorder columns in a data frame.
Basically, we are just selecting the four relevant columns in the right order and replacing our current data frame. Notice that we use double square brackets here.
Now, we have our columns in the right order but there is still a problem. Our date doesn’t have the right format and since it is the index, we cannot manipulate it. Therefore, we need to reset the index and then convert our datetime to a number.
Here, we drop the index and map the date2num function onto our Date column. Now, our datetime values are converted into numbers that matplotlib can deal with. We can start with the plotting.
Plotting The Data
For the final step, we will define our plots and visualize the data we have prepared.
First, we define a new subplot (also called axis) for our data. Then, we use the candlestick function, in order to plot our values. Also, we define the colors and the width of the sticks. Additionally, we put the dates on the x-axis and turn on the grid. Finally, we show our plot. The end result looks like this:
Of course, the more you zoom in, the more you will be able to see the individual candlesticks. This is a great way to visualize four different values in a single chart.
I hope this tutorial was helpful to you! You can check out the links below for extra content.