Monitoring Stock Performance made easy with R and Shiny (2024)

Monitoring Stock Performance made easy with R and Shiny (1)

Comparing the performance of many stocks in a single visualization can be time consuming. It is particularly tedious if you want to do this over and over again. With the help of R and Shiny, you can easily create and track a stock portfolio to see how individual stocks perform over time — all in one interactive visualization.

In this article, we will go through the process step by step and do the following:

  1. Gather stock closing prices for a given time period
  2. Create a simple user interface allowing the user to select the data to be displayed
  3. Define server functions

Let’s first load the R packages that we’ll be using in our app. You will need to install them on your system using the install.packages() function.

library(shiny)
library(shinyWidgets)
library(shinythemes)
library(plotly)
library(tidyverse)
library(tidyquant)

We first define our portfolio’s ticker symbols and two stock indices as benchmarks. Then, we use the tq_get() function from the tidyquant package to get stock (closing) prices from Yahoo Finance. The to and from arguments are used to specify the desired date range.

tickers <- c("GRVY","SE","PLTR","U","NET","SNOW","MDB")benchmarks <- c("^NDX","^GSPC") # Nasdaq100 and SP500prices <- tq_get(tickers, 
get = "stock.prices",
from = today()-months(12),
to = today(),
complete_cases = F) %>%
select(symbol,date,close)
bench <- tq_get(benchmarks,
get = "stock.prices",
from = today()-months(12),
to = today()) %>%
select(symbol,date,close)

The code specifying the user interface will be wrapped in the fluidPage function and saved in a variable that we callui . This is demonstrated at the bottom of the article where we will put it all together.

Our app will have three main UI components:

  1. A title panel
  2. A sidebar where users can select and filter the data
  3. A main panel for visualizing the data

The Title Panel

This one is easy. We simple write the following:

titlePanel("My Tech Stock Portfolio")

The Sidebar

This part requires a bit more work, since this is where we specify what the user can do. In our app, we will need some input components that allow the user to pick and choose between stocks, the desired time range and whether or not to include a benchmark. Please have a look at the bottom of the article to see how we put the individual components together in our sidebar.

First, a pickerInput lets the user pick between stocks and select/deselect all. All are selected by default using the selected argument. Importantly, we specify the inputId in order to reference the selected stocks in our server logic.

pickerInput(
inputId = "stocks",
label = h4("Stocks"),
choices = c(
"Gravity" = tickers[1],
"Sea Limited" = tickers[2],
"Palantir" = tickers[3],
"Unity" = tickers[4],
"Cloudflare" = tickers[5],
"Snowflake" = tickers[6],
"MongoDB" = tickers[7]),
selected = tickers,
options = list(`actions-box` = TRUE),
multiple = T
)
Monitoring Stock Performance made easy with R and Shiny (4)

Then, radioButtons() provides different time ranges to choose from, as well as whether to include a benchmark index in the visualization.

# Time range
radioButtons(
inputId = "period",
label = h4("Period"),
choices = list("1 month" = 1, "3 months" = 2, "6 months" = 3, "12 months" = 4, "YTD" = 5),
selected = 4
)
# Benchmark
radioButtons(
inputId = "benchmark",
label = h4("Benchmark"),
choices = list("SP500" = 1, "Nasdaq100" = 2,"None" = 3),
selected = 3)
Monitoring Stock Performance made easy with R and Shiny (5)

The Main panel

This is where the plot goes. In our case, this is pretty straightforward.

mainPanel(plotlyOutput("plot", height=800))

Based on the user input, we need to specify how our app should behave. This is all done in the server part of our code. Because we want our app to be responsive and modify the visualization according user input, we wrap the logic to filter the data inside observeEvent .

For the sake of brevity, not all the input logic is included in the code chunk below. The complete code for this part is provided at the end.

observeEvent(c(input$period,input$stocks,input$benchmark), {

# filter stock symbols
prices <- prices %>%
filter(symbol %in% input$stocks)

# filter time period (example)
if (input$period == 1) {
prices <- prices %>%
filter(
date >= today()-months(1)) }
# filter benchmark (example)
if (input$benchmark == 1) {
bench <- bench %>%
filter(symbol=="^GSPC",
date >= min(prices$date))
prices <- rbind(prices,bench) }
# add more server logic here..
})

Finally, we can plot our data combining ggplot2 and plotly . In order to better compare our stocks, we also need to recalculate their price levels to index base 100.

