Stock price and portfolio return visualization of S&P 500 stocks using Shiny
Visit Shiny App here:
URL link: https://jessiec006.shinyapps.io/StockVisualization0630/
Coming from a finance/accounting background and have worked as a transaction service advisor for publicly traded firms for 3 years, I have always been intrigued by the depth of information the seemingly “plain and simple” stock price can convey to informed audience.
For this project, I build a Shiny app that serves as a useful and informational visualization tool that helps user to visualize (1) historical stock price, (2) stock price forecast using traditional time -series predicting models, (3) stock trading volume heatmap and correlation analysis, and (4) stock portfolio return simulation. This blog is divided into such 4 sections as well.
Further note before we begin:
Dataset: daily stock price (low, high, open, close) and trading volume of all stocks on S&P 500 from February 2013 to February 2018 (5 years). Available on Kaggle at https://www.kaggle.com/camnugent/sandp500.
Dynamic listing: S&P 500 is a dynamic index that includes 500 companies with the largest market capitalizations listed on the NYSE and NASDAQ. This means that certain companies will leave and certain companies will join this index during the 5-year time span. This extra feature will bring interesting twists and dynamics to our visualization as I will demonstrate later.
Part 1: Stock price historical trend
In this section, users are able to input the ticker of the company of their choice, select the range of dates & type(s) of price (open price / close price / high price / low price). The app will display stock price over time using line chart, histogram, and bar chart to visualize the historical trend of stock price during the chosen time period. The metrics at the bottom allows users to understand how many days were the chosen stock listed on S&P 500 (particularly useful for those stocks that have exited/entered the index), the maximum close price, minimum close price, and average close price the during the chosen time period.
Part 2: Stock price forecasting models
In this section, users are able to input the ticker of the company of their choice, and visualize how accurate classical stock price time-series forecasting models predict the daily stock price from March 2017 to February 2018, using the historical daily price from February 2013 to March 2017 as training data.
Here, three forecasting models are included: (a) ARIMA (Autoregressive Integrated Moving Average), (b) Exponential Smoothing, and (c) Holt Winters Exponential Smoothing. For more information on the more complex theories and principles behind each of these statistical models, please refer to here.
Accuracy measurements, which include ME, MAE, RMSE, and MPE, are included at the bottom of the graph.
Part 3: Stock trading volume heatmap and correlation
In this section, users are able to visualize the historical monthly trading volume ratio of stocks that have been listed on S&P 500 during the 5-year time horizon.
Monthly trading volume heat ratio is calculated as the total trading volume of a given stock divided by the total trading volume of all S&P500 stocks in a given particular month.
Users can pick their desired date range as well as the stock ticker of their choice. If certain stock was not listed on S&P 500 during the selected date range (for example, Hilton [HLT] was not listed on S&P500 until 2017), the heatmap will automatically grey out the date where it was not listed. The heatmap allows users to visualize how the trading volume of stocks fluctuates over time (relative to the entire S&P 500), and how correlated the trading volume of stocks they choose is.
Part 4: Stock portfolio return simulation
In this section, users are able to build a portfolio consisting of 5 stocks of their choice, and customize weights assigned to each stock. Weights have to add up to 100%. Users are also able to select their desired date range.
The app will display the movement of the hypothetical portfolio return of the 5 stocks during the chosen time frame. Note that, if any stock(s) was not present on S&P 500 during the chosen date range, I have built in an algorithm to redistribute the weight originally assigned to that stock(s) by users equally to the rest of the stocks.
I hope you enjoy reading this post and navigating through this app. Please do not hesitate to reach out to me at [email protected] for any comments and suggestions. Feedback is greatly appreciated!