Created a dashboard on Streamlit using Python for stock comparison and fundamental analysis

Stocks had reached record highs in November of 2021 with the S&P500 and NASDAQ indexes hitting 4,818 and 16,212 respectively. However, the euphoria was short-lived as the following months saw a gradual downturn in the market which is still ongoing today. Much of this can be attributed to rising interest rates, which in turn is a consequence of the worst inflation the United States has experienced in decades.

At the peak of the frothy market last year, it was tricky to judge stocks whereas in hindsight, some of the valuations didn’t make sense. There were also notions that high price-to-earnings (P/E) ratios and high price-to-sales (P/S) ratios were the new norm, driven by excessive valuations for popular tech companies such as Tesla, Shopify and Square.

Now that most of the overheated stocks have tumbled, it confirms the notion that valuations were in fact over-stretched. That is something I learned over the course of 2022.

Having understood this fact, I tried to expand my knowledge of the fundamentals of a publicly traded company. As the year progressed, I became aware of newer key metrics which allowed me to make more informed decisions. Since I was exposed to a greater number of checks, it made it easier to reject more stocks whereas it’s easy to get excited about a stock one knows very little about.

In doing so, I started to see stocks in a completely different light. The more fundamentals I took into consideration, the more I realized that there are in fact very few companies that are worth investing in. Also, knowing what numbers to look at made the process more efficient since you could weed out companies quickly.

As a result, I have massively reduced my watchlist of potential stocks to just a handful of names which I monitor closely and add to as needed. Also, as the number of metrics to keep track of increased, I needed a consolidated place to compare these stats across multiple companies.

This is where the stock comparison dashboard comes in.

The problem

Below are a few key reasons why I created this dashboard:

  • To manage and view the increasing number of valuation metrics in one place
  • To compare multiple stocks in one consolidated place
  • To create a user-friendly experience

Before creating the dashboard, I was not able to find any platform that provided this sort of detailed and elaborate information on a single page without costing a fortune.

My primary stock research platform is Yahoo Finance which presents a vast source of stock information. However, one drawback of the app is that you would often need to click on tabs under a stock to get to the metrics you want to see.

For example, if we look at the GOOG (Alphabhet Inc.) ticker on the Yahoo Finance iOS app, the only valuable metrics I can see on the opening page are market cap and P/E ratio.

To see anything further, we would need to switch tabs or click on a link. If we want to look at projected earnings growth (PEG), we will need to click on the Show all key statistics button which, to be fair, presents several of the other key metrics that one might be interested in such as:

  • P/S ratio
  • Price/Book ratio
  • Debt/Equity ratio
  • Profit margin
  • Free cash flow
  • Total cash
  • Total debt
  • Etc.

Another rather painful metric to look up on the app is revenue growth over the past few years. For that, you have to switch from the Summary to Financials tab and would then need to click on the Performance section to actually see the revenue numbers. Even then, one would have to do the math to determine the revenue growth percentage from year to year.

The Streamlit dashboard provides all the aforementioned metrics on a single page. Not only does this provide a snapshot of all the fundamentals but also allows users to draw correlations between various metrics.

An additional motivation for creating this dashboard was to view the valuation metrics for multiple companies. The Yahoo Finance app did not provide a great way to perform such a detailed comparison.

The Yahoo Finance app allowed comparison of price-related metrics such as stock price, price change, volume, day high, day low etc., but did not have an obvious way to compare valuation metrics or fundamentals which is a huge drawback.

The solution

As mentioned earlier, the ideal way to quickly get a snapshot of the financials of not only one stock but multiple comparable stocks is through an easy-to-use consolidated platform.

Hence, I created such a dashboard in Python which was deployed via a Streamlit app, accessible to anyone with a link.

Streamlit dashboard

Based on the key metrics I normally look for; I created a dashboard containing a table and a plot. The table consists mostly of percentages, ratios and basic stock information such as market cap and dividend yield. Here is the complete list of metrics:

  • Stock price
  • Market cap
  • P/S ratio
  • Trailing P/E
  • Forward P/E
  • PEG ratio
  • Price/Book ratio
  • Debt/Equity ratio
  • Trailing EPS
  • Forward EPS
  • Return on equity
  • Return on assets
  • Dividend yield

Other key metrics which represent continuous variables such as revenue, earnings and cash flow were plotted on a 9-by-9 grid. Here are all the metrics:

  • Yearly revenue
  • Revenue growth rate
  • Trailing and forward P/E
  • Yearly earnings/profits
  • Profit growth rate
  • Operating and profit margins
  • EBITDA
  • Total cash and debt
  • Operating and free cash flow

