---
title: "ANLY 512 Lab 1"
subtitle: Financial Dashboard
author: "Mengyuan Huang, Ziyan Zhang"
date: "`r Sys.Date()`"
output:
flexdashboard::flex_dashboard:
orientation: row
vertical_layout: fill
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(knitr)
```
```{r}
# data source
## https://finance.yahoo.com/quote/AMZN/history?p=AMZN
## https://finance.yahoo.com/quote/COST/history?p=COST
## https://finance.yahoo.com/quote/TGT/history?p=TGT
## https://finance.yahoo.com/quote/WMT/history?p=WMT
# read data
## AMZN <- read.csv("Lab1_AMZN.csv") # Amazon
## COST <- read.csv("Lab1_COST.csv") # Costco
## TGT <- read.csv("Lab1_TGT.csv") # Target
## WMT <- read.csv("Lab1_WMT.csv") # Walmart
# change data type
## AMZN$Date <- as.Date(AMZN$Date)
## COST$Date <- as.Date(COST$Date)
## TGT$Date <- as.Date(TGT$Date)
## WMT$Date <- as.Date(WMT$Date)
# load required packages
library(ggplot2)
library(plotly)
library(tidyquant)
library(flexdashboard)
# get data
DATA <- c("AMZN", "COST", "TGT", "WMT") %>%
tq_get(get = "stock.prices", from = "2019-01-01")
AMZN <- subset(DATA, symbol == "AMZN")
COST <- subset(DATA, symbol == "COST")
TGT <- subset(DATA, symbol == "TGT")
WMT <- subset(DATA, symbol == "WMT")
# define global variables and functions
names <- c("AMZN", "COST", "TGT", "WMT")
last_date = max(DATA$date)
second_last_date = max(DATA$date[DATA$date != last_date])
one_week_prior = max(DATA$date[DATA$date <= last_date - 7])
one_month_prior = max(DATA$date[DATA$date <= last_date - 30])
last_month_volumn <- function(df){
last_month <- df[df$date > one_month_prior,]
return(sum(last_month$volume))
}
last_month_price_percent_change <- function(df){
price_prior = df$close[df$date == one_month_prior]
price_now = df$close[df$date == last_date]
return((price_now-price_prior)*100/price_prior)
}
```
```{r}
# last date close price change
AMZN_last_close <- AMZN$close[AMZN$date == last_date]
AMZN_last_change <- round(AMZN_last_close - AMZN$close[AMZN$date == second_last_date], 2)
AMZN_last_change_s <- if (AMZN_last_change < 0) paste("-$", abs(AMZN_last_change), sep = "") else paste("+$", AMZN_last_change, sep = "")
COST_last_close <- COST$close[COST$date == last_date]
COST_last_change <- round(COST_last_close - COST$close[COST$date == second_last_date], 2)
COST_last_change_s <- if (COST_last_change < 0) paste("-$", abs(COST_last_change), sep = "") else paste("+$", COST_last_change, sep = "")
TGT_last_close <- TGT$close[TGT$date == last_date]
TGT_last_change <- round(TGT_last_close - TGT$close[TGT$date == second_last_date], 2)
TGT_last_change_s <- if (TGT_last_change < 0) paste("-$", abs(TGT_last_change), sep = "") else paste("+$", TGT_last_change, sep = "")
WMT_last_close <- WMT$close[WMT$date == last_date]
WMT_last_change <- round(WMT_last_close - WMT$close[WMT$date == second_last_date], 2)
WMT_last_change_s <- if (WMT_last_change < 0) paste("-$", abs(WMT_last_change), sep = "") else paste("+$", WMT_last_change, sep = "")
```
Row
-----------------------------------------------------------------------
### AMAZON (AMZN) Last Closing Price, `r AMZN_last_change_s` compares to prior date {.value-box}
```{r}
valueBox(
value = paste("$",round(AMZN_last_close,2)),
icon = "fa-area-chart",
color = if (AMZN_last_change >= 0) "green" else "red"
)
```
### COSTCO (COST) Last Closing Price, `r COST_last_change_s` compares to prior date {.value-box}
```{r}
valueBox(
value = paste("$",round(COST_last_close,2)),
icon = "fa-area-chart",
color = if (COST_last_change >= 0) "green" else "red"
)
```
### TARGET (TGT) Last Closing Price, `r TGT_last_change_s` compares to prior date {.value-box}
```{r}
valueBox(
value = paste("$",round(TGT_last_close,2)),
icon = "fa-area-chart",
color = if (TGT_last_change >= 0) "green" else "red"
)
```
### WALMART (WMT) Last Closing Price, `r WMT_last_change_s` compares to prior date {.value-box}
```{r}
valueBox(
value = paste("$",round(WMT_last_close,2)),
icon = "fa-area-chart",
color = if (WMT_last_change >= 0) "green" else "red"
)
```
Row
-----------------------------------------------------------------------
### Last 30-Day Price Change (%)
```{r}
price_change = c(last_month_price_percent_change(AMZN),
last_month_price_percent_change(COST),
last_month_price_percent_change(TGT),
last_month_price_percent_change(WMT))
p_change_data = as.data.frame(price_change)
p_change_data$Company <- names
p_change_data$status[price_change >= 0] = 'Increased Price'
p_change_data$status[price_change < 0] = 'Decreased Price'
ggplotly(
ggplot(p_change_data, aes(x = Company, y = price_change, fill = status)) +
geom_bar(stat="identity") +
scale_fill_manual("Status", values = c('Increased Price' = "seagreen", 'Decreased Price' = "indianred")) +
xlab("Company") +
ylab("Price Change (%) in the past month")
)
```
### Last 30-Day Volumn (Million)
```{r}
volumn = c(last_month_volumn(AMZN),
last_month_volumn(COST),
last_month_volumn(TGT),
last_month_volumn(WMT))
volumn_data = as.data.frame(volumn)
volumn_data$Company <- names
ggplotly(
ggplot(volumn_data, aes(x = Company, y = volumn / 1000000)) +
geom_bar(stat="identity", fill = 'dodgerblue3') +
xlab("Company") +
ylab("Volumn (million) in the past month")
)
```
Row {.tabset .tabset-fade}
-----------------------------------------------------------------------
### AMAZON (AMZN)
```{r}
# calculate moving average
AMZN$mavg <- SMA(AMZN$close, 15)
# plot
AMZN %>%
plot_ly(x = ~date, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
line = list(color = '#E377C2', width = 0.5),
hoverinfo = "none", inherit = F)
```
### COSTCO (COST)
```{r}
# calculate moving average
COST$mavg <- SMA(COST$close, 15)
# plot
COST %>%
plot_ly(x = ~date, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
line = list(color = '#E377C2', width = 0.5),
hoverinfo = "none", inherit = F)
```
### TARGET (TGT)
```{r}
# calculate moving average
TGT$mavg <- SMA(TGT$close, 15)
# plot
TGT %>%
plot_ly(x = ~date, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
line = list(color = '#E377C2', width = 0.5),
hoverinfo = "none", inherit = F)
```
### WALMART (WMT)
```{r}
# calculate moving average
WMT$mavg <- SMA(WMT$close, 15)
# plot
WMT %>%
plot_ly(x = ~date, type="candlestick",
open = ~open, close = ~close,
high = ~high, low = ~low) %>%
add_lines(x = ~date, y = ~mavg, name = "Mv Avg",
line = list(color = '#E377C2', width = 0.5),
hoverinfo = "none", inherit = F)
```