Stock price from Yahoo Finance API

Get Free Stock Data From Yahoo Finance API Using Python

There are multiple APIs for pulling stock data, and Yahoo Finance API is the most widely used API for getting free stock data using Python. This article shows how to use Python to get stock data for analysis.

Step 1: Install Yahoo Finance API Python Library

To install a Python library, run the code below in a terminal using command line.

pip install yfinance

If the package is successfully installed, the following output will be displayed at the end of the installation. The version of yfinance you see may not be exactly the same as mine depending on when you run this installation.

Successfully built yfinance
Installing collected packages: lxml, yfinance
  Found existing installation: lxml 4.2.6
    Uninstalling lxml-4.2.6:
      Successfully uninstalled lxml-4.2.6
Successfully installed lxml-4.6.3 yfinance-0.1.60

Step 2: Import Python Libraries

In this step, we will import pandas for data processing and seaborn for visualization. The Yahoo Finance API library yfinance installed in step 1 is also imported. Run the code below in your IDE.

import pandas as pd
import seaborn as sns
import yfinance as yf

Step 3: Download Data

I am interested to see the stock price change for Zoom since COVID, so Zoom’s stock data between March 2020 and June 2021 is downloaded. The downloaded data is in DataFrame format, I saved it to csv format for easy access.

The ticker for Zoom is ‘ZM’. We first check the information related to the ticker using .info .

# Get the information about the ticker for Zoom, 'ZM'
print(yf.Ticker('ZM').info)

The output is a Python dictionary with the information for Zoom.

{'zip': '95113', 'sector': 'Technology', 'fullTimeEmployees': 5059, 'longBusinessSummary': 'Zoom Video Communications, Inc. provides a video-first communications platform ......

Next we let Python know the time range for the stock data and download the data, the downloaded dataset is saved in Python DataFrame by default. We can use the “type” function to check the format of the dataset.

# Time range of the stock data
start_date="2020-03-01"
end_date="2021-06-30"

# Download data
data = yf.download("ZM", start=start_date, end=end_date)

# Check the format of the dataset
type(data)

You will receive the output from the inputed code below. The first line shows the downloading progress and the second line shows the type of the downloaded dataset, which is Pandas Dataframe.

[*********************100%***********************]  1 of 1 completed
pandas.core.frame.DataFrame

Let’s take a look at some sample data using .head(). We can see that we have the open, high, low, close, adjusted close price, and volume for each day.

# Take a look at the data
data.head()

Step 4: Save the Data as csv File

In this step, we will save the stock data into csv format for easy access.

# Save the stock data into csv format
data.to_csv('stock_zoom.csv')

Step 5: Visualization

This step includes some simple visualization to check data quality. We can see that the stock price for Zoom increases since March and reached peak in October.

# Visualize data using seaborn
sns.set(rc={'figure.figsize':(12,8)})
sns.lineplot(x=data.index, y=data['Close'])

Now let’s find out the highest and lowest closing price.

# Find the highest and lowest price within the time range
print(f"Between {start_date} and {end_date}, Zoom's lowest stock price is ${round(data['Close'].min())} and highest stock price is ${round(data['Close'].max())}")

We will get the output below.

Between 2020-03-01 and 2021-06-30, Zoom's lowest stock price is $107 and highest stock price is $568

Step 6: Putting Everything Together

# Import libraries
import pandas as pd
import seaborn as sns
import yfinance as yf

# Get the information about the ticker for Zoom, 'ZM'
zoom = yf.Ticker('ZM')
print(zoom.info)

# Time range of the stock data
start_date="2020-03-01"
end_date="2021-06-30"

# Download data
data = yf.download("ZM", start=start_date, end=end_date)

# Save the stock data into csv format
data.to_csv('stock_zoom.csv')

# Visualize data using seaborn
sns.set(rc={'figure.figsize':(12,8)})
sns.lineplot(x=data.index, y=data['Close'])

# Find the highest and lowest price within the time range
print(f"Between {start_date} and {end_date}, Zoom's lowest stock price is ${round(data['Close'].min())} and highest stock price is ${round(data['Close'].max())}")

For more information about data science and machine learning, please check out my YouTube channel and Medium Page or follow me on LinkedIn.

Leave a Comment

Your email address will not be published. Required fields are marked *