Yfinance is a python package that enables us to fetch historical market data from Yahoo Finance API in a Pythonic way. It becomes so easy for all the Python developers to get data with the help of yfinance.
We can easily download historical stock data from yfinance, but the problem is, it is very time taking. Hence, we use multithreading for covering up the time. Multithreading enables us to download large amounts of data by executing multiple threads concurrently.
Installation
This module does not come built-in with Python. To install it type the below command in the terminal.
pip install yfinance
Let’s see the process step by step which are explained below with the implementation:
Step 1: Get all the required modules
Step 2: Getting stocks history data
Python
import yfinance as yf
msft = yf.Ticker( 'MSFT' )
data = msft.history()
print (data)
|
Output:

This is a recent year’s stock data of Microsoft Corporation. Please note that if we use data = msft.history(period=’max’) it will return all data till now.
Step 3: Downloading the data
Python3
import yfinance as yf
data = yf.download(
tickers = 'MSFT' ,
)
print (data)
|
Output:

Hence, 1 of 1 download is completed. It is all data from beginning to end till now of only one company.
Step 4: Download the data from several companies
This is a code for downloading the data from companies like IBM, Apple (AAPL), Amazon(AMZN), Microsoft(MSFT), etc.
Python3
import yfinance as yf
ticker_list = [ 'IBM' , 'MSFT' , 'AAPL' , 'AMZN' ]
data = yf.download(
tickers = ticker_list,
group_by = 'ticker' ,
)
data = data.T
for t in ticker_list:
print (t)
print ( '\n' )
print (data.loc[t])
print ( '\n' )
|
Output:

Step 5: Calculating the time of execution and using multithreading
As yfinance uses its own built-in threading technique for mass downloading. To do this, We need to assign a new parameter in a yf.download.
data = yf.download(
tickers=ticker_list,
threads=True, # Set thread value to true
group_by=’ticker’,
)
Program for calculating time, we use the time module
Python
import yfinance as yf
import time
start = time.time()
ticker_list = [ 'IBM' , 'MSFT' , 'AAPL' , 'AMZN' ]
data = yf.download(
tickers = ticker_list,
threads = True ,
group_by = 'ticker' ,
)
data = data.T
for t in ticker_list:
print (t)
print (data.loc[t])
print ( 'The program takes ' , time.time() - start, 'seconds.' )
|
Before thread:

After thread:
