Stock price and portfolio return visualization of S&P 500 stocks using Shiny

Posted on Jun 30, 2019

Visit Shiny App here:

URL link:


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

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!


The skills the author demonstrated here can be learned through taking Data Science with Machine Learning bootcamp with NYC Data Science Academy.

About Author

Related Articles

Leave a Comment

No comments found.

View Posts by Categories

Our Recent Popular Posts

View Posts by Tags

#python #trainwithnycdsa 2019 2020 Revenue 3-points agriculture air quality airbnb airline alcohol Alex Baransky algorithm alumni Alumni Interview Alumni Reviews Alumni Spotlight alumni story Alumnus ames dataset ames housing dataset apartment rent API Application artist aws bank loans beautiful soup Best Bootcamp Best Data Science 2019 Best Data Science Bootcamp Best Data Science Bootcamp 2020 Best Ranked Big Data Book Launch Book-Signing bootcamp Bootcamp Alumni Bootcamp Prep boston safety Bundles cake recipe California Cancer Research capstone car price Career Career Day citibike classic cars classpass clustering Coding Course Demo Course Report covid 19 credit credit card crime frequency crops D3.js data data analysis Data Analyst data analytics data for tripadvisor reviews data science Data Science Academy Data Science Bootcamp Data science jobs Data Science Reviews Data Scientist Data Scientist Jobs data visualization database Deep Learning Demo Day Discount disney dplyr drug data e-commerce economy employee employee burnout employer networking environment feature engineering Finance Financial Data Science fitness studio Flask flight delay gbm Get Hired ggplot2 googleVis Hadoop hallmark holiday movie happiness healthcare frauds higgs boson Hiring hiring partner events Hiring Partners hotels housing housing data housing predictions housing price hy-vee Income Industry Experts Injuries Instructor Blog Instructor Interview insurance italki Job Job Placement Jobs Jon Krohn JP Morgan Chase Kaggle Kickstarter las vegas airport lasso regression Lead Data Scienctist Lead Data Scientist leaflet league linear regression Logistic Regression machine learning Maps market matplotlib Medical Research Meet the team meetup methal health miami beach movie music Napoli NBA netflix Networking neural network Neural networks New Courses NHL nlp NYC NYC Data Science nyc data science academy NYC Open Data nyc property NYCDSA NYCDSA Alumni Online Online Bootcamp Online Training Open Data painter pandas Part-time performance phoenix pollutants Portfolio Development precision measurement prediction Prework Programming public safety PwC python Python Data Analysis python machine learning python scrapy python web scraping python webscraping Python Workshop R R Data Analysis R language R Programming R Shiny r studio R Visualization R Workshop R-bloggers random forest Ranking recommendation recommendation system regression Remote remote data science bootcamp Scrapy scrapy visualization seaborn seafood type Selenium sentiment analysis sentiment classification Shiny Shiny Dashboard Spark Special Special Summer Sports statistics streaming Student Interview Student Showcase SVM Switchup Tableau teachers team team performance TensorFlow Testimonial tf-idf Top Data Science Bootcamp Top manufacturing companies Transfers tweets twitter videos visualization wallstreet wallstreetbets web scraping Weekend Course What to expect whiskey whiskeyadvocate wildfire word cloud word2vec XGBoost yelp youtube trending ZORI