Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (2024)

comments

Introduction


Stock market analysis and good investing (for long-term growth) requirecareful examination of the financial data. Variousmetrics and ratiosare often used in such analysis i.e. toassess the inherent quality of a stock. You may have heard about some of them in the talk from financial and investment experts.

For example, thePrice-over-Earning ratioorPE ratio. It is the ratio of the share price over the annual earnings/share.

Or, theBook value per share. It takes the ratio of a firm’s common equity divided by its number of shares outstanding. When a stock is undervalued, it will have a higher book value per share in relation to its current stock price in the market.


Assessing a Stock's Future With the Price-to-Earnings Ratio and PEG
The price-to-earnings ratio (P/E) is one of the most widely used metrics for investors and analysts to determine stock…

Often such data are available from websites like Yahoo Finance. However, unless, you are using some kind of paid, registered service, you cannot download or scrape the data programmatically.


Yahoo Finance - Stock Market Live, Quotes, Business & Finance News
At Yahoo Finance, you get free stock quotes, up-to-date news, portfolio management resources, international market…

However, many microservices exist which provide such data over a simple API call. To take advantage of that, we show, in this article,how to write a simple Python class script for interfacing with afinancial data microservice.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (2)

Image source: Screen capture by the author (Website)

With this Python class, you can pull data and build a Pandas DataFrame with almost all important financial metrics and ratios by invoking a series of simple methods.

We also provide simple charting methods (bar chart and scatter plots) for analyzing the data graphically.

Note, that, you need to obtain your own secret API key (free) from the website and register it after instantiating the class object.

With that being said, let us examine the Python package/class and the various methods that come with it.

The Python class and various built-in methods


The core Python class is availablehere on my Github repo. Feel free to star and fork the repo and improve upon it. You can simply clone the repository, and start using the script in your own Notebook.

> mkdir My_project> cd My_Project> git clone https://github.com/tirthajyoti/Finance-with-Python.git> cd financeAPI  

To keep the code clean, in this article, we show the use of the class in a test Jupyter notebook.

We start by importing regular libraries and the class object.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (3)

Read the secret API key from a file and register it


Note that you need to have a file calledSecret_Key.txtin the same directory as the code files. Without it, you cannot progress.

Register here:https://financialmodelingprep.com/login

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (4)

Create a class instance


Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (5)

It has a description


Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (6)

We cannot access the data without registering the key


We definitely want to start pulling the data now. Let’s say we want to build a data dictionary for the company Apple (with the ticker symbol ‘AAPL’). We can try it but we won’t be successful because we have not registered the secret key with the class object yet.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (7)

So, we register the secret key


Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (8)

Let us build a data dictionary now


For all the methods in this class, we have to pass on the ticker symbol of the company (on the US financial market). For Apple Inc, it is ‘AAPL’.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (9)

If we examine this dictionary, we will note that a huge amount of data has been pulled from the API endpoint. A partial screenshot is provided below.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (10)

Build a DataFrame with multiple companies’ data


Working with Python dictionaries is fine but for large-scale data analytics, we should think of building a Pandas DataFrame. We provide a built-in method to do just that. Building a DataFrame is as easy as passing on a list of ticker symbols and the code does all the data scraping and structuring job for you.

Let’s say we want to download all the financial data for the following companies,

  • Twitter
  • Facebook
  • Microsoft
  • Nvidia
  • Apple
  • Salesforce

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (11)

A nicely formatted DataFrame is ready for your use!

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (12)

What kind of data is available anyway?


We can easily examine the kind of data that has been pulled from the API service. Note, we pass on an argument ‘profile’, ‘metrics’ or ‘ration’, and get back the list of corresponding data items.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (13)

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (14)

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (15)

On to plotting — visual analytics


In the package, we have included code for simple visual analytics with the data.

It is often helpful to examine various metrics and financial ratios in simple bar charts. To do that, just pass on the name of the variable you want to be plotted. You can also include usual Matplotlib keyword arguments such as color and transparency (alpha).

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (16)

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (17)

You can also plot simple scatter plots to visually analyze inter-relationships between financial metrics.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (18)

You can also pass on a third variable to be used for scaling the size of the markers in the scatter plot. This helps, in an indirect way, visualize more than two variables in a 2-dimensional plot. For example, we pass on the share price parameter as the third variable in the code below.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (19)

Custom analysis with the underlying DataFrame


Often, investors may want to create their own filters and investment logic with the available data.

For example, we may want to consider only those companies with market capitalization > 200 billion USD and then look at the metric ofEnterprise value over EBIDTAin a bar chart.

We can access the underlying DataFrame, create a custom DataFrame, and then assign this custom DataFrame to a newfinanceAPI()object to take advantage of the ready-made charting methods.

In this way, we won't need to request data from the API again.We should avoid the data read as much as possible because of the limitation of the number of data read with the free API key.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (20)

And then we plot a bar chart based on this custom DataFrame (embedded in the custom class object).

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (21)

Summary


We show the implementation and usage of a simple Python class/package that can be used to pull a broad range of financial metrics and ratios from a microservice.

Registration with the service is required to obtain a Free API key for this. The usage is, of course, limited in terms of the number of data read for the free account. The website also haspaid plans for unlimited usage.

Note that this work focuses onstatic financial data, as available in the annual financial statements, and not on the dynamic time-series share market pricing data.It should be used for gauging the long-term financial strength of a firm for growth-oriented investing.

As a data scientist, often you may need to write custom class/packages to pull and analyze data from microservice APIs, and hopefully, this article (and the associated codebase) can give you a simple yet effective introduction toward that knowledge.

Also, you can check the author’sGitHubrepositoriesfor code, ideas, and resources in machine learning and data science. If you are, like me, passionate about AI/machine learning/data science, please feel free toadd me on LinkedInorfollow me on Twitter.


Original. Reposted with permission.

Related:

  • Time Series Classification Synthetic vs Real Financial Time Series
  • Four Ways to Apply NLP in Financial Services

More On This Topic

  • Building Multimodal Models: Using the widedeep Pytorch package
  • Building a Structured Financial Newsfeed Using Python, SpaCy and Streamlit
  • How to Package and Distribute Machine Learning Models with MLFlow
  • Analyze Python Code in Jupyter Notebooks
  • The Data Matters: Choosing the right data to analyze can make or…
  • Drag, Drop, Analyze: The Rise of No-Code Data Science

I am an expert in financial data analysis and programming, with a demonstrable depth of knowledge in the field. My expertise is evident in the various financial metrics and ratios discussed in the article, as well as the Python class script provided for interfacing with a financial data microservice.

In the article, the importance of stock market analysis and investing for long-term growth is highlighted. The focus is on examining financial data, and specific metrics and ratios are discussed, such as the Price-over-Earnings ratio (PE ratio) and Book Value per Share. These metrics are essential for assessing the inherent quality of a stock and determining whether it is undervalued.

The article also mentions the use of data from websites like Yahoo Finance, emphasizing the significance of obtaining financial information to make informed investment decisions. However, it acknowledges that programmatically downloading or scraping such data may require a paid, registered service.

To address this limitation, the article introduces a Python class script available on the author's GitHub repository. This script allows users to pull financial data from a microservice through a simple API call. The script includes methods for building a Pandas DataFrame with important financial metrics and ratios. Additionally, it provides charting methods for visual analysis, such as bar charts and scatter plots.

The article guides readers on how to use the Python class script by importing regular libraries, reading a secret API key from a file, and registering the key. It then demonstrates the process of pulling data for a specific company, in this case, Apple (ticker symbol 'AAPL'). The article further explains how to build a DataFrame with data for multiple companies, such as Twitter, Facebook, Microsoft, Nvidia, Apple, and Salesforce.

The script's capabilities are extended to custom analysis, where investors can create their own filters and investment logic using the available data. For instance, the article illustrates filtering companies with a market capitalization greater than $200 billion and visualizing the Enterprise Value over EBITDA metric in a bar chart.

In summary, the article provides a comprehensive overview of financial data analysis, introduces a practical Python class script for accessing financial data through a microservice, and demonstrates its usage for building DataFrames and conducting visual analysis. The emphasis is on static financial data for assessing the long-term financial strength of a firm in the context of growth-oriented investing.

Pull and Analyze Financial Data Using a Simple Python Package - KDnuggets (2024)
Top Articles
Latest Posts
Article information

Author: Pres. Lawanda Wiegand

Last Updated:

Views: 5938

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Pres. Lawanda Wiegand

Birthday: 1993-01-10

Address: Suite 391 6963 Ullrich Shore, Bellefort, WI 01350-7893

Phone: +6806610432415

Job: Dynamic Manufacturing Assistant

Hobby: amateur radio, Taekwondo, Wood carving, Parkour, Skateboarding, Running, Rafting

Introduction: My name is Pres. Lawanda Wiegand, I am a inquisitive, helpful, glamorous, cheerful, open, clever, innocent person who loves writing and wants to share my knowledge and understanding with you.