How to Use Yahoo Finance API in Python : Only 2 Steps

Yahoo Finance API Python Example

Do you want to download historical stock data for free? If yes then this tutorial is for you. In this entire post, I will show you yahoo finance API python examples step by step.

Why Yahoo Finance API?

Yahoo finance has large datasets of the historical financial dataset. It not only contains stock prices but also other calculated metrics like beta that measure the volatility of a stock compared to the volatility of the entire stock market. That’s why it is a great python module.

How to Install Yahoo Finance API?

If you are a beginner and not have installed it in your system, then you install it your pc using the pip command.

For Python 3.xx version
pip3 install yfinance

For python 2.xx verison
pip install yfinance

Step by Step Guide to using Yahoo Finance API in python

Step 1: Import all necessary python libraries.

In our example, I will use two python modules one is yfinance and pandas. Let’s import all of them.

import pandas as pd

import yfinance as yf

Step 2: Download the data from Yahoo Finance API

To download the data you have to use download() method. Inside the download method, you have to pass the tickers(stock name) and date range. Date range is not necessary but for learning purposes, I am setting a date from the last 60 days from the date of writing this post.

Execute the following code (yahoo finance python)

df_yahoo = yf.download('FB',
start='2020-09-15',
end='2020-11-15',
progress=False

 

It will start downloading the data for the Facebook Stock. Here you can see I am passing FB Facbook ticker with start and end dates. To keep compatibility with older versions, auto_adjust defaults to False when using mass-download.

Please note that you are able to download data since 1950.

The output of the above code contains time-series data with open,close e.t.c of Facebook Stock.

Facebook Stock Data
Facebook Stock Data

Other Features of Yahoo Finance API Python

The above example was a simple implementation of Yahoo Finance API. You can also do many other things using it. Some of them are.

Use of Multiple Tickers

You can also download two or more tickers simultaneously. Just pass the ticker as a list.

df_yahoo = yf.download(['FB',"AAPL"],
start='2020-09-15',
end='2020-11-15',
progress=False)

You will get Open, High, Low, Close e.t.c for each Symbol.

Download data of Multiple Tickers
Downloading data of Multiple Tickers

Removing Adjusted Close Price

If you want to remove Adjust Close Price column then you can do so by setting auto_adjust= True. Execute the below code.

df_yahoo = yf.download('FB',
start='2020-09-15',
end='2020-11-15',
progress=False,auto_adjust=True)

Output

Downloading Stock Data without Adjusted Close Price
Downloading Stock Data without Adjusted Close Price

Downloading Stocks Dividends and Stock Split

You can download dividends and splits of stock by setting actions =”inline”. Just Execute the code and see the output.

df_yahoo = yf.download('FB',
start='2020-09-15',
end='2020-11-15',
progress=False,auto_adjust=True,actions="inline")

Output

Downloading Stocks Dividends and Stock Split
Downloading Stocks Dividends and Stock Split

 

END NOTES (yfinance python)

These are the steps to use yahoo finance api in python. After downloading it you can do many manipulation on the dataframe. For example converting prices to returns, visualising time-series data e.t.c.

Hope this article has helped you in understanding using yahoo finance api python example. If  you have any queries then you can contact us.

yahoo finance API
yahoo finance API

Source:

yfinance Github

Pypi 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner