使用R中的Rbbg(RBloomberg)包中的bar()函数拉出分钟数据

时间:2013-08-12 21:40:25

标签: r bloomberg

我对Rbbg程序包是相当新的,所以请原谅我的任何无知,但我想知道是否可以使用bar()函数提取超过~25天的分钟数据。我发现我不能提取超过25或26天的数据,我想知道我做错了什么,或者是否有可能。

以下是我正在使用的代码:

#install.packages("rJava")
#install.packages("Rbbg", repos="http://r.findata.org")
#install.packages("timeDate")

library(rJava)
library(Rbbg)
library(timeDate)

conn <- blpConnect()

weekdays = timeSequence(from = (Sys.Date()-38), to = (Sys.Date()-1), by = "day")[isWeekday(timeSequence(from = (Sys.Date()-38), to = (Sys.Date()-1), by = "day"))]

date_time=numeric()
volume=numeric()
for(i in 1:length(weekdays)){

start.date <- paste(weekdays[i],"13:30:00.000")
end.date <- paste(weekdays[i],"20:00:00.000")

raw=bar(conn, "GOOG US Equity", "TRADE", start.date, end.date, "1")

date_time=append(date_time,raw$time)
volume=append(volume,raw$volume)
}

date.time <- data.frame(do.call('rbind', strsplit(as.character(date_time),'T',fixed=TRUE)))
use=data.frame("date"=format(as.Date(date.time$X1), "%m/%d/%Y"),"time"=date.time$X2,"volume"=raw$volume)

blpDisconnect(conn)

此工作正常并从过去3.5周的交易日中提取数据。现在,如果我尝试在行weekdays = timeSequence(from = (Sys.Date()-38), to = (Sys.Date()-1), by = "day")[isWeekday(timeSequence(from = (Sys.Date()-38), to = (Sys.Date()-1), by = "day"))]中将38更改为更大的数字,则在尝试运行上述脚本时会出现以下错误:Error in matrix.data[, 1] : subscript out of bounds

API是否只能提取约3.5周的分钟数据?或者我做错了什么?理想情况下,我希望至少有100个交易日的数据。

谢谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

布隆伯格提供六个月的日内数据。如果我使用我们的R包RbbgExtension中的函数BarData(可以在GitHub找到)在谷歌上,那么六个月后我会得到1分钟的酒吧。只是为了澄清为什么我们将时间转换为GMT,这是因为欧洲股票在多个时区交易但同时开始,因此要获得时间一致的数据,我们将价格转换为GMT。

> goog.bar <- BarData(tickers = "GOOG US",
+                     type = "Equity",
+                     start.date.time = "2014-01-01 09:30:00",
+                     end.date.time = "2015-01-21 16:05:00",
+                     time.zone = "America/New_York",
+                     interval = "1")
R version 3.1.0 (2014-04-10) 
rJava Version 0.9-6 
Rbbg Version 0.5.2 
Java environment initialized successfully.
Looking for most recent blpapi3.jar file...
Adding C:\blp\API\APIv3\JavaAPI\v3.7.1.1\lib\blpapi3.jar to Java classpath
Bloomberg API Version 3.7.1.1 
> head(goog.bar)
                       open   high    low   close numEvents volume
2014-07-10 13:30:00 565.910 566.85 565.01 565.745       142  38862
2014-07-10 13:31:00 565.500 566.06 565.01 565.190        49   7606
2014-07-10 13:32:00 565.110 566.70 565.11 566.350        49   6878
2014-07-10 13:33:00 566.390 566.60 566.00 566.600        34   4473
2014-07-10 13:34:00 566.470 566.48 565.65 565.850        52   6365
2014-07-10 13:35:00 565.775 566.45 565.55 566.050       107  14513
Warning message:
timezone of object (GMT) is different than current timezone (). 
> tail(goog.bar)
                      open    high    low   close numEvents volume
2015-01-21 20:54:00 518.00 518.520 518.00 518.275       135  15492
2015-01-21 20:55:00 518.31 518.500 518.22 518.230       116  14048
2015-01-21 20:56:00 518.23 518.374 518.22 518.350        83   9696
2015-01-21 20:57:00 518.35 518.390 518.18 518.250        99  12289
2015-01-21 20:58:00 518.25 518.640 518.20 518.420       116  15353
2015-01-21 20:59:00 518.39 518.390 517.80 518.040       309 101288
Warning message:
timezone of object (GMT) is different than current timezone ().

然而,我能够让你犯错误。如果我没有指定实际的结束日期/时间,那么输出就会突出显示。所以关键在于结束日期/时间。

> goog.bar <- BarData(tickers = "GOOG US",
+                     type = "Equity",
+                     start.date.time = "2014-01-01 09:30:00",
+                     time.zone = "America/New_York",
+                     interval = "1")
R version 3.1.0 (2014-04-10) 
rJava Version 0.9-6 
Rbbg Version 0.5.2 
Java environment initialized successfully.
Looking for most recent blpapi3.jar file...
Adding C:\blp\API\APIv3\JavaAPI\v3.7.1.1\lib\blpapi3.jar to Java classpath
Bloomberg API Version 3.7.1.1 
> head(goog.bar)
                       open   high    low   close numEvents volume
2014-07-10 13:30:00 565.910 566.85 565.01 565.745       142  38862
2014-07-10 13:31:00 565.500 566.06 565.01 565.190        49   7606
2014-07-10 13:32:00 565.110 566.70 565.11 566.350        49   6878
2014-07-10 13:33:00 566.390 566.60 566.00 566.600        34   4473
2014-07-10 13:34:00 566.470 566.48 565.65 565.850        52   6365
2014-07-10 13:35:00 565.775 566.45 565.55 566.050       107  14513
Warning message:
timezone of object (GMT) is different than current timezone (). 
> tail(goog.bar)
                       open    high     low   close numEvents volume
2014-08-15 19:54:00 573.445 573.695 573.445 573.620        30   4428
2014-08-15 19:55:00 573.675 573.710 573.550 573.570        54   6496
2014-08-15 19:56:00 573.617 573.715 573.340 573.430       102  12067
2014-08-15 19:57:00 573.585 573.600 573.330 573.375        36   4300
2014-08-15 19:58:00 573.375 573.730 572.920 573.000       181  22467
2014-08-15 19:59:00 572.990 573.500 572.780 573.480       158  43183
Warning message:
timezone of object (GMT) is different than current timezone ().