![]() |
VOOZH | about |
Yahoo Finance provides access to financial data which including stock prices, company financials and key metrics. By using the yfinance Python library we can easily retrieve this data and analyze it for various financial tasks. In this article, we'll see how to use Python to get financial data from Yahoo Finance and display it in a structured format.
Before we start we need to install the yfinance library. We can install it by using pip command:
pip install yfinance
For fetching financial data, we need the ticker symbol of the company. A ticker is a unique series of letters assigned to a security for trading purposes. For example:
Below are various examples that shows how to retrieve Financial Data from Yahoo Finance. Let us take the results for Meta hence we use the "META" ticker.
Getting basic financial information about Meta we can use .info() function.
Output:
{'address1': '1 Meta Way', 'city': 'Menlo Park', 'state': 'CA', 'zip': '94025', 'country': 'United States',-------'trailingPegRatio': 0.7646}
We can fetch financial key metrics like Company Sector, Price Earnings Ratio and Company Beta from the above dictionary of items quickly.
Output :
Company Sector: Communication Services
P/E Ratio: 20.967308
Company Beta: 1.279
Though we have fetched few financial key metrics in a dictionary format we can split that by using key-value pair.
Output :
To get the historical market data such as stock prices we can use the history() method. Fetching Metaβs stock prices for the maximum available period and displaying it in a tablular format.
Output :
If we want to display all the rows of a ticker symbol we will use Pandas function and set the set_option() function to display maximum rows.
Output:
We can specify a custom date range to fetch historical data. Hereβs we used for Meta to get information from May 31, 2019 to January 30, 2021.
Output:
We can visualize the closing prices using matplotlib library.
Output:
Whether we are tracking stock prices, analyzing market trends or finding company financials these methods will provide us flexibility and tools need to perform in-depth analysis and valuable insights.