In this Yahoo Finance and Python article we want to learn How to Build Financial Tools with Yahoo Finance & Python, so Yahoo Finance API is a free service that allows you to access financial data for thousands of companies. you can use this data to analyze trends, build custom financial models and make informed investment decisions.
First of all we need to install this library
1 |
pip install yfinance |
Now that you have access to financial data using Yahoo Finance API and Python, you can start building custom financial tools. these are some examples:
Real-time stock price tracker
You can build real time stock price tracker that will display the current stock price of a company. this is an example code snippet that will fetch the real time stock price of Apple Inc. (AAPL)
1 2 3 4 5 6 7 8 9 10 11 12 |
import yfinance as yf # Define ticker symbol tickerSymbol = 'AAPL' # Get data for the stock tickerData = yf.Ticker(tickerSymbol) # Get real tim stock price tickerPrice = tickerData.info['regularMarketPrice'] print('Real-time stock price for {}: ${}'.format(tickerSymbol, tickerPrice)) |
This will be the result

Historical stock price analyzer
You can build a tool that will analyze the historical stock prices of a company and display the average stock price for a given time period. this is an example code that will fetch the historical stock prices of Apple Inc. and calculate the average stock price for the year 2022:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import yfinance as yf # Defin 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='2022-1-1', end='2022-12-31') # Calculate the average stock price for the year 2022 avgPrice = tickerDf['Close'].mean() print('Average stock price for {} in 2022: ${}'.format(tickerSymbol, round(avgPrice, 2))) |
This will be the result

Investment portfolio analyzer
You can build a tool that will analyze the performance of your investment portfolio over time. this is an example code that will fetch the historical stock prices of two companies (AAPL and MSFT) and calculate the total return on a hypothetical investment portfolio:
1 2 3 4 5 6 7 8 9 10 11 12 |
import yfinance as yf # Defin ticker symbols tickerSymbols = ['AAPL', 'MSFT'] # Get the data the stocks tickerData = yf.download(tickerSymbols, start='2022-1-1', end='2022-12-31') # Calculate the total return on the investment portfolio totalReturn = ((tickerData['Adj Close'].iloc[-1] / tickerData['Adj Close'].iloc[0]) - 1) * 100 print('Total return on investment portfolio: {}%'.format(round(totalReturn, 2))) |
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