我有以下两个功能(除了各行之外,它们是相同的):
start_dates <- systemtime - 3600*24*(1:10)
end_dates <- systemtime - 3600*24*(0:9)
3600*24*(1:10)
将获得每小时数据的最后10个周期,而3600*24*(0:9)
将获得t-1
个周期。问题是,由于连接ping或超时,我根本无法应用3600*24*(1:100)
并无法获得最近的100个周期。但是我可以得到3600*24*(11:20)
或接下来的10个甚至更远的时间,例如
start_dates <- systemtime - 3600*24*(80:100)
end_dates <- systemtime - 3600*24*(79:99)
数据存在于网站上,所以我想减慢连接速度或在每个请求之间稍加停顿。只想知道如何在请求之间添加sys.sleep()
。也许
3600*24*(1:2)
sys.sleep()
3600*24*(3:4)
sys.sleep()
etc...
功能1:
library(png)
library(IKTrading)
require(PerformanceAnalytics)
library(e1071)
library(caret)
library(class)
###########################################################################
systemtime <- Sys.time()
# Variables
product_id = "ETH-EUR"
granularity = 3600
start_dates <- systemtime - 3600*24*(1:10) # considering the last 10 days #use 86400*(1:10) when granuality = 300, use 1440*(1:10) when grauality = 60
end_dates <- systemtime - 3600*24*(0:9) #use 86400*(0:9) when granuality = 300
initDate = min(start_dates)
# 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
}
# Looping
res <- lapply(seq_along(end_dates),
function (k) coinbaseGET(product_id, start_dates[k], end_dates[k], granularity))
res <- do.call(rbind, res)
colnames(res) <- c("date", "low", "high", "open", "close", "volume")
功能2:
systemtime <- Sys.time()
# Variables
product_id = "ETH-EUR"
granularity = 3600
start_dates <- systemtime - 3600*24*(80:100)
end_dates <- systemtime - 3600*24*(79:99)
initDate = min(start_dates)
# 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
}
# Looping
res <- lapply(seq_along(end_dates),
function (k) coinbaseGET(product_id, start_dates[k], end_dates[k], granularity))
res <- do.call(rbind, res)
colnames(res) <- c("date", "low", "high", "open", "close", "volume")