output$plot <- renderPlotly({
print(
ggplotly(prices %>%
# index 100
group_by(symbol) %>%
mutate(init_close = if_else(
date == min(date), close,NA_real_)) %>%
mutate(
value = round(100 * close / sum(init_close, na.rm=T),1)) %>%
ungroup() %>%
ggplot(aes(date, value, colour = symbol)) +
geom_line(size = 1, alpha = .9) +
#uncomment line below to show area under curves
#geom_area(aes(fill = symbol),position="identity",alpha=.2) +
theme_minimal(base_size=16) +
theme(axis.title = element_blank(),
plot.background = element_rect(fill = "black"),
panel.background = element_rect(fill = "black"),
panel.grid = element_blank(),
legend.text = element_text(colour = "white"))
)
)
})

That’s it!

We’ve walked through pretty much everything we need to create our app. Now, we only need to put the pieces together. Below is the full code for our shiny new stock portfolio app!

You can check out the app here

As you can see, I’ve chosen a dark theme supplied by the shinythemes package.

Monitoring Stock Performance made easy with R and Shiny (6)
Monitoring Stock Performance made easy with R and Shiny (2024)

FAQs

How do you summarize a stock performance? ›

Price-to-earnings ratio (P/E): Calculated by dividing the current price of a stock by its EPS, the P/E ratio is a commonly quoted measure of stock value. In a nutshell, P/E tells you how much investors are paying for a dollar of a company's earnings.

What is a way to measure stock performance? ›

Compare your stocks' performance against benchmarks, or stock market indices. Review stock indicators, including Earnings Per Share (EPS), Price to Earnings (P/E) ratio, Price to Earnings ratio to Growth ratio (PEG), Price to Book Value ratio (P/B), Dividend Payout ratio (DPR), and Dividend Yield.

Is R good for stock analysis? ›

Quantmod is an R package specifically designed for quantitative financial modeling and trading. It provides a wide range of functions and tools for collecting, analyzing, and visualizing financial and stock market data.

How do you monitor and maintain stock levels briefly explain? ›

What are the best ways to monitor stock levels and reduce shrinkage in inventory management?
  1. Track inventory in real time. ...
  2. Conduct regular audits and cycle counts. ...
  3. Implement security measures and controls. ...
  4. Use inventory management software. ...
  5. Train and motivate your staff. ...
  6. Apply the Pareto principle.
Sep 12, 2023

How to analyze stocks for beginners? ›

There are a few aspects to consider when you wish to determine whether a share is worth investing in. The company's fundamentals: Research the company's performance in the last five years, including figures like earnings per share, price to book ratio, price to earnings ratio, dividend, return on equity, etc.

What is the best way to compare stock performance? ›

You can compare the performance of stocks based on the following:
  • Analyst ratings.
  • Book value.
  • Debt.
  • Dividend.
  • Market Rank.
  • News sentiment.
  • Price Performance.
  • Profitability.

What is a stock inventory summary? ›

The Stock Summary report displays the details of all your item's opening stock, quantity sold (Quantity Out), purchased (Quantity In), and closing stock (calculated as Opening Stock + Quantity In - Quantity Out) for a given period.

What does R mean in stocks? ›

'R' stands for the amount of risk you take during a trade. Technically, it is just another way of looking at a profit and loss ratio.

What is the R value in stocks? ›

“R” is how much you are risking, and making, per trade. It is a standardized amount (dollars or percentage of account) for YOU. R could be 1% of your account if you typically risk/want to risk 1% of the acount. Or it could be 2% or 0.5% of the account balance.

What is R for analysis? ›

R is a statistical programming tool that's uniquely equipped to handle data, and lots of it. Wrangling mass amounts of information and producing publication-ready graphics and visualizations is easy with R. So are all sorts of data analysis, mining, and modeling tasks.

How do you monitor stock inventory? ›

Here are some of the techniques that many small businesses use to manage inventory:
  1. Fine-tune your forecasting. ...
  2. Use the FIFO approach (first in, first out). ...
  3. Identify low-turn stock. ...
  4. Audit your stock. ...
  5. Use cloud-based inventory management software. ...
  6. Track your stock levels at all times. ...
  7. Reduce equipment repair times.

How to tell if a stock is outperforming the market? ›

A stock outperforms if its return is higher than that of an index or another stock. A stock is said to outperform if it produces a higher return than an index or the overall stock market, and analysts give stocks an outperform rating if superior performance is expected.

Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated:

Views: 5680

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.