这是来自here的有问题的追随者。感谢@nate提供了有用的解决方案。
我正在尝试从币库中收集价格数据。到目前为止,我的工作是:
start <- strftime(Sys.time() - 86400, "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
end <- strftime(Sys.time(), "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
product_id = "ETH-EUR"
granularity = 300
# request url
req.url <- paste0("https://api.pro.coinbase.com/products/", product_id, "/candles")
req.url
# fetching the data ourselves - the return
res <- httr::GET(url = req.url,
query = list(start = start, end = end,
granularity = granularity))
res <- as.data.frame(t(matrix(unlist(httr::content(res)), nrow = 6)))
res[['V1']] <- as.POSIXct(.subset2(res,1L), origin="1970-01-01")
c(min(res$V1),max(res$V1))
colnames(res) <- c("time", "low", "high", "open", "close", "volume")
其中给出以下内容:
> c(min(res$V1),max(res$V1))
[1] "2019-01-23 13:45:00 CET" "2019-01-24 13:40:00 CET"
(最近24小时的数据价值)
如果我将start
和end
时间更改为以下内容:
start <- strftime(Sys.time() - 86400*5, "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
end <- strftime(Sys.time(), "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
(收集最近5天的数据)-coinbase不允许收集太多数据。
但是可以收集:
t = 5 and t = 4
t= 4 and t = 3
t = 3 and t = 2
t = 2 and t = 1
t = 1 and t = 0
所以5天前收集数据,然后从该日期减去24小时。 类似于以下内容:
start <- strftime(Sys.time() - 86400*5, "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
end <- strftime(Sys.time() - 86400*(5-1), "%Y-%m-%dT%H:%M:%SZ", tz = "UTC")
哪个作品。因此,知道了这一点,我试图创建一个函数,该函数将“迭代”日期并提取信息。
我一直在从事以下工作-但我知道一个功能将比我目前的功能好100倍:
start <- NULL
end <- NULL
for(i in 1:5){
start[[i]] <- as.data.frame(strftime(Sys.time() - 86400*i, "%Y-%m-%dT%H:%M:%SZ", tz = "UTC"))
end[[i]] <- as.data.frame(strftime(Sys.time() - 86400*(i-1), "%Y-%m-%dT%H:%M:%SZ", tz = "UTC"))
}
start <- do.call(rbind.data.frame, start)
end <- do.call(rbind.data.frame, end)
colnames(start) <- "start"
colnames(end) <- "end"
for(i in 1:5){
res[[i]] <- httr::GET(url = req.url,
query = list(start = start[i, ], end = end[i, ],
granularity = 300))
}
您对如何将此功能应用于函数有任何提示吗?
答案 0 :(得分:1)
您可以创建这样的函数
# Function
coinbaseGET <- function (product_id, start, end, granularity) {
# request url
req.url <- paste0("https://api.pro.coinbase.com/products/", product_id, "/candles")
req.url
# fetching the data ourselves - the return
res <- httr::GET(url = req.url,
query = list(start = start, end = end,
granularity = granularity))
res <- as.data.frame(t(matrix(unlist(httr::content(res)), nrow = 6)))
res[['V1']] <- as.POSIXct(.subset2(res,1L), origin="1970-01-01")
res
}
例如使用lapply
# Variables
product_id = "ETH-EUR"
granularity = 300
start_dates <- Sys.time() - 86400*(1:10) # considering the last 10 days
end_dates <- Sys.time() - 86400*(0:9)
# Looping
res <- lapply(seq_along(end_dates),
function (k) coinbaseGET(product_id, start_dates[k], end_dates[k], granularity))
res <- do.call(rbind, res)
结果
str(res)
# 'data.frame': 2813 obs. of 6 variables:
# $ V1: POSIXct, format: "2019-01-24 13:55:00" "2019-01-24 13:50:00" "2019-01-24 13:45:00" "2019-01-24 13:40:00" ...
# $ V2: num 103 102 102 103 102 ...
# $ V3: num 103 103 103 103 103 ...
# $ V4: num 103 102 103 103 102 ...
# $ V5: num 103 103 102 103 103 ...
# $ V6: num 9.63 18.11 30.07 80.98 12.75 ...
# Checking the dates
c(min(res$V1), max(res$V1))
# [1] "2019-01-14 15:00:00 CET" "2019-01-24 13:55:00 CET"]