yfinance Python – Complete Tutorial – 2022


If you’re into finance, crypto and stocks, then you must have felt the need for accurate (close to accuracy) market data. With Yahoo Finance, you can get your hands on the market data range of crypto, other currencies, and stocks. 

But, the question is, where to access the data successfully and implement it for our benefit? Don’t worry about it, as yfinance has got your back. With the yfinance python library, you can access Yahoo Finance market data. 

In this article, we’ll give you a complete tutorial of yfinance API to help you get the market data for successful investment or trading.

yfinance Creator Intro

Before we jump into the tutorial, let’s talk about the man behind the yfinance. On May 15, 2017, Yahoo announced the decommission of theri AP, and people felt it as a great loss and started to find a suitable alternative. 

Thanks to Ran Aroussi, who created yfinance – a fantastic API to get the market data. People loved the software because we’ve witnessed more than 300k installations a month.

Yahoo Finance API is Good or Not?

Let me be very transparent: Yahoo Finance APIs are not the perfect and official solution. Every technical software has drawbacks, and we’re going to address yfinance disadvantages so that you may decide if it’s good or bad for you. 

Some people reported that they created too many requests, and then Yahoo blacklisted. Also, if you plan on using AI for sentiment analysis, I’m afraid yfinance is not for you. 

No doubt, Yahoo finance data is still good, and if you have no issues with these limitations, then it’s best for you

Should We Use yfinance?

If you’ve chosen Yahoo finance in the above section, yfinance is the best way to get the Yahoo market data. You may find multiple paid or free APIs to access that data, but we recommend yfinance, and we have reasons: 

  1. yfinance is easy to use and simple
  2. 1-minute bar granularity
  3. You can get the data as Pandas DataFrames
yfinance Installation Guide (Step-By-Step Guide)

Before installing yfinance, you must know the packages required or compatible with yfinance. 

→You must have Python (Version 2.7 and 3.4+ or greater)

→ Pandas (Version 0.23.1 or higher)

→ Numpy (Version 1.11.1 or higher)

→ Requests (Version 2.14.2 or higher) 

→ lxml (Version 4.5.1 or higher) 

→ pandas-datareader (Version 0.4.0 or higher) 

There are two easy steps to install yfinance:

  1. Load the python 
  2. Install yfinance (Using Conda or pip)

Load your python virtual environment and follow the below steps…

Before installing yfinance, you must know the packages required or compatible with yfinance. 

→You must have Python (Version 2.7 and 3.4+ or greater)

→ Pandas (Version 0.23.1 or higher)

→ Numpy (Version 1.11.1 or higher)

→ Requests (Version 2.14.2 or higher) 

→ lxml (Version 4.5.1 or higher) 

→ pandas-datareader (Version 0.4.0 or higher) 

There are two easy steps to install yfinance:

  1. Load the python 
  2. Install yfinance (Using Conda or pip)

Load your python virtual environment and follow the below steps…

Installation With pip
Installation With Conda
yfinance Classes

Now that you have loaded yfinance, you will see a screen like:

Out of all these things, you will mainly use Ticker, Tickers and Download.

Download Historical Price Data with yfinance

We use a ticker object to download the data for one ticker and a download method for multiple tickers.

Download one ticker

To download one ticker, we create a ticker object and then get our data using that object. Below is a syntax you can use to create a ticker object:

After this, we have multiple options to get the data.

Here: 

Actions – Actions like splits and dividends

Analysis – For Revisions and EPS targets

Info – Queried data

Recommendations – Buy, sell and hold ratings

We can use the history method to get historical data. Here is how:

Now, let’s get the last 30 days of data on a daily basis. Below you can see we got the data in the pandas dataframe:

Want to get the last week’s data (minute data)?

When using minute data, remember that it should be the last 30 days.

Download Multiple Tickers

Now that you have gone through downloading a single ticker, multiple tickers are now easy for you. To download various tickers using yfinance, we must pass a list of tickers and specify group tickers by column or ticker. 

How about monthly data for Google and Facebook? (Most recent)

If you want to group by the ticker; you need to give start and end dates:

Download Fundamental Data Using yfinance

If you want to download fundamental data for tickers, you can loop through ticker objects. Let’s take a company as an example and get the fundamental data.

Here:

dhr gives a lot of data

get-methods gives fundamental data

If you want to get the company’s general; information, you can use the info method.

You can use a dictionary to access this data

To get the revenue or earnings per annum, we can use the earnings method.

We have another method in which we use financial statements to get financial ratios.

You can calculate the ratios more easily by concentrating on financial statements.

We can get time as the row (index) and column as the data field using the transpose method.

We can also get multiple symbols fundamentals by looping through the tickers.

Download Fundamentals for Multiple Ticker

To download fundamentals for multiple tickers, we have to create a list of tickers. 

Let us give you a demonstration:

We’ve got a list, and the next step is to get the list of tickers from this list.

Now, we will loop through every ticker and covert data into a list. The purpose here is to concentrate on financial data and eliminate duplicated headings.  

This financial data includes profit and loss, cash flow and so on…

Now we will iterate and dataframes using the concatenation process, re-index them, and fix the headers (duplicate) using pandas.io.parser.

So, this was the whole tutorial to get the financial information of the ticker. If you want to pull out specific data, Pandas will do that for you.

Get Options Data Using yfinance

You can also get options data with yfinance. With the help of options, you get the right to trade your underlying assets. 

We have two different methods to get the Options data: 

Ticker.options – In this method, we get options expiry date.

Ticker.option_chain – In this method, we get a finance.ticker.Options chain object. It can give you a complete chain.

Get Options Call Data

To get the Options Call data, you need to use a call. Here’s how:

Get Options Put Data

We can use options.puts for this purpose:

Get Institutional Holders Using yfinance
Why Should or Should Not Use Yahoo Finance for Live Trading?

We will give you an example for you to decide if you should use Yahoo finance for live trading or not. For this purpose, we’ll get the Facebook (Meta) data.

Meta:

Now, what’s in it for you? Fb and Meta return different data being the same company. That’s the main issue with using Yahoo Finance. 

Conclusion

Yahoo Finance is one of the best sources to get free market data (Stocks, crypto, and finance). If you have a yfinance tool, things will get easier for you. 

No doubt, yfinance is fantastic software, but Yahoo finance data is not entirely official or perfect. Still, this tool is best to do your market research and analysis in a free version. 

We’ve demonstrated everything in detail so that you may get the idea of using yfinance.

want to learn how to algo trade so you can remove all emotions from trading and automate it 100%? click below to join the free discord and then join the bootcamp to get started today.


Leave a Reply

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