In this quick tutorial, we are going to use python to get data about a collection of stocks, and then plot then on a single graph.
The very first thing we need to is find the tickers for the stocks that we want to plot.
So go to Yahoo Finance and search for the companies you are interested in.
When you get to the company's listing you will see the ticker in parentheses next to the company's name.
An example of United Utilities from the Yahoo Finance page.
Once you have the list of companies you want to plot. It's time to start coding.
First we need a few imports:
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
If you don't have any of these installed. Go to your terminal and run:
pip install pandas
pip install numpy
pip install pandas-datareader
pip install matplotlib
Now let's create an array of stock objects:
stocks = [
{
'ticker': 'UU.L',
'name': 'United Utilities'
},
{
'ticker': 'VOD.L',
'name': 'Vodafone Group'
},
{
'ticker': 'BP.L',
'name': 'BP Group'
}
]
Next let's create a method to take this data and plot the data.
def create_plot(stocks):
# Create an empty dataframe
data = pd.DataFrame()
for stock in stocks:
# Create a column for the adjusted close of each stock
# Here we use the DataReader library to get the data.
data[stock['ticker']] = wb.DataReader(stock['ticker'], data_source='yahoo', start='2007-1-1')['Adj Close']
Here's what the contents of the data frame currently looks like:
You can see that adjusted close prices for each stock at present their own columns.
The problem with the current state of the data is that we can't easily plot this data as if we did, all the of the stocks would displayed on different scales.
So what we need to do is change this data to be the percentage return between each date and the first date in the series.
To do this we can use a lovely bit of pythonic code:
# Calculate the returns for all the days
returns = data.apply(lambda x: (x / x[0] * 100))
Here we are using a lambda expression to turn create a new data frame which contains the percent returns of each of the days for each of the stocks.
That's all we have to do to get and adjust the data.
Now all we have do is plot the data.
plt.figure(figsize=(10,6))
# Plot the returns
for stock in stocks:
plt.plot(returns[stock['ticker']], label=stock['name'])
# We need to call .legend() to show the legend.
plt.legend()
# Give the axes labels
plt.ylabel('Cumulative Returns %')
plt.xlabel('Time')
plt.show()
And we're done.
You can find the full code here: Full Code
I've also made a video tutorial to go alongside this tutorial.
If you have any questions or have any feedback (definitely welcome!), then please leave a comment below, or you can find me at:
Article originally posted here
Top comments (0)