Created
February 6, 2023 23:21
-
-
Save josephdunn/f3ff09c759d6afd65920206ac39ae537 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(xts) | |
library(crypto2) | |
coins <- crypto_list(only_active = TRUE) | |
btc_hist <- data.frame(crypto_history(coins, limit = 1, start_date = '20180101', | |
end_date = '20220101', interval = 'daily', finalWait = FALSE)) | |
# intervals <- c(5, 10, 20, 50, 200) | |
intervals <- seq(10, 100, 10) | |
closes <- xts(btc_hist$close, as.Date(btc_hist$timestamp)) | |
# make future (logrets at different future distances) | |
future.intervals <- c(1, 2, 5, 10, 15, 20, 25, 30, 40, 50) | |
future <- setNames(do.call(cbind, lapply(future.intervals, function(x) | |
{ | |
log(lag(closes, k = -x)) - log(closes) | |
})), paste0('future.', future.intervals)) | |
# 0/1 sig | |
c1 <- na.omit(cbind(closes, setNames(do.call(cbind, lapply(intervals, function(x) | |
{ | |
ifelse(closes == rollapply(closes, x, min), 1, 0) | |
})), paste0('sig.min.', intervals)), setNames(do.call(cbind, lapply(intervals, function(x) | |
{ | |
ifelse(closes == rollapply(closes, x, max), 1, 0) | |
})), paste0('sig.max.', intervals)), future)) | |
# model builder | |
buildModels <- function(d, future.intv, intv) | |
{ | |
lapply(future.intv, function(x) | |
{ | |
f <- as.formula(paste0('future.', x, ' ~ ', paste0('sig.min.', intv, collapse = ' + '), | |
' + ', paste0('sig.max.', intv, collapse = ' + '), ' + 0')) | |
lm(f, data = d) | |
}) | |
} | |
# build some models | |
c1.models <- buildModels(c1, future.intervals, intervals) | |
# TOOD: do something with the models, e.g.: | |
# lapply(c1.models, summary) | |
# or | |
# sapply(c1.models, function(x) { summary(x)$r.squared })) | |
# 'quantile of day's price relative to a rolling set of prices' | |
c2 <- na.omit(cbind(closes, setNames(xts(do.call(cbind, lapply(intervals, function(x) | |
{ | |
ecdf(rollapply(closes, x, min))(closes) | |
})), index(closes)), paste0('sig.min.', intervals)), setNames(xts(do.call(cbind, | |
lapply(intervals, function(x) | |
{ | |
ecdf(rollapply(closes, x, max))(closes) | |
})), index(closes)), paste0('sig.max.', intervals)), future)) | |
# build more models | |
c2.models <- buildModels(c2, future.intervals, intervals) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment