将盘中数据加载到 R 中以使用 quantmod 进行处理
Loading intraday data into R for handling it with quantmod
我需要修改this example code for using it with intraday data which I should get from here and from here。据我了解,该示例中的代码适用于任何历史数据(或不适用于?),因此我的问题归结为以必要的格式(我的意思是每天或当天)加载初始数据的问题。
我也是从this question, it is impossible to load intraday data with getSymbols()
. I tried to download that data into my hard-drive and to get it then with a read.csv()
function, but this approach didn't work as well. Finally, I found few solutions of this problem in various articles (e.g. here)的回答中了解到的,但是它们似乎都很复杂"artificial"。
所以,我的问题是如何从程序员的角度优雅而正确地将给定的日内数据加载到给定的代码中,而不是重新发明轮子?
P.S。我对 R 和 quantstrat 中的时间序列分析非常陌生,因此如果我的问题似乎很模糊,请告诉我您需要知道什么才能回答它。
如果没有 "reinventing the wheel",我不知道该怎么做,因为我不知道任何现有的解决方案。不过使用自定义函数非常容易。
intradataYahoo <- function(symbol, ...) {
# ensure xts is available
stopifnot(require(xts))
# construct URL
URL <- paste0("http://chartapi.finance.yahoo.com/instrument/1.0/",
symbol, "/chartdata;type=quote;range=1d/csv")
# read the metadata from the top of the file and put it into a usable list
metadata <- readLines(paste(URL, collapse=""), 17)[-1L]
# split into name/value pairs, set the names as the first element of the
# result and the values as the remaining elements
metadata <- strsplit(metadata, ":")
names(metadata) <- sub("-","_",sapply(metadata, `[`, 1))
metadata <- lapply(metadata, function(x) strsplit(x[-1L], ",")[[1]])
# convert GMT offset to numeric
metadata$gmtoffset <- as.numeric(metadata$gmtoffset)
# read data into an xts object; timestamps are in GMT, so we don't set it
# explicitly. I would set it explicitly, but timezones are provided in
# an ambiguous format (e.g. "CST", "EST", etc).
Data <- as.xts(read.zoo(paste(URL, collapse=""), sep=",", header=FALSE,
skip=17, FUN=function(i) .POSIXct(as.numeric(i))))
# set column names and metadata (as xts attributes)
colnames(Data) <- metadata$values[-1L]
xtsAttributes(Data) <- metadata[c("ticker","Company_Name",
"Exchange_Name","unit","timezone","gmtoffset")]
Data
}
我会考虑将类似的东西添加到 quantmod,但它需要进行测试。我在 15 分钟内写完了这篇文章,所以我确定会有一些问题。
我需要修改this example code for using it with intraday data which I should get from here and from here。据我了解,该示例中的代码适用于任何历史数据(或不适用于?),因此我的问题归结为以必要的格式(我的意思是每天或当天)加载初始数据的问题。
我也是从this question, it is impossible to load intraday data with getSymbols()
. I tried to download that data into my hard-drive and to get it then with a read.csv()
function, but this approach didn't work as well. Finally, I found few solutions of this problem in various articles (e.g. here)的回答中了解到的,但是它们似乎都很复杂"artificial"。
所以,我的问题是如何从程序员的角度优雅而正确地将给定的日内数据加载到给定的代码中,而不是重新发明轮子?
P.S。我对 R 和 quantstrat 中的时间序列分析非常陌生,因此如果我的问题似乎很模糊,请告诉我您需要知道什么才能回答它。
如果没有 "reinventing the wheel",我不知道该怎么做,因为我不知道任何现有的解决方案。不过使用自定义函数非常容易。
intradataYahoo <- function(symbol, ...) {
# ensure xts is available
stopifnot(require(xts))
# construct URL
URL <- paste0("http://chartapi.finance.yahoo.com/instrument/1.0/",
symbol, "/chartdata;type=quote;range=1d/csv")
# read the metadata from the top of the file and put it into a usable list
metadata <- readLines(paste(URL, collapse=""), 17)[-1L]
# split into name/value pairs, set the names as the first element of the
# result and the values as the remaining elements
metadata <- strsplit(metadata, ":")
names(metadata) <- sub("-","_",sapply(metadata, `[`, 1))
metadata <- lapply(metadata, function(x) strsplit(x[-1L], ",")[[1]])
# convert GMT offset to numeric
metadata$gmtoffset <- as.numeric(metadata$gmtoffset)
# read data into an xts object; timestamps are in GMT, so we don't set it
# explicitly. I would set it explicitly, but timezones are provided in
# an ambiguous format (e.g. "CST", "EST", etc).
Data <- as.xts(read.zoo(paste(URL, collapse=""), sep=",", header=FALSE,
skip=17, FUN=function(i) .POSIXct(as.numeric(i))))
# set column names and metadata (as xts attributes)
colnames(Data) <- metadata$values[-1L]
xtsAttributes(Data) <- metadata[c("ticker","Company_Name",
"Exchange_Name","unit","timezone","gmtoffset")]
Data
}
我会考虑将类似的东西添加到 quantmod,但它需要进行测试。我在 15 分钟内写完了这篇文章,所以我确定会有一些问题。