In this article we want to learn How to Get Financial Data from Yahoo with Python, so for this we are going to use Yahoo Finance API, now first let’s talk about that.
What is Yahoo Finance for Python?
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.
Install Yahoo Finance API for Python
You can use pip for the installation of Yahoo Finance API, open your command prompt or terminal and run this command.
1 |
pip install yfinance |
Real-time stock price tracker in Python
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 with Python
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 with Python
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
FAQs:
What is Yahoo Finance for Python?
Yahoo Finance for Python refers to the Yahoo Finance API, it is a free service that provides access to financial data for thousands of companies. It allows users to analyze trends, build custom financial models and make informed investment decisions based on real-time and historical data.
How do I install Yahoo Finance API for Python?
You can easily install the Yahoo Finance API for Python using pip. Simply open your command prompt or terminal and execute the following command:
1 |
pip install yfinance |
Subscribe and Get Free Video Courses & Articles in your Email