library(quantmod)
library(DT)
library(highcharter)
library(blastula)
library(formattable)
library(ggplot2)
library(ggthemes)
prices <- round(getSymbols(params$symbol, auto.assign = FALSE, src = 'yahoo'), 2)
close <- Cl(last(prices))
open <- Op(last(prices))
recent <- last(prices, n=90)
recent_nv <- recent[,-5]
The stock closed down at 332.8 dollars per share yesterday.
The chart below is made with the quantmod
and highcharter
R packages. An API returns all of the price history based on the stock tick symbol provided as a parameter. The candlestick chart is a default function from highcharter, as is the Economist theme.
highchart(type = "stock") %>%
hc_yAxis_multiples(
list(title = list(text = NULL), height = "75%", top = "0%"),
list(title = list(text = NULL), height = "15%", top = "80.5%", opposite = TRUE)
) %>%
hc_add_series(prices, type = "candlestick", yAxis=0, name= params$symbol) %>%
hc_add_series(prices[,paste0(params$symbol,".Volume")], name="Volume", type="column", yAxis=1) %>%
hc_add_theme(hc_theme_economist())
The table below displays the daily price data for the stock. A concise, interactive table is created with the DT
package.
df <- as.data.frame(recent)
df[,paste0(params$symbol, ".Volume")] <- df[,paste0(params$symbol, ".Volume")]/1000000
datatable(df) %>%
formatCurrency(c(paste0(params$symbol, ".Open"), paste0(params$symbol, ".High"), paste0(params$symbol, ".Low"), paste0(params$symbol,".Close")), digits=2) %>%
formatRound(c(paste0(params$symbol, ".Volume")), digits=0)
This report also creates an excel file with the relevant information updated by R. The Excel file contains a legacy report that will slowly be replaced.
fname <- sprintf('%s.xlsx', Sys.Date())
write.csv(df, file = fname)
rmarkdown::output_metadata$set(rsc_output_files = list(fname))
This report also produces an email that is sent to key stakeholders with summary information if the price change is above 10.
# Calculate the total change
close <- Cl(last(prices, n = 2))
diff <- round(as.numeric(close[2]) - as.numeric(close[1]),2)
# If the change is above a $10 / share, send an email to stake holders
if (abs(diff) > params$threshold || params$force) {
rmarkdown::output_metadata$set(rsc_email_suppress_scheduled = FALSE)
# get metadata
report_name <- Sys.getenv("RSC_REPORT_NAME")
report_url <- Sys.getenv("RSC_REPORT_URL")
subscription_url <- Sys.getenv("RSC_REPORT_SUBSCRIPTION_URL")
# set a dynamic subject line
subject <- paste0(params$symbol,
' is ',
ifelse(diff > 0, 'up', 'down'),
' today by $',
abs(diff), '!')
rmarkdown::output_metadata$set("rsc_email_subject" = subject)
# also update the body
price_new <- prices[,c(1,4)]
colnames(price_new) <- c('open', 'close')
price_new$change <- price_new$close - price_new$open
tbl <- format_table(
x = as.data.frame(tail(price_new)),
list(
change = formatter("span", style = x ~ ifelse(x > 0, style(color = "green"), style(color = "red"))),
area(col = c(open, close)) ~ normalize_bar("lightgrey")
)
)
p <- recent[,6] %>%
autoplot() +
geom_smooth() +
theme_fivethirtyeight() +
labs(
title = sprintf("%s Price Adjusted", params$symbol)
)
msg <- compose_email(
body = "
Hello Team,
Here are the latest stock prices for {params$symbol}.
{tbl}
The historical trend is shown below:
{add_ggplot(p, width = 6, height = 6)}
Let me know if you have any questions.
Best,
Team Lead",
footer = "
This <strong>{report_name}</strong> document is available on our RStudio Connect server [here]({report_url}).
To stop receiving these emails, <code>[unsubscribe]({subscription_url})</code>.
"
)
rmarkdown::output_metadata$set(rsc_email_body_html = msg$html_str)
rmarkdown::output_metadata$set(rsc_email_images = msg$images)
# attach the excel file
rmarkdown::output_metadata$set(rsc_email_attachments = list(fname))
} else {
# don't send an email
rmarkdown::output_metadata$set(rsc_email_suppress_scheduled = TRUE)
}