Do you want to download historial stocks data for free. If yes then this tutorial is for you. In this entire post I will show you yahoo finance api python example through step by step.
Why Yahoo Finance API ?
Yahoo finance have large datasets of historical financial dataset. It not only contains stocks prices but also other calculated metrics like beta that measures the voltality of a stock compare to the volatality of the entire stock market. Thats why it is great python module.
How to Install Yahoo Finance API ?
If you are beginners and not have installed it in your system, then you install it you 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 use 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. Lets 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 purpose I am setting date from last 60 days from the date of writing this post.
Execute the following code.
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.
Other Features of Yahoo Finance API Python
The above example was 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 ticker 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.
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)
Downloading Stocks Dividends and Stock Split
You can dowload dividend 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")
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.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.