Stock Market Dashboard: Data Visualization with Streamlit in Python
- sherry salek
- Jul 6, 2022
- 4 min read
Updated: Jul 16, 2022
What is Stock Market?
According to Yahoo Finance, the term stock market refers to several exchanges in which shares of publicly held companies are bought and sold. If a stock is in high demand and low in supply i.e. more people want to buy it and fewer people are willing to sell it then the price for the stock will go up and similarly if the stock is in low demand and high on supply which means people more people are ready to sell it but fewer people are willing to buy it then its prices go down. You can check my dashboard and check some stock prices and my github codes.
Stock Market Data in Python:
One of the first sources from which you can get historical daily price-volume stock market data is Yahoo finance. You can use yfinance module to get the data in python.
How to set up the environment?
We used Anaconda Prompt which is similar to a terminal or command prompt (cmd). First, we created folders related to our project and installed Pandas. Then we created Finance environment in python.

Libraries used
The following are the libraries required to be installed beforehand which can easily be downloaded with the help of the pip function. A brief description of the Library’s name and its application is provided below:
Library | Application |
Yahoo Finance | To download stock data |
Streamlit | Open-source app framework to explore and understand data via dashboard |
Pandas | To handle data frames in python |
Spyder | A free and open source scientific environment |



Then we fired up our spyder:

Now we imported the libraries in our spyder and saved our python file in our directory:
Data description
We have downloaded the daily stock prices data using the Yahoo finance API functionality. The period is between January 1, 2021 to July 15, 2022 data capturing Open, High, Low, Close, and Volume.
Open: The price of the stock when the market opens in the morning
Close: The price of the stock when the market closed in the evening
Adj Close: While closing price merely refers to the cost of shares at the end of the day, the adjusted closing price considers other factors like dividends, stock splits, and new stock offerings. Since the adjusted closing price begins where the closing price ends, it can be called a more accurate measure of stocks' value
High: Highest price the stock reached during that day
Low: Lowest price the stock is traded on that day
Volume: The total amount of stocks traded on that day
Here, we will take the Example of six companies: Netflix (NFLX), Nvidia (NVDA), Amazon (AMZN), Tesla (TSLA), Apple (AAPL), Bitcoin USD (BTC-USD).
We checked our dashboard by opening another prompt:

And then, the following code in Spyder Console and made sure to interrupt or stop the kernel when we are done:

To define our data, we downloaded the data. We used the condition statement and the len() function to return the number of characters in the string. It checks for our condition, if the condition is true, then the set of code present inside the ” if ” block will be executed otherwise not. To download the yf data, we set the stock list to our dropdown menu and specify the start and end. We also need the adjusted close price and we can reference the column in the DataFrame.
Visualizing the Stock Data
We created line char with streamlit. We saved our file and ran our streamlit application again. However, comparing the stocks does not make sense, since they have different scales.
In order to be able to compare different stocks, we defined a function (relative returns) and we define the percentage change of the absolute prices with pct_change() and then we use cumulative product function cumprod() to cumulative returns over our data (Basically we add 1 to our return and multiply by cumulative return on the day before plus 1 and then minus 1. To demonstrate in excel, look at the following picture:

We used fillna(0), since cumulative return on the very first day is 0 and we do not have the price the day before the first day, we start from day 2.
We created our data frame again by calling the relative return function and take the rest of our previous argument function. We also add a header to our graph to make it more comprehensible.
Adjusted close price visualization:

Conclusion
The above analysis can be used to understand six companies' stock behavior over one year. The line graph shows changes in the accumulative returns of the companies stock adjusted closed price from Jan 01, 2021 to Jan 01, 2022 by using streamlit library in python. Overall, what stands out from the graph is that there was a considerable upward trend in the stock price of NVDA over the past year, however there has been a slight fall since November 2021.
Looking at the details for BTC-USD, there has been lots of fluctuations throughout the past year. The graph started at upward trend in the beginning of 2021, reached in its maximum in April 2021 and went down significantly to its minimum in July 2021. Again, it showed an upward trend with lots of fluctuations to reach its highest peak in Nov 2021. Since then, it collapsed again till Jan 2022.
Turning to other stocks, they rose the most stably and steadily, over the one-year period. Meanwhile, the stock price for 'TSLA' went down gently in the first half-year period (from 0 value to -0.21), followed by an upswing trend to 0.67 in Nov 2021, before eventually showed gradual fall to the beginning of 2022.
Further studies can be done to decide which stock to pick from these six companies for low-risk low gain or high-risk high gain depending on the risk capacity of the investor.
Comments