我试图用命令将对象保存到文本文件write.table(ESH2, "c:/ESH2.txt", sep=",").问题是保存的时间序列不包含我下载的日期和时间值.我使用了包twsInstrument和命令getBAT(ESH2)
使用该命令将其加载到R中时的数据 load(file = "C:/ESH2.Rdata")
ES.Bid.Price ES.Ask.Price ES.Trade.Price ES.Mid.Price ES.Volume
1323700200 1237.25 1237.50 1237.50 1237.375 6954
1324057980 1210.25 1210.50 1210.25 1210.375 3792
1324058040 1210.50 1211.00 1211.00 1210.750 3305
.........
.........
.........
1324058100 NA NA NA NA 823
attr(,".indexCLASS")
[1] POSIXct POSIXt
attr(,".indexTZ")
[1]
attr(,"from")
[1] 20111211 23:59:59
attr(,"to")
[1] 20111216 23:59:59
attr(,"src")
[1] IB
attr(,"updated")
[1] "2011-12-16 18:54:55 CET"
Run Code Online (Sandbox Code Playgroud)
第一列应显示Date_Time而不是1323700200.
我正在寻找一种简单的方法来每周下载一次数据并合并数据.
ps是的,我可以阅读教程/书籍来完成这一点,是的,我会这样做,但问题是我没有时间.我想本周开始收集数据,因为交互式经纪人正在限制数据请求1min data = 5DAYS maximum.我感谢任何帮助和建议.
您可能有一个xts或zoo对象,需要使用该write.zoo函数.如果我对"ESH2"对象的结构是正确的,那么你称之为"第一列"的数据实际上是rownames,在zoo/xts用语中是"索引",而数据可以通过coredata和是一个矩阵对象.
从read/write.zoo页面上的示例:
Lines <- "CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 100 0
CVX 20070201 9 30 51 73.25 300 0
CVX 20070201 9 30 51 73.25 81400 0
CVX 20070201 9 40 51 73.25 100 0
CVX 20070201 9 40 52 73.25 100 0
CVX 20070201 9 40 53 73.25 300 0"
z <- read.zoo(textConnection(Lines),
colClasses = c("NULL", "NULL", "numeric", "numeric", "numeric", "numeric",
"numeric", "NULL"),
col.names = c("Symbol", "Date", "Hour", "Minute", "Second", "Price",
"Volume", "junk"),
index = 1:3, # do not count columns that are "NULL" in colClasses
FUN = function(h, m, s) times(paste(h, m, s, sep = ":")),
FUN2 = function(tt) trunc(tt, "00:00:05"),
aggregate = mean)
# The only material I added.
write.zoo(z)
"Index" "Price" "Volume"
09:30:50 73.25 32660
09:40:50 73.25 166.666666666667
Run Code Online (Sandbox Code Playgroud)