Sometimes I need to plot some financial data and it might be tricky because most of the financial websites do not offer enough customization or they have a usage limitations unless you subscribe. So, I’ve decided to find a reliable data source and plot this data myself. Here are the basic steps that are necessary to do that.
This is the first part of the “Plotting Financial Data With Python” series and it’s better if you read it in chronological order:
- Part 1 - History (you are here)
- Part 2 - Variance
- Part 3 - Comparing Returns
- Part 4 - Efficient Frontier
- Part 5 - Efficient Frontier (N Assets)
You can get the full source code here: https://github.com/bubelov/market-plots
Picking The Right Tools
There are 3 choices that have to be made before we start coding:
- Choose a data source
- Choose a programming language
- Choose a visualization library
Choosing a Data Source
There are many financial data sources but most of them are not free. There is nothing wrong with paying for high quality data but I don’t use it often enough to justify those costs so I use a free API called AlphaVantage unless it doesn’t have the data I need. I also use AlphaVantage API in this series but you can pick any other data source, it does not affect other parts of the code as long as you’ve got the relevant data.
I prefer to use Python for quick data visualizations since it’s widely adopted for such tasks. It’s also a really nice language for writing small utilities or integrating different parts of a big system.
There are plenty of libraries for plotting data. Here is the list from the Python wiki: Plotting
I use Matplotlib since it’s widely adopted and it’s got everything that I need.
Here is their website: Matplotlib
This post assumes that you have Python 3 installed. If you have older version of Python you’re going to need some code adjustments.
Let’s create our project folder and give it a sensible name, such as /market-plots. It’s a good practice to isolate our little project from the rest of the system so we won’t mess with the global package registry.
Here is how we can create a local environment for a scope of this project:
cd market-plots virtualenv venv
This command will create a folder called venv which will contain our project-scoped dependencies.
Let’s activate our virtual environment:
You can always exit your virtual environment by executing this command:
Now, let’s create a text file where we’ll list all of the needed dependencies. By convention, it should be called requirements.txt:
matplotlib requests python-dotenv
We can install all of those dependencies by executing the following command:
pip install -r requirements.txt
Now we have everything ready to start coding.
Getting Alpha Vantage API key
You can obtain a free API key here and add it to your project by executing the following command:
echo ALPHA_VANTAGE_KEY=YOUR_API_KEY > .env
Let’s create a new file and call it alpha_vantage.py. It will wrap TIMESERIES list of functions, you can check the AlphaVantage API documentation for more details.
The logic inside this file is quite straightforward. Let’s examine all of the arguments to understand how to use this function properly:
- symbol - it’s just a stock trading symbol such as GOOG, TSLA and so on
- interval - sampling interval, you can use DAILY, WEEKLY or MONTHLY intervals
- adjusted - whether to use an absolute price or adjust for stock splits
Now since we have a financial API wrapper, let’s use it to plot stock price history. We need to add a new file called history.py which should contain the following code:
Let’s test this script by giving it a real query. We can lookup the S&P 500 index history by passing it’s symbol (SPX) as an argument to our new script:
python history.py SPX
You should see the chart similar to this one:
We’ve created a simple wrapper that allows us to query stock price history and used it to plot the data on screen. In the next post we will use this data to show the risk of different stocks.
Next Post: Part 2 - Variance