Overview

Column

Table

Column

Chart

[1] "TSLA" "AMZN" "NFLX" "GOOG"

Facet Chart

Details

Column

TSLA

AMZN

NFLX

GOOG

---
title: "Stock Analysis"
author: "ByoungJun Jo"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{r setup, include=FALSE, source_code = TRUE}
library(flexdashboard)
library(quantmod)
library(plyr)
library(DT)
library(tidyquant)
library(dplyr)
library(highcharter)
library(viridisLite)
library(ggplot2)
library(broom)
```

# Intro text {.sidebar}

Summary

In this analysis, indicators such as “Price/Sales”, “P/E Ratio”, “Price/EPS Estimate Next Year”, and “Market Capitalization” will be used to compare four different companies’s financial data from Jan 1, 2020 to Sept 31, 2022. These companies are Tesla, Amazon, Netflix, and Google.

We can see that Tesla has the highest rate of return given volatility measured in a Bolinger band with 20-day Moving Average. Despite the recent bear market in 2022, it could be that the demand for Tesla's products are still high enough to rationalize the stock price, given volatility.Therefore, we can conclude that Tesla (TSLA) has the most highest short-term gain lately.

```{r include = FALSE}
knitr::opts_chunk$set(cache = TRUE)
```
# Overview {data-icon="fa-list"}

## Column {data-height=350}

### Table
```{r source_code = TRUE}
what_metrics <- yahooQF(c("Price/Sales", 
                          "P/E Ratio",
                          "Price/EPS Estimate Next Year",
                          "PEG Ratio",
                          "Market Capitalization"))

tickers <- c("TSLA", "AMZN", "NFLX", "GOOG")
# Not all the metrics are returned by Yahoo.
metrics <- getQuote(paste(tickers, sep="", collapse=";"), what=what_metrics)

#Add tickers as the first column and remove the first column which had date stamps
metrics <- data.frame(Symbol=tickers, metrics[,2:length(metrics)]) 

#Change colnames
colnames(metrics) <- c("Symbol", "P-E Ratio", "Price EPS Estimate Next Year", "Market Cap")

#write this to the csv file
write.csv(metrics, "FinancialMetrics.csv", row.names=FALSE)
DT::datatable(metrics)
```

## Column {data-height=650 .tabset .tabset-fade}

### Chart {data-width=300}

```{r}
start = as.Date("2020-01-01") 
end = as.Date("2022-9-30")

getSymbols(tickers, src = "yahoo", from = start, to = end)
stocks = as.xts(data.frame(A = TSLA[, "TSLA.Adjusted"], 
B = AMZN[, "AMZN.Adjusted"], C = NFLX[, "NFLX.Adjusted"], 
E = GOOG[,"GOOG.Adjusted"]))

names(stocks) = c("Tesla", "Amazon", "Netflix","Google")
index(stocks) = as.Date(index(stocks))

stocks_series = tidy(stocks) %>% 
  
  ggplot(aes(x=index,y=value, color=series)) +
  labs(title = "Daily Stock Prices January 2020 - September 2022",
       
       subtitle = "End of Day Adjusted Prices",
       caption = " Source: Yahoo Finance") +
  
  xlab("Date") + ylab("Price") +
  scale_color_manual(values = c("Red", "Black", "DarkBlue","Orange"))+
  geom_line()
stocks_series
```

### Facet Chart {data-width=700}

```{r}
stocks_series2 = tidy(stocks) %>% 
  
  ggplot(aes(x=index,y=value, color=series)) + 
  geom_line() +
  facet_grid(series~.,scales = "free") + 
  labs(title = "Daily Stock Prices January 2020 - September 2022",
                                              
                                              subtitle = "End of Day Adjusted Prices",
                                              caption = " Source: Yahoo Finance") +
  
  xlab("Date") + ylab("Price") +
  scale_color_manual(values = c("Red", "Black", "DarkBlue","Orange"))
stocks_series2
```

# Details {data-icon="fa-map"}

## Column {data-height=650 .tabset .tabset-fade}

```{r}
TSLA <- tq_get("TSLA", get = "stock.prices", from = start, to = end)
AMZN <- tq_get("AMZN", get = "stock.prices", from = start, to = end)
NFLX <- tq_get("NFLX", get = "stock.prices", from = start, to = end)
GOOG <- tq_get("GOOG", get = "stock.prices", from = start, to = end)
TANG <- c("TSLA", "AMZN", "NFLX", "GOOG") %>%
    tq_get(get = "stock.prices", from = start, to = end)

# Setup dates for zoom window
end <- ymd("2022-09-30")
start <- end - weeks(20)
```

### TSLA {data-width=700}
```{r}
# Create chart
TSLA %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    # geom_ma(ma_fun = SMA, n = 15, size = 1) +
    # geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) +
    labs(title = "TSLA: Candlestick + BBands (2022.06 - 2022.09)",
         subtitle = "Visualize volatility",
         x = "", y = "Closing Price") +
    coord_x_date(xlim = c(start, end),
                 ylim = c(150, 330))
```

### AMZN {data-width=700}
```{r}
# Create chart
AMZN %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    # geom_ma(ma_fun = SMA, n = 15, size = 1) + 
    # geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) +
    labs(title = "AMZN: Candlestick + BBands (2022.06 - 2022.09)",
         subtitle = "Visualize volatility",
         x = "", y = "Closing Price") +
    coord_x_date(xlim = c(start, end),
                 ylim = c(80, 180))
```

### NFLX {data-width=700}
```{r}
# Create chart
NFLX %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    # geom_ma(ma_fun = SMA, n = 15, size = 1) + 
    # geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) +
    labs(title = "NFLX: Candlestick + BBands (2022.06 - 2022.09)",
         subtitle = "Visualize volatility",
         x = "", y = "Closing Price") +
    coord_x_date(xlim = c(start, end),
                 ylim = c(100, 300))
```

### GOOG {data-width=700}
```{r}
# Create chart
GOOG %>%
    ggplot(aes(x = date, y = close)) +
    geom_candlestick(aes(open = open, close = close, high = high, low = low)) + 
    # geom_ma(ma_fun = SMA, n = 15, size = 1) + 
    # geom_ma(ma_fun = SMA, n = 50, color = "red", linetype = 4, size = 1) +
    geom_bbands(aes(high = high, low = low, close = close),
                ma_fun = SMA, n = 20, sd = 2, size = 1) +
    labs(title = "GOOG: Candlestick + BBands (2022.06 - 2022.09)",
         subtitle = "Visualize volatility",
         x = "", y = "Closing Price") +
    coord_x_date(xlim = c(start, end),
                 ylim = c(80, 150))
```