Retrieving & Visualising Data from Yahoo Finance in Python

How do you take data from Yahoo Finance and view it on Python?

The Derivative Tester
4 min readFeb 12, 2022
Photo by fabio on Unsplash

In this post, I am going to just run through what I have learned while using Python to try to retrieve and visualize stock data. Instead of doing this like a tutorial, I’ll just go through the steps I used after extensively questioning my best friend (aka Google). I’ll run through:

  1. How to retrieve the data
  2. How to visualize the data
  3. How to play around with the data

What tools & packages did I use?

Instead of going through exactly how to install and where I found all these tools, I’ll provide useful links and also tell you exactly what was used so you can just Google them in your own time as and when you find them useful.

I worked mainly out of Jupyter Notebook in Anaconda Navigator. Beyond the typical packages, I also installed (or maybe they were pre-installed):

pip install numpy
pip install pandas
pip install matplotlib
pip install yahoo-finance
pip install seaborn

I used numpy in case I need mathematical operations, pandas for working with data frames and datasets, matplotlib to visualize the data, yahoo-finance to draw data from Yahoo’s database, and seaborn to make my graphs look prettier. I imported them as the following:

Getting Stock Information:

To retrieve information from Yahoo Finance, you can specify the ticker using the yfinance package (for more information about the commands you can refer to yfinance Github repo). Using the following example, we will retrieve the information about Tesla.

Using tsla, we can then pull information on Tesla stock. As an example, look below for how I pulled the entire stock price history for Tesla in Yahoo and print it. The code will give you the following details:

Beyond this, we can also pull general information on the stock. Below I show 2 examples of me pulling the Forward PE Ratio and Beta of Tesla.

Visualize The Stock Price Data:

Moving on from the above, we want to use the data to visualize stock prices. I’ve recreated everything from scratch and downloaded all Tesla data from Yahoo Finance. FYI I used plotly instead of matplotlib & seaborn for you to be able to interact with the data here.

From here we can start to plot out the data for Tesla. In this case, I will plot Tesla’s closing price as far back as the data allows us and color the chart green.

Let’s go one step further. Below, I’ve created an area graph that shows the daily volume traded for Tesla’s stock using the same data set, defining the area and line for Volume in Red.

Pretty simple and fun to play with these. Before we close off, let us go a bit more into the raw data….

Playing Around With The Data:

What if I want to see the percentage change of Tesla’s stock price since the data was available? We shall reuse the original Tesla closing price dataset and divide the entire set of data by the first closing price. Following that, we will store that data under tsla_pctchange_inception.

Now, we have a dataset that we can use to graph. Let’s see how many times Tesla’s stock has multiplied by:

Looks like at its peak, Tesla stock multiplied by more than 250 times from its inception price.

This is just a nice good bit of fun but hopefully, this is a somewhat useful catalog for visualizing stock data and doing basic wrangling of data.

Stay tuned and I will document how I then use the data to create something simple to backtest options with.

For those who have not, please read the background and disclaimers here.

--

--