---
title: "FM_Intro1"
author: '18.642'
date: "2024-09-04"
output:
  pdf_document:
    toc: true
  html_document:
    toc: true
    toc_float: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(warn = -1)
defaultW <- getOption("warn")
options(warn = -1)
```


```{r, echo=FALSE, message = FALSE}
# Updated 9/6/2022
# 0. Load packages ----
library("tidyquant")
library("tidyverse")
library("fpp2")

```

\newpage
# 1. Collect historical data  

## 1.1 Set start and end date for collection 

```{r,echo=TRUE}      
date_start <- "2011-01-01"
date_end <- "2024-08-31"
```

## 1.2 Collect time series data from Yahoo 


```{r,echo=TRUE}
#       Collect index_data for S&P 500 and VIX indexes ----
#       Source: http://finance.yahoo.com
#
index_data <- tq_get(c("^GSPC", "^VIX"),
                     get = "stock.prices",
                     from = date_start,
                     to = date_end) 
```

```{r,echo=TRUE}
#        Collect stock_data for stocks ----

#       Source: http://finance.yahoo.com
stock_data <- tq_get(c("NVDA",
                       "GE", 
                       "AAPL", 
                       "GOOG", 
                       "AMZN", 
                       "XOM", 
                       "GME", 
                       "ARKK",
                       "AMC",
                       "BTC-USD",
                       "XLF"),
                     get = "stock.prices",
                     from = date_start,
                     to = date_end)
```

## 1.3 Collect time series data from FRED


```{r, echo=TRUE}
#  Source: St. Louis Federal Reserve
##               http://research.stlouisfed.org/fred2/
#
# Series name | Description
# ------------ --------------
# SP500       | SP500 Stock market index
# VIXCLS      | Vix volatility index
#
# DGS3MO      | 3-Month Treasury, constant maturity rate
# DGS1        | 1-Year Treasury, constant maturity rate
# DGS5        | 5-Year Treasury, constant maturity rate
# DGS10       | 10-Year Treasury, constant maturity rate
#
# DAAA        | Moody's Seasoned Aaa Corporate Bond Yield 
# DBAA        | Moody's Seasoned Baa Corporate Bond Yield 
#
# DCOILWTICO  | Crude Oil Prices: West Text Intermediate (WTI) - Cushing, Oklahoma
# CBBTCUSD    | Coinbase Bitcoin 
economic_data <- tq_get(c(
                      "SP500",
                      "VIXCLS",
                      "DGS3MO",
                      "DGS1",
                      "DGS5",
                      "DGS10",
                      "DAAA",
                      "DBAA",
                      "DCOILWTICO",
                      "CBBTCUSD"),
                    get = "economic.data",
                    from = date_start,
                    to = date_end)

```

# 2. Select/Filter/Merge  datasets


## 2.1 Select/Filter datasets 

The following code creates the object market\_data. For each dataset:

* 1. Select three columns (symbol, date, price)
* 2. Filter out/remove NA observations
* 3. Rename adjusted to price (for stocks/indexes)----




```{r, echo=TRUE}

## 2.1 Select three columns (symbol, date, price)
## 2.2 Rename adjusted to price (for stocks/indexes)----
## 2.3 Remove NA observations

index_data1 <- index_data %>% 
  dplyr::select(symbol, date, adjusted) %>% 
  dplyr::filter(!is.na(adjusted)) %>% 
  dplyr::rename(price = adjusted)

stock_data1 <- stock_data %>% 
  dplyr::select(symbol, date, adjusted) %>% 
  dplyr::filter(!is.na(adjusted)) %>% 
  dplyr::rename(price = adjusted)

# economic_data has same 3 columns (no select/rename needed)
economic_data1 <- economic_data %>% 
  dplyr::filter(!is.na(price)) 
```

## 2.2 Merge index_data1, stock_data1, and economic_data1

```{r, echo=TRUE}
market_data <- index_data1 %>% 
  full_join(stock_data1) %>% 
  full_join(economic_data1)
```


# 3. Plot time series
 
## 3.1 S&P 500 Index

```{r,echo=TRUE}
market_data %>% 
  filter(symbol == "SP500") %>% 
  ggplot(market_data, mapping = aes(date, price)) +
    geom_line() +
    labs(title="S&P 500 Index", x="Date", y="Price")
```


\newpage
##   3.2 VIX Index 
```{r,echo=TRUE}
market_data %>% 
  filter(symbol == "VIXCLS") %>% 
  ggplot(market_data, mapping = aes(date, price)) +
  geom_line() +
  labs(title="VIX Index", x="Date", y="Value")

```
\newpage

## 3.3 Plot both together

```{r,echo=TRUE}
market_data %>% 
  filter(symbol %in% c("SP500","VIXCLS")) %>% 
  ggplot( mapping = aes(date,price),col=symbol) +
  geom_line() +
  facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
  labs(x="Date", y="Price/Value", title="Index Time Series: S&P 500 and VIX")
```


\newpage
## 3.4 ARKK  


```{r, echo=TRUE}
market_data %>% 
  filter(symbol %in% c("ARKK")) %>% 
  ggplot( mapping = aes(date,price)) +
  geom_line() +
  facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
  labs(x="Date", y="Price", 
       title="Price Time Series:  ARKK")
```

\newpage
## 3.5 GME   


```{r, echo=TRUE}
market_data %>% 
  filter(symbol %in% c("GME")) %>% 
  ggplot( mapping = aes(date,price)) +
  geom_line() +
  facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
  labs(x="Date", y="Price", 
       title="Price Time Series:  GME")
```


\newpage
## 3.6 Bitcoin  


```{r, echo=TRUE}
market_data %>% 
  filter(symbol %in% c("BTC-USD")) %>% 
  ggplot( mapping = aes(date,price)) +
  geom_line() +
  facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
  labs(x="Date", y="Price", 
       title="Price Time Series:  Bitcoin")
```


\newpage
## 3.7 Nvidia


```{r, echo=TRUE}
market_data %>% 
  filter(symbol %in% c("NVDA")) %>% 
  ggplot( mapping = aes(date,price)) +
  geom_line() +
  facet_wrap(~ symbol, ncol = 1, scale = "free_y") +
  labs(x="Date", y="Price", 
       title="Price Time Series:  Nvidia")
```


\newpage
## 3.8 Bond Yields on the same graph  
```{r, echo=TRUE}
list_yields<-c("DGS3MO","DGS1", "DGS5","DGS10","DAAA","DBAA")

market_data %>% 
  filter(symbol %in% list_yields) %>% 
  mutate(Series = factor(symbol, levels=rev(list_yields))) %>%
  ggplot(market_data, 
         mapping = aes(date, price, color = Series)) +
    geom_line() +
    labs(x="Date", y="Yield (Percent)",
         title="Yield Time Series from Federal Reserve Economic Database (FRED) \nUS Treasurys (3mo,1,5,10 yr) \nCorporates (AAA, BAA)")
```  
 
 
 

\newpage

## 3.9 Crude Oil Futures

```{r,echo=TRUE}
market_data %>% 
  filter(symbol == "DCOILWTICO") %>% 
  ggplot(market_data, mapping = aes(date, price)) +
  geom_line() +
  geom_abline(intercept=0,slope=0,col='red',lwd=2)+  
  labs(x="Date", y="Price", title="Time Series of Crude Oil Future (DCOILWTICO)")
```

\newpage
# 4. Save R workspace with all time series 
```{r,echo=TRUE}

save(file="data_fm_intro1.Rdata",list=ls())
```