A snapshot of the dashboard is shown below. Here is a link to the app.

Note that when the page is freshly loaded, some initial data will already be displayed for ADI and INTC tickers by default. This is to serve as an example. More information on the table can be explored by scrolling to the right.

Code for this project can be found here. In addition to the Python files used to execute the Streamlit app, I have also included a Jupyter Notebook which I used as a scratchpad to test the APIs functionality. The two should be used separately.

In order to execute the code, the only change that needs to be made is including your own API key while retrieving stock data. More details regarding this process are described below.

Yahoo Finance API

The Yahoo Finance API is one of the many APIs on Rapid API that provides stock information. However, this particular one seems to have one of the most extensive sources of data and a variety of metrics.

A snippet of the API’s homepage is shown below which provides a brief description of the service and also provides usage metrics in the top-right corner of the page such as popularity, latency and service level, all of which have impressive numbers. Popularity and service level scores of 9.9/10 and 100% indicates that this is a widely used and reliable service while a latency of 965ms is by no means slow.

In order to use the API, you will need to sign up for Rapid API and subscribe to the Yahoo Finance API. Once subscribed and logged in, you can use the automatically generated API Key to perform transactions for different metrics.

An example code template for a given transaction is shown on the right-hand side panel while a list of all the available metrics is shown on the left. You can simply copy and paste the template into your own code to execute a transaction.

For a more detailed overview of Rapid API and how the APIs work, see this article in which I described how to use a different finance API. However, the functionality is similar, and the information can be used to get started with this specific API.

Challenges faced

One of the biggest challenges was reducing the amount of time to display the stock analysis once the tickers had been entered. On average, information for each stock takes an average of 7-9 seconds to extract and manipulate.

If we dig deeper, there are two key API calls required to retrieve all the relevant information:

  • stock-info
  • earnings

Individual API transaction times for each call are shown in the screen shot below. Since both calls need to be made for each stock, the total API transaction time for each ticker is around 800ms. This indicates that a majority of the computation time goes into the data manipulation and calculations.

Initially, the transaction time for each stock was greater than 15sec which defeated the purpose of having a user-friendly stock comparison app. Reducing the time to under 10sec greatly improves the user experience.

Another challenge was dealing with an obsolete version of the API. The version of the API I was originally working with had a hostname of yh-finance.rapidapi.com which worked great but since it was obsoleted, documentation and support for the API was removed from Rapid API making it risky to build an app on top of this version.

The new version has a hostname yahoo-finance97.rapidapi.com which has a well established homepage on Rapid API and can be accessed here.

Since a bulk of the original code was written using the initial API, it was a little cumbersome to port over the code to use the new API. However in the long term, this was probably the most sustainable option.

A third challenge was to decide which metrics to show on the table versus the plot. I initially had some of the ratios displayed on a plot but later realized that I myself preferred to look at these values simply as numbers in a table which made it more readable. Similarly, I had metrics such as revenue and profits in the table but later realized that they were better represented in charts.

Another key exclusion was quarterly revenue and earnings. It would have been good to include these metrics but the ones already in place were more important for fundamental analysis in my opinion. This is purely a personal preference.

Future work

One of the key improvements to focus on would be to bring the computation time down. As mentioned earlier, greater than 80% of the consumed time is due to formatting and calculations whereas only a fraction of the time was used in API transactions.

All the metrics displayed on the dashboard are by no means a comprehensive list of fundamentals but only some key ones I personally look at while making stock decisions. Hence, there could be other metrics that could explain stocks better.

In the future, additional key variables could be added to the dashboard.

Another idea could be to add information other than simply fundamentals such as the latest news or links to the company’s annual reports.

Conclusion

Hopefully, this can be a useful tool to gauge the financials of a stock and as mentioned earlier, it presents only a handful of variables required to analyze a stock. Fundamental analysis only takes the financials of a company into account whereas other quantitative and qualitative metrics are also important in valuing a stock. Some other quantitative factors could be:

  • Market size and growth opportunities
  • Customer user base and growth
  • Number of products sold per month

A few qualitative variables could be:

  • Is it a good product?
  • How is the management of the company?
  • Is the company looking for ways to expand its products or services offerings?

So you could say fundamental analysis is simply a part of the process, but a key one, in evaluating a stock.

If you have made it this far, I really appreciate it! Let me know in the comments what other financial metrics you would like to see on the dashboard? Should the table and plots be redesigned? What additional information would be good to see for each company? I would love to hear any feedback. Cheers!