Yahoo Finance is a popular source for stock market data, and the yfinance library provides reliable access to its unofficial API. The library allows users to fetch historical prices, fundamental data, and options chain data for stocks. Key functionalities include retrieving OHLCV data, transforming data structures for convenience, and utilizing various parameters for data requests. Additionally, users can plot candlestick charts and customize bar sizes using pandas. The article emphasizes the ease of use and versatility of yfinance for backtesting and research.
Yahoo Finance is definitely one of the most popular sources of stock market data available for retail traders, and is as such the first data source that most of us used back when we got started. It is also worth mentioning that its ease of use and array of datasets allow for quick prototyping, making it a valuable source for backtesting research ideas and implementing very basic strategies.
Although there are more than a few Python libraries that enable coders to have programmatic access to the unofficial API of Yahoo Finance, yfinance stands out as one of the oldest, most reliable, and actively used libraries out there.
In this article, I’ll cover the most useful aspects of this library, and I’ll even go into some detail describing its parameters.
Before getting started, it’s worth mentioning that I recommend using BacktestXL if you’re not a proficient Python programmer. BacktestXL is a backtesting engine that works natively in Microsoft Excel, and its free plan allows users to fetch historical stock prices without a single line of code. Disclaimer: we’re the developers, but it really is the software we use for our personal research!
Installing yfinance
Assuming that you followed the previous steps, the remainder of the installation is trivial. You just need to open a terminal and write the following command:
pip install yfinance
Fetch Historical Prices using yfinance
Fetch fundamental data using yfinance
Fetch Options Chain Data from Yahoo Finance
Tips and tricks using yfinance
- Plotting a Candlestick Chart using yfinance and finplot
A common task of price data is plotting it using candlesticks, which is very straightforward if we use yfinance in addition to Finplot. The following snippet is a working example that retrieved the chart above.
# IMPORT REQUIRED LIBRARIES
# YOU SHOULD MAKE SURE THAT YOU ALREADY INSTLALED THEM PREVIOUSLY
import yfinance as yf
import finplot as fplt
# CREATE A TICKER INSTANCE FOR TESLA
tsla = yf.Ticker('TSLA')
# RETRIEVE 1 YEAR WORTH OF DAILY DATA OF TESLA
df = tsla.history(interval='1d',period='1y')
# PLOT THE OHLC CANDLE CHART
fplt.candlestick_ochl(df[['Open','Close','High','Low']])
fplt.show()

- Change OHLC bar size with yfinance and pandas
Another common issue arises when we need to change the bar size of the data retrieved by Yahoo. We might be interested in creating bar data with a custom size that is commonly not used, such as 45 minutes. In order to do so, we have to group smaller size bars into 45-minute candles. We could use 1, 5, or 15-minute bars, 15 being the most convenient value since it allows us to fetch more data.
The following script fetches 60 days’ worth of 15-minute bar data of Tesla:
import yfinance as yf
import finplot as fplt
tsla = yf.Ticker('TSLA')
df = tsla.history(interval='15m',period='60d')
print(df)

And the following script transforms said data into 45-minute bars:
import pandas as pd
df_45min = df.groupby(pd.Grouper(freq='45Min')).agg({"Open": "first",
"High": "max",
"Low": "min",
"Close": "min",
"Volume": "sum"})
By using the pandas groupby function, we can aggregate data according to our specific needs. OHLC requires different groupings since the Open is the first price of every period, High is its maximum price, Low is its minimum price, Close is the last price, and Volume requires adding all values of the column. The result of that function looks as follows:

Notice that in regrouping the DataFrame, we got rid of the Data Splits and Dividends columns.