# install.packages("lubridate") library("lubridate") # Read in after downloading the .csv file from CryptoCompare price <- read.csv(file.choose()) price <- price[,-3:-8] # Choose time period start <- ymd("2016-01-01") end <- ymd("2017-08-01") # Select the period of prices price$time <- ymd(price$time) int <- interval(start, end) price <- price[price$time %within% int,] # Calculate the return and plot the hisogram numerator <- diff(price[,2], k=1) denominator <- head(price[,2], -1) Return <- numerator/denominator hist(Return) # Value at Risk VaRhistorical <- function(returnVector, alpha=.95, digits=2) { result <- -quantile(returnVector, 1- alpha) signif(result, digits=digits) } VaRresult <- VaRhistorical(Return) cat("VaR at .95:", VaRresult) # Expected Shortfall EShistorical <- function(returnVector, alpha=.95, digits=2) { v <- quantile(returnVector, 1- alpha) result <- -mean(returnVector[returnVector <= v]) signif(result, digits=digits) } ESresult <- EShistorical(Return) cat(" ES at .95:", ESresult)