In this article we want to talk about Yahoo Finance API for Python, Yahoo Finance is popular platform that provides financial data and news to investors and traders. it offers different financial tools and information that can be accessed through its APIs. in this article we want to explore how to use the Yahoo Finance API for Python to retrieve financial data.
Yahoo Finance API for Python
First of all we need to install yahoo finance
1 |
pip install yfinance |
You can use the API to retrieve historical stock data for a particular company. this is an example of how to retrieve the data for Apple Inc. (AAPL).
1 2 3 4 5 6 |
import yfinance as yf aapl = yf.Ticker("AAPL") # get historical market data data = aapl.history(period="max") |
This will retrieve the historical stock data for Apple Inc. from its inception to the present day.
You can also use the API to retrieve live stock data for a particular company. this is an example of how to retrieve the data for Apple Inc, this code will retrieve the live stock data for Apple Inc, including the current price, volume and other information.
1 2 3 4 5 6 |
import yfinance as yf aapl = yf.Ticker("AAPL") # get live market data data = aapl.info |
You can use the API to retrieve financial statements for a particular company. this is an example of how to retrieve the income statement for Apple Inc.
1 2 3 4 5 6 |
import yfinance as yf aapl = yf.Ticker("AAPL") # get income statement income_statement = aapl.financials |
This is practical example code that retrieves the historical stock data for Apple Inc. (AAPL) using the Yahoo Finance API for Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import yfinance as yf # Define ticker symbol tickerSymbol = 'AAPL' # Get data for the stock tickerData = yf.Ticker(tickerSymbol) # Get the historical prices for this stock tickerDf = tickerData.history(period='1d', start='2010-1-1', end='2023-3-31') # Print the data print(tickerDf) |
This code imports yfinance package, after we have defined the ticker symbol for the stock (in this case, AAPL for Apple Inc.), and retrieves the historical prices for the stock from January 1, 2010 to March 31, 2023. resulting data will be printed in the terminal or command prompt, and will include information such as the opening and closing prices, high and low prices, volume and many more.
You can modify the code to retrieve different types of financial data such as live stock data or financial statements by using different functions from the yfinance package. but this example should give you a good starting point for using the Yahoo Finance API for Python.
Run the code and this will be the result

Also you can use Matplotlib to visualize the data
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import yfinance as yf import matplotlib.pyplot as plt # Define ticker symbol tickerSymbol = 'AAPL' # Get data for the stock tickerData = yf.Ticker(tickerSymbol) # Get historical prices for this stock tickerDf = tickerData.history(period='1d', start='2010-1-1', end='2023-3-31') # Calculate moving average of the closing price movingAvg = tickerDf['Close'].rolling(window=50).mean() # Plot the closing prices and moving average plt.plot(tickerDf['Close'], label='Closing Price') plt.plot(movingAvg, label='Moving Average') plt.title('Historical Stock Prices for ' + tickerSymbol) plt.xlabel('Date') plt.ylabel('Price') plt.legend() plt.show() |
This code first calculates moving average of the closing price of AAPL using window size of 50, and then plots both the closing prices and the moving average on the same graph using the plt.plot() function.
Run the code and this will be the result

Learn More on PyQt5
- How to Deploy PyQt5 Applications
- How to Integrate PyQt5 with OpenCV
- How to Use Stylesheets in PyQt5
- Learn PyQt5 Event Handling with Signals and Slots
- PyQt5 Tutorial: Creating Menus and Toolbars
- How to Open & Save Files in Python PyQt5
- PyQt5 Designer Tutorial: Complete Guide to Qt Designer
- PyQt5 vs Tkinter: Which GUI Library is Right for You
- How to Build Web Applications in PyQt5