简体   繁体   English

在 R 中从 Bloomberg 下载多个时间序列的干净方法

[英]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 货币)的一些时间序列数据,但我遇到以下问题:

  1. if i try to download for example 2y, 5y, 10y and 30y swap rates using the 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另一方面,如果我将非交易日选项设置为 true 我有类似的长度时间序列,然后可以使用 na.omit() function 进行清理
  1. the format in which the data is downloaded is messy...i would like to have a data frame in which the first column is the date, second column is the first security, third column is second security and so forth.下载数据的格式很乱...我想要一个数据框,其中第一列是日期,第二列是第一证券,第三列是第二证券,依此类推。 Instead what i get is [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

data used使用的数据

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM