Chapter 2 Data sources

  • Xuan Lian is responsible for collecting the data.

2.1 Yahoo Finance API

  • The crypto and traditional stocks data will be obtained by Yahoo Finance Website YH Finance

  • We need following two packages to import data.

    • quantmod

    • TTL

Here we show the code of extracting the Bitcoin Data of the latest three days.

# install.packages('quantmod')
# install.packages('TTR')
library('TTR')
library('quantmod')
df_btc <- getSymbols('BTC-USD',src='yahoo',auto.assign=FALSE)
tail(df_btc,3)
##            BTC-USD.Open BTC-USD.High BTC-USD.Low BTC-USD.Close BTC-USD.Volume
## 2022-05-03  38528.10938  38629.99609 37585.62109   37750.45312    27326943244
## 2022-05-04  37748.01172  39902.94922 37732.05859   39698.37109    36754404490
## 2022-05-05  39727.38672  39788.44141 38256.51172   38256.51172    35743780864
##            BTC-USD.Adjusted
## 2022-05-03      37750.45312
## 2022-05-04      39698.37109
## 2022-05-05      38256.51172

2.2 Google Trend API

To quantify the social impact of the cryptos, we use Google Trend as the measurement and retrieve Google Trends data via gtrendsR package.

The gtrendsR package is on CRAN and can be installed via

install.packages("gtrendsR")

Here we show the code of retrieving and displaying the Google Trend ‘interest over time’ data for the keyword ‘Bitcoin’ in the United States.

# install.packages("gtrendsR")
library(gtrendsR)
Bitcoin_US_trend <- gtrends(c("Bitcoin"), geo = c("US"))
print(tail(Bitcoin_US_trend$interest_over_time,3))
##           date hits keyword geo      time gprop category
## 259 2022-04-17   13 Bitcoin  US today+5-y   web        0
## 260 2022-04-24   13 Bitcoin  US today+5-y   web        0
## 261 2022-05-01   10 Bitcoin  US today+5-y   web        0

2.3 CoinMarketCap

We are also interested in the data of the market cap of cryptocurrencies, so we will also use CoinMarketCap website to get historical market cap data of cryptocurrencies.

We will use rvest package to get the information from the website. We are interested in the yearly market cap data, so we get the historical snapshot at December 31th from 2016 to 2021 for the top 20 market cap cryptocurrencies and ignore the others.

base_url = 'https://coinmarketcap.com/historical/'

for(year in seq(2016,2021)){
  url = paste0(base_url,year,'1231')
  dt = read_html(url) %>% html_table(fill = TRUE)
  dt = dt[3][[1]] %>% dplyr::select(Symbol, `Market Cap`) %>%
    mutate('year' = year) %>%
    slice_head(n=20)
  if(year == 2016){
    crypto_market_cap = as_tibble(dt)
  }
  else{
    crypto_market_cap = rbind(crypto_market_cap,dt)
  }
}
print(head(crypto_market_cap))
## # A tibble: 6 × 3
##   Symbol `Market Cap`        year
##   <chr>  <chr>              <int>
## 1 BTC    $15,492,555,878.41  2016
## 2 ETH    $696,993,349.65     2016
## 3 XRP    $234,334,889.55     2016
## 4 LTC    $212,503,030.89     2016
## 5 XMR    $188,311,149.77     2016
## 6 ETC    $123,523,126.75     2016