I'm not sure how to ask this with the proper terminology, but here goes:
The BDH() function in RBLPAPI returns, for a list of symbols (e.g., 'SPX Index','RIY Index','IBM Equity') a list of closing prices. The problem is that the result is not a matrix or a dataframe, but a list.
So, if I run the query with 3 symbols, I get a list with 3 elements. For example, in this case, if
symbolist <-c("SPX Index","MXWO Index","MXEA Index")
resultlist <- bdh(symbollist, "PX_LAST", options=opt,start.date=as.Date(begdate))
then resultlist is a list with 3 elements, and as many rows as there are dates between "begdate" and today (or as many month-ends, if "opt" declares monthly periodicity). Suppose in this case I've set this up to retrieve 60 dates.
But I don't WANT a list. I want a zoo object containing each of these as an element. I thought about starting by trying to put each element in a matrix by
data<-matrix(nrow=60,ncol=length(symbollist))
and then looping through from 1 to length(symbolist), letting
data[,i] <- resultlist$symbollist[i][,2]
but this clearly doesn't work since what I really want is
data[,1] <-resultlist$'SPX Index'[,2]
data[,2] <-resultlist$'MXWO Index'[,2]
etc
But there's probably a much easier way to do this.
I am sending this to both the general help list and the r-sig-finance list since there is probably both a general way to stuff a list into a zoo object and a way to do it cleanly with the BDH() command. Thanks in advance for help.
Mike
getting a subset corresponding to a list element
4 messages · Michael Ashton, Bob, Daniel Cegiełka +1 more
Hi Michael,
Try not to post twice - this is really more of a general R question. To
answer the question, however, turn each element of your resultlist into an
xts (or zoo) object so that you have a list of xts objects (called xtsList
for example.) Then call do.call("merge", xtsList). Also, your example is
tough because it requires access to bloomberg, which isn't necessarily the
case for the vast majority of R users.
Bob
On Fri, May 26, 2017 at 4:58 PM, Michael Ashton <
m.ashton at enduringinvestments.com> wrote:
I'm not sure how to ask this with the proper terminology, but here goes:
The BDH() function in RBLPAPI returns, for a list of symbols (e.g., 'SPX
Index','RIY Index','IBM Equity') a list of closing prices. The problem is
that the result is not a matrix or a dataframe, but a list.
So, if I run the query with 3 symbols, I get a list with 3 elements. For
example, in this case, if
symbolist <-c("SPX Index","MXWO Index","MXEA Index")
resultlist <- bdh(symbollist, "PX_LAST", options=opt,start.date=as.
Date(begdate))
then resultlist is a list with 3 elements, and as many rows as there are
dates between "begdate" and today (or as many month-ends, if "opt" declares
monthly periodicity). Suppose in this case I've set this up to retrieve 60
dates.
But I don't WANT a list. I want a zoo object containing each of these as
an element. I thought about starting by trying to put each element in a
matrix by
data<-matrix(nrow=60,ncol=length(symbollist))
and then looping through from 1 to length(symbolist), letting
data[,i] <- resultlist$symbollist[i][,2]
but this clearly doesn't work since what I really want is
data[,1] <-resultlist$'SPX Index'[,2]
data[,2] <-resultlist$'MXWO Index'[,2]
etc
But there's probably a much easier way to do this.
I am sending this to both the general help list and the r-sig-finance list
since there is probably both a general way to stuff a list into a zoo
object and a way to do it cleanly with the BDH() command. Thanks in advance
for help.
Mike
[[alternative HTML version deleted]]
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
2017-05-26 22:58 GMT+02:00 Michael Ashton <m.ashton at enduringinvestments.com>:
I'm not sure how to ask this with the proper terminology, but here goes:
So, if I run the query with 3 symbols, I get a list with 3 elements. For example, in this case, if
(...)
But I don't WANT a list. I want a zoo object containing each of these as an element.
Hi,
use my src.bloomberg.R custom code and try this:
require(xts)
# works with and without suffix 'Index' etc. :)
getSymbols.bloomberg(c('SPX', 'MXWO', 'MXEA'), from = as.Date(begdate))
# PX_LAST: SYM[,4] or Cl(SYM)
sym_data <- merge(SPX[,4], MXWO[,4], MXEA[,4])
tail(sym_data)
zoo_data <- as.zoo(sym_data)
tail(zoo_data)
btw. "to" arg is set as "to = Sys.Date() - 1" (EOD data)
getSymbols.bloomberg <- function(symbols = NULL, exchcode = 'US',
from = Sys.Date() - 365, to = Sys.Date() - 1, env = .GlobalEnv,
auto.assign, verbose, ...)
Best,
Daniel
-------------- next part --------------
A non-text attachment was scrubbed...
Name: src.bloomberg.R
Type: application/octet-stream
Size: 2401 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-finance/attachments/20170526/bbcf3415/attachment.obj>
2 days later
Zitat von Michael Ashton <m.ashton at enduringinvestments.com>:
I'm not sure how to ask this with the proper terminology, but here goes:
The BDH() function in RBLPAPI returns, for a list of symbols (e.g.,
'SPX Index','RIY Index','IBM Equity') a list of closing prices. The
problem is that the result is not a matrix or a dataframe, but a list.
So, if I run the query with 3 symbols, I get a list with 3 elements.
For example, in this case, if
symbolist <-c("SPX Index","MXWO Index","MXEA Index")
resultlist <- bdh(symbollist, "PX_LAST",
options=opt,start.date=as.Date(begdate))
then resultlist is a list with 3 elements, and as many rows as there
are dates between "begdate" and today (or as many month-ends, if
"opt" declares monthly periodicity). Suppose in this case I've set
this up to retrieve 60 dates.
But I don't WANT a list. I want a zoo object containing each of
these as an element. I thought about starting by trying to put each
element in a matrix by
data<-matrix(nrow=60,ncol=length(symbollist))
and then looping through from 1 to length(symbolist), letting
data[,i] <- resultlist$symbollist[i][,2]
but this clearly doesn't work since what I really want is
data[,1] <-resultlist$'SPX Index'[,2]
data[,2] <-resultlist$'MXWO Index'[,2]
etc
But there's probably a much easier way to do this.
I am sending this to both the general help list and the
r-sig-finance list since there is probably both a general way to
stuff a list into a zoo object and a way to do it cleanly with the
BDH() command. Thanks in advance for help.
Mike
Try this:
require("Rblpapi")
require("zoo")
blpConnect()
res <- bdh(c("SPX Index","MXWO Index","MXEA Index"),
fields = "PX_LAST",
start.date = as.Date("2017-01-01"),
end.date = as.Date("2017-01-05"))
do.call("merge",
lapply(res, function(x) zoo(x[[2]], x[[1]])))
Enrico Schumann Lucerne, Switzerland http://enricoschumann.net