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.
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
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
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.
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(
threads=True, # Set tread value to true
Program for calculating time, we use the time module
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course