We notice that all the stocks had a volatile Nov, Dec months but Microsoft stocks were the least volatile of the lot when we observe the Bollinger Bands
We notice that the current PE ratio is higher than the recently captured P/E ratios for all stocks except for Microsoft. Micron has had a huge dip in its P/E ratio and is the least favorite of the lot
GM and Micron have had significatnt drops in their returns. MSFT has been more stable. Overall from the above vizualizations and analysis, for some one who is risk averse, MSFT would be the best bet for short term gains
---
title: "Lab1 - Dashboarding Assignment"
subtitle: Financial Analysis of Stocks
output:
flexdashboard::flex_dashboard:
orientation: rows
vertical_layout: fill
source_code: embed
---
```{r}
#Install tidyquant,flexdashboard
#Load flexdashboard
require(flexdashboard)
# Loads tidyquant, tidyverse, lubridate, quantmod, TTR, xts, zoo
require(tidyquant)
```
Dashboard
=======================================================================
Sidebar {.sidebar}
-----------------------------------------------------------------------
### Finacial Analysis of Stocks
This dashboard analyses the stocks of Micron, Microsoft,General Motors and IBM to predict the stock with th best short term gain
Row
-----------------------------------------------------------------------
### Time Series of Stock Volume in the last year
```{r, fig.height=5, fig.width=20}
#Getting the stock prices of 4 stocks(Micron, Microsoft, GM, IBM) in the last 6 months
MMGI <- c("MU", "MSFT", "GM", "IBM") %>%
tq_get(get = "stock.prices", from = "2018-10-01", to = "2019-03-01")
#Start and end dates
end <- ymd("2019-03-01")
start <- end - weeks(24)
n <- 30
MMGI %>%
ggplot(aes(x = date, y = volume, color = symbol)) +
geom_point() +
labs(title = "Daily Stock Volume", x = "",y="Volume") +
facet_wrap(~ symbol, scale = "free_y") +
scale_fill_tq(fill="green4",theme="light") +
theme_tq() +
theme(panel.border = element_blank(),
panel.grid.major = element_line(colour = "grey61", size = 0.5, linetype = "dotted"),
panel.grid.minor = element_blank(),
axis.line=element_line(colour="black"),
plot.title = element_text(hjust = 0.5,size=18,colour="black"))+
theme(legend.position="none")
```
Row {.tabset}
-----------------------------------------------------------------------
### Comparison of Stock Volatility
```{r,fig.height=6, fig.width=20}
#Getting the stock prices of 4 stocks(Micron, Microsoft, GM, IBM) in the last 6 months
MMGI <- c("MU", "MSFT", "GM", "IBM") %>%
tq_get(get = "stock.prices", from = "2018-10-01", to = "2019-03-01")
#Start and end dates
end <- ymd("2019-03-01")
start <- end - weeks(24)
#Use 30 day simple moving avg to visualize stock volatility
n <- 30
MMGI %>%
filter(date >= start - days(2 * n)) %>%
ggplot(aes(x = date, y = close, group = symbol)) +
geom_candlestick(aes(open = open, close = close, high = high, low = low)) +
geom_bbands(aes(high = high, low = low, close = close),
ma_fun = SMA, n = n, sd = 2, size = 1) +
labs(title = "Volatility of Stocks!",
subtitle = "Vizualize highs and lows of stocks using simple moving averages and stock prices",
x = "", y = "Closing Price") +
coord_x_date(xlim = c(start, end)) +
facet_wrap(~ symbol, scales = "free_y")
```
> We notice that all the stocks had a volatile Nov, Dec months but Microsoft stocks were the least volatile of the lot when we observe the Bollinger Bands
### Comparison of P/E ratios
```{r,fig.height=6, fig.width=20}
# Comparing the P/E ratios of stocks, historical vs current
#Get key statistics like P/E ratios, Chane, Open price etc
MU_ratios <-tq_get(c("MU"),get = "key.ratios")
MU_historical_pe_ratios <- MU_ratios %>%
filter(section == "Valuation Ratios") %>%
unnest() %>%
filter(category == "Price to Earnings") %>%
select(category, date, value)
#Current P/E ratios
pe_metrics <- yahooQF(c("P/E Ratio"))
tickers_MU <- c("MU")
# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers_MU, sep="", collapse=";"), what=pe_metrics)
mu_gg<-ggplot(MU_historical_pe_ratios,aes(x = date, y = value)) +
geom_line(aes(color = "historical.pe"), size = 1) +
geom_point(size = 3) +
geom_ma(aes(color = "historical.ma"), ma_fun = SMA, n = 3, size = 1) +
geom_hline(yintercept = metrics$`P/E Ratio`, color = "red", size = 1) +
labs(title = "Micron P/E Ratio: Historical Trends Versus Current Value",
y = "", x = "") + ylim(min(MU_historical_pe_ratios$value),max(MU_historical_pe_ratios$value)) +
scale_colour_manual(name = "", values = c('historical.pe' = 'black',
'historical.ma' = 'blue'),
labels = c('3-Year Moving Average',
'Historical PE')) +
annotate("text", label = "Current PE", x = ymd("2016-06-01"), y = 16,
color = "red", size = 3.5) +
theme_bw() +
theme(legend.justification="left", legend.position="top")
#Microsoft P/E ratio comparison
MSFT_ratios <-tq_get(c("MSFT"),get = "key.ratios")
MSFT_historical_pe_ratios <- MSFT_ratios %>%
filter(section == "Valuation Ratios") %>%
unnest() %>%
filter(category == "Price to Earnings") %>%
select(category, date, value)
#Current P/E ratios
pe_metrics <- yahooQF(c("P/E Ratio"))
tickers_MSFT <- c("MSFT")
# Not all the metrics are returned by Yahoo.
metrics_MSFT <- getQuote(paste(tickers_MSFT, sep="", collapse=";"), what=pe_metrics)
msft_gg<-ggplot(MSFT_historical_pe_ratios,aes(x = date, y = value)) +
geom_line(aes(color = "historical.pe"), size = 1) +
geom_point(size = 3) +
geom_ma(aes(color = "historical.ma"), ma_fun = SMA, n = 3, size = 1) +
geom_hline(yintercept = metrics_MSFT$`P/E Ratio`, color = "red", size = 1) +
labs(title = "MSFT P/E Ratio: Historical Trends Versus Current Value",
y = "", x = "") + ylim(min(MSFT_historical_pe_ratios$value),max(MSFT_historical_pe_ratios$value)) +
scale_colour_manual(name = "", values = c('historical.pe' = 'black',
'historical.ma' = 'blue'),
labels = c('3-Year Moving Average',
'Historical PE')) +
annotate("text", label = "Current PE", x = ymd("2016-06-01"), y = 16,
color = "red", size = 3.5) +
theme_bw() +
theme(legend.justification="left", legend.position="top")
#General Motors P/E ratio comparison
GM_ratios <-tq_get(c("GM"),get = "key.ratios")
GM_historical_pe_ratios <- GM_ratios %>%
filter(section == "Valuation Ratios") %>%
unnest() %>%
filter(category == "Price to Earnings") %>%
select(category, date, value)
#Current P/E ratios
pe_metrics <- yahooQF(c("P/E Ratio"))
tickers_GM <- c("GM")
# Not all the metrics are returned by Yahoo.
metrics_GM <- getQuote(paste(tickers_GM, sep="", collapse=";"), what=pe_metrics)
gm_gg<-ggplot(GM_historical_pe_ratios,aes(x = date, y = value)) +
geom_line(aes(color = "historical.pe"), size = 1) +
geom_point(size = 3) +
geom_ma(aes(color = "historical.ma"), ma_fun = SMA, n = 3, size = 1) +
geom_hline(yintercept = metrics_GM$`P/E Ratio`, color = "red", size = 1) +
labs(title = "GM P/E Ratio: Historical Trends Versus Current Value",
y = "", x = "") + ylim(min(GM_historical_pe_ratios$value),max(GM_historical_pe_ratios$value)) +
scale_colour_manual(name = "", values = c('historical.pe' = 'black',
'historical.ma' = 'blue'),
labels = c('3-Year Moving Average',
'Historical PE')) +
annotate("text", label = "Current PE", x = ymd("2016-06-01"), y = 16,
color = "red", size = 3.5) +
theme_bw() +
theme(legend.justification="left", legend.position="top")
#IBM P/E ratio comparison
IBM_ratios <-tq_get(c("IBM"),get = "key.ratios")
IBM_historical_pe_ratios <- IBM_ratios %>%
filter(section == "Valuation Ratios") %>%
unnest() %>%
filter(category == "Price to Earnings") %>%
select(category, date, value)
#Current P/E ratios
pe_metrics <- yahooQF(c("P/E Ratio"))
tickers_IBM <- c("IBM")
# Not all the metrics are returned by Yahoo.
metrics_IBM <- getQuote(paste(tickers_IBM, sep="", collapse=";"), what=pe_metrics)
ibm_gg<-ggplot(IBM_historical_pe_ratios,aes(x = date, y = value)) +
geom_line(aes(color = "historical.pe"), size = 1) +
geom_point(size = 3) +
geom_ma(aes(color = "historical.ma"), ma_fun = SMA, n = 3, size = 1) +
geom_hline(yintercept = metrics_IBM$`P/E Ratio`, color = "red", size = 1) +
labs(title = "IBM P/E Ratio: Historical Trends Versus Current Value",
y = "", x = "") + ylim(min(IBM_historical_pe_ratios$value),max(IBM_historical_pe_ratios$value)) +
scale_colour_manual(name = "", values = c('historical.pe' = 'black',
'historical.ma' = 'blue'),
labels = c('3-Year Moving Average',
'Historical PE')) +
annotate("text", label = "Current PE", x = ymd("2019-06-01"), y = 16,
color = "red", size = 3.5) +
theme_bw() +
theme(legend.justification="left", legend.position="top")
require(gridExtra)
grid.arrange(mu_gg,msft_gg,gm_gg,ibm_gg,ncol=2)
```
> We notice that the current PE ratio is higher than the recently captured P/E ratios for all stocks except for Microsoft. Micron has had a huge dip in its P/E ratio and is the least favorite of the lot
### Comparison of Regression on Monthly Returns
```{r,fig.height=6, fig.width=20}
#Get adjusted stock price and 1 year returns
get_annual_returns <- function(stock.symbol) {
stock.symbol %>%
tq_get(get = "stock.prices",
from = "2018-03-01",
to = "2019-03-01") %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
type = "log",
period = "monthly")
}
#Regression of MU returns
MU_annual_log_returns <- get_annual_returns("MU")
mu<-ggplot(MU_annual_log_returns,aes(x = date, y = monthly.returns)) +
geom_hline(yintercept = 0, color = "black") +
geom_point(size = 2, color = palette_light()[[3]]) +
geom_line(size = 1, color = palette_light()[[3]]) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "MU: Monthly Returns",
x = "", y = "Monthly Returns", color = "") +
theme_tq() + scale_y_continuous(labels = scales::percent)
#Regression of MSFT returns
MSFT_annual_log_returns <- get_annual_returns("MSFT")
msft<-ggplot(MSFT_annual_log_returns,aes(x = date, y = monthly.returns)) +
geom_hline(yintercept = 0, color = "black") +
geom_point(size = 2, color = palette_light()[[9]]) +
geom_line(size = 1, color = palette_light()[[9]]) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "MSFT: Monthly Returns",
x = "", y = "Monthly Returns", color = "") +
theme_tq() + scale_y_continuous(labels = scales::percent)
#Regression of GM returns
GM_annual_log_returns <- get_annual_returns("GM")
gm<-ggplot(GM_annual_log_returns,aes(x = date, y = monthly.returns)) +
geom_hline(yintercept = 0, color = "black") +
geom_point(size = 2, color = palette_light()[[7]]) +
geom_line(size = 1, color = palette_light()[[7]]) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "GM: Monthly Returns",
x = "", y = "Monthly Returns", color = "") +
theme_tq() + scale_y_continuous(labels = scales::percent)
#Regression of IBM returns
IBM_annual_log_returns <- get_annual_returns("IBM")
ibm<-ggplot(IBM_annual_log_returns,aes(x = date, y = monthly.returns)) +
geom_hline(yintercept = 0, color = palette_light()[[1]]) +
geom_point(size = 2, color = palette_light()[[10]]) +
geom_line(size = 1, color = palette_light()[[10]]) +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "IBM: Monthly Returns",
x = "", y = "Monthly Returns", color = "") +
theme_tq() + scale_y_continuous(labels = scales::percent)
require(gridExtra)
grid.arrange(mu,msft,gm,ibm,ncol=2)
```
> GM and Micron have had significatnt drops in their returns. MSFT has been more stable. Overall from the above vizualizations and analysis, for some one who is risk averse, MSFT would be the best bet for short term gains