Charting Volume and Close Price together in Matplotlib

Rob Dundas
4 min readMay 1, 2022

--

April 30, 2022

In this article, I’ll use the TD Ameritrade API to retrieve the historical prices and volumes for a given stock ticker. With this data, I’ll use the data to graph everything into two side-by-side charts using matplotlib.

Google 3-month Price History chart with Volume Bar Chart using Matplotlib.
Google 3-month price chart with volume with Matplotlib
Google 3-month price chart with volume bar chart from Yahoo! Finance.
Google 3-month price chart with volume from Yahoo! Finance

Continuing from the “Snappin’ Necks and Cashing Checks” series, I wanted to put everything that I learned into a single matplotlib chart, where there are two charts: One showing the close price over time and the trading volume. In a future article, I’ll be using this data to calculate and chart the Relative Strength Index (RSI), as I have learned that this is one of the fundamental “oscillators” in technical analysis. I am not a financial analysis and admit to knowing very little in this space, which is why I am writing these articles; to learn and use my programming skills to write something fun.

After a quick review of Matplotlib’s documentation, this should be possible. Specifically, I want to draw a stacked chart, where the close price, 50-day moving average and 200-day moving average are all charted on the top chart, and the volume is charted on the second chart below it.

In looking at the charts used by technical traders, this seems like a good first step in the right direction. I am modeling the first chart from Investopedia’s article Essential Strategies for Trading Volume by Casey Murphy, where the article states:

…volume, is one of the most accurate ways of gauging money flow…High volume suggests that there is a heightened interest in the name, and if it is combined with a move higher in share price, then it is often used as a signal of strong upward momentum.

Price history and volume example chart taken from Investopedia website
Image by Sabrina Jiang © Investopedia 2020

The first charts are the one that I created (gist below) side-by-side with the same chart from Yahoo Finance. From a price perspective, the lines look more or less the same, however, when looking at the volume bars, I am seeing instances that should be green are drawn red and vice versa. More on this below…

Next, the code!

The code is broken into roughly four sections: 1) Get the data, 2) Set up the chart(s), 3) Draw the charts, and 4) Display the chart(s). I would walk the article through each section, but honestly, I’ve done a little day drinking and I’m not in the mood to type it all out. That said, here’s the gist:

Tesla 3-month price history graph with volume bar chart using Matplotlib
Tesla 3-month Price History with Volume Bar Chart using Matplotlib
Tesla 3-month price history graph with volume bar chart from Yahoo! Finance
Tesla 3-month Price History with Volume Bar Chart from Yahoo! Finance

In the first charts (GOOG), there is a big discrepancy between the colors that I reflect in the volume and the ones that are in Yahoo Finance and TD Ameritrade’s website. Specifically, the >4M volume spike on 2/1/2022. However, when I look at that day in the JSON from the API, the values are correct, which means that the API is not delivering accurate data. I will have to investigate this, but I don’t believe that is a topic for this article. For this article, I am quite content at being able to draw a price history graph along with a volume color-coded bar chart. It’s honestly starting to look a little professional :) but let’s not get carried away (yet).

The charts are very nice to see and definitely provide insight into a particular stock, however, I would like to start learning how to use Python to make predictions on stock movement, which may be the topic of future articles. It’s this kind of modeling that I would like to get into next. I’m having a lot of fun with this though!

Happy coding!

Originally published at https://insurtechprogramming.com.

--

--

Rob Dundas
Rob Dundas

Written by Rob Dundas

Attorney, Python enthusiast, insurance technology guru, canine aficionado, musician

No responses yet