# Packages and helpers
install.packages("tidyquant")
library(tidyverse)
library(tidyquant)
library(skimr)
library(timetk)
theme_set(theme_minimal())Should your fund invest in Bitcoin?
๐ Background
You work as an analyst at an investment fund in New York. Your CFO wants to explore if it is a good idea to invest some of the fund's assets in Bitcoin. You have to prepare a report on this asset and how it compares to the stock market in general.
๐พ The data
You have access to three files:
Bitcoin daily data in US dollars
- "date" - date from September 17, 2014 to November 17, 2021
- "open" - the price at the beginning of the trading day
- "high" - the highest price reached that day
- "low" - the lowest price reached that day
- "close" - the price at the closing of the trading day
- "volume" - how many Bitcoin were traded that day
bitcoin <- read_csv('./data/bitcoin-usd.csv')
bitcoin %>%
skim_without_charts()S&P 500 daily data
- "date" - date from September 17, 2014 to November 17, 2021
- "open" - the index level at the beginning of the trading day
- "high" - the highest level reached that day
- "low" - the lowest level reached that day
- "close" - the level at the closing of the trading day
- "volume" - how many shares in the companies that make up the index were traded that day
sp500 <- read_csv('./data/sp500.csv')
sp500 %>%
skim_without_charts()inflation and gold as monthly data
- "date" - date from September, 2014 to November, 2021
- "gold_usd" - price in usd of gold for that month
- "cpi_us" - the inflation index for the US for that month (cpi = consumer price index)
CPI data from the U.S. Bureau of Labor Statistics (https://www.bls.gov/cpi/). Publicly available information.
gold_cpi <- read_csv('./data/monthly_data.csv')
gold_cpi %>%
skim_without_charts()๐ช Competition challenge
Create a report that covers the following:
-
How does the performance of Bitcoin compare to the S&P 500 and the price of gold?
-
Analyze Bitcoin's returns and volatility profile. Do you believe it could help improve the performance of a portfolio? Do you believe Bitcoin could be used as a hedge versus inflation?
- The CFO is looking to lower volatility in the fund. Explore building a portfolio using some or all of these assets. Make a recommendation that minimizes overall risk.
Strategy
In order to answer those questions we will first analyze and compare the individual assets to answer questions 1 and 2 and then consolidate some possible portfolios to answer question 3
What about the individual stocks ?
First we create a dataset with monthly periodicity from Oct 2014 to Oct 2021 in orther to compare the assets returns.
# Making dates and periodicity of prices consistent in order to compare returns
get_monthly_returns <- function(tbl, var, col_rename){
tbl %>%
tq_transmute(select = var,
mutate_fun = periodReturn,
period = "monthly",
leading = F,
indexAt = "lastof",
col_rename = col_rename)
}
select_and_fix_date <- function(tbl,var){
tbl %>%
select(date, ) %>%
mutate(date = date-days(1))
}
inf_rate <- gold_cpi %>%
select_and_fix_date(cpi_us) %>%
get_monthly_returns(cpi_us,"inf_rate")
assets_return <- bitcoin %>%
get_monthly_returns(close,"btc_usd") %>%
inner_join(
sp500 %>%
get_monthly_returns(close,"gspc"),
by = "date"
) %>%
inner_join(
gold_cpi %>%
select_and_fix_date(gold_usd) %>%
get_monthly_returns(gold_usd,"gold_usd"),
by = "date"
) %>%
slice(-1) %>%
pivot_longer(-date,names_to = "symbol",values_to = "return") %>%
# Taking account for inflation
left_join(inf_rate, by = "date") %>%
mutate(inf_adj_return = (1+return)/(1+inf_rate)-1)
Comparing returns and volatility
โ
โ