[英]clean way to download multiple time series from Bloomberg in R
i am trying to download some time series data about euro swaps (EUSA10 Currency for example) in R using the blpapi but i am encountering the following problems:我正在尝试使用 blpapi 在 R 下载有关欧元掉期(例如 EUSA10 货币)的一些时间序列数据,但我遇到以下问题:
include.non.trading.days=FALSE
option, the resulting time series are for some reason of different length and i receive a message error about it.include.non.trading.days=FALSE
选项下载例如 2y、5y、10y 和 30y 掉期利率,生成的时间序列由于某种原因长度不同,我收到一条关于它的消息错误。 If, on the other hand i set the non trading day option to true i have similar length time series that can then be cleaned up using the na.omit() function[date][security][date][security2]......[date][securityN]
.[date][security][date][security2]......[date][securityN]
。 Any suggestions on how to solve this? Below a quick few lines i wrote as an example下面我写了几行作为例子
# Load package
library(Rblpapi)
# Connect to Bloomberg
blpConnect()
# Declaring securities
sec<-c("eusa2 curncy", "eusa5 curncy", "eusa10 curncy")
# Declaring field to be dowloaded
flds<-"PX_LAST"
data<-as.data.frame(bdh(sec,flds,start.date=as.Date("2019-08-18"),end.date=as.Date("2020-08-18"), include.non.trading.days=TRUE"))
It's states in the Rblapi manual that the Rblapi::bdh
returns Rblapi手册中指出
Rblapi::bdh
返回
A list with as a many entries as there are entries in securities;
与证券条目一样多的条目列表; each list contains a data.frame with one row per observations and as many columns as entries in fields.
每个列表包含一个 data.frame,每个观察值一行,列数与字段中的条目一样多。 If the list is of length one, it is collapsed into a single data frame.
如果列表的长度为 1,则它会折叠成一个数据框。 Note that the order of securities returned is determined by the backend and may be different from the order of securities in the securities field.
请注意,返回的证券顺序由后端确定,可能与证券领域的证券顺序不同。
So I'd suggest you rbind
the data then reshape it in order to have the result you want.所以我建议你
rbind
数据然后重塑它以获得你想要的结果。 a fast way to do it is use the data.table::rbindlist
function it takes a list as input and returns a data.table
containing all entries and if idcol=TRUE
then it'll append a .id
column showing where the data.frame came from.一个快速的方法是使用
data.table::rbindlist
function 它需要一个列表作为输入并返回一个包含所有条目的data.table
并且如果idcol=TRUE
那么它将 append 一个.id
列显示数据帧的来源。 Also this method will work even if you have different number of rows in the data.frame
s resulting from the Rblapi::bdh
call.即使您在
Rblapi::bdh
调用产生的data.frame
中有不同数量的行,此方法也将起作用。
# Declaring field to be dowloaded
flds<-"PX_LAST"
# LOADING THE DATA FROM THE API
l <- bdh(sec,flds,start.date=as.Date("2019-08-18"),end.date=as.Date("2020-08-18"), include.non.trading.days=TRUE)
# the names of the securities columns as returned by the api
securities <- paste0("eusa", c(2,5,10,15,30), ".curncy.",flds)
# row binding the resulting list
dt <- data.table::rbindlist(l, idcol=T, use.names=FALSE)
# idcol=T appends an id column (.id) to the resulting data.table
# use.names=F because the columns of the data.frames are different
# remaking the .id column so it reflects the name of the column that it already had
dt[, .id:= securities[.id] ]
# making a wider data.table
data.table::dcast(dt, eusa2.curncy.date ~ .id, value.var=securities[1])
# eusa2.curncy.date is the column that defines a group of observation
# .id the name of the columns
# securities[1] or eusa2.curncy.PX_LAST is the column that contains the values
As I don't have access to a bloomberg api endpoint I created this mock data which resemble the output of dbh
由于我无权访问 bloomberg api 端点,因此我创建了类似于
dbh
的 output 的模拟数据
col.names <- paste0("eusa", rep(c(2,5,10,15,30),each=2), ".curncy.", rep(c(flds,"date"), 5))
l<-rep(list(data.frame(rnorm(200), 1:200)), 5)
for (i in 1:length(l)) colnames(l[[i]]) <- col.names[(2*i-1):(2*i)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.