[R-SIG-Finance] create a new variable in a loop - Rbbg, RBloomberg

John Laing john.laing at gmail.com
Thu Jun 14 19:09:34 CEST 2012


Marco,

I would recommend against this approach of creating and renaming many
environment variables in a loop. Some packages (e.g.
quantmod::getSymbols) do exactly that, but I prefer to use a list.
This feels more consistent with the R idiom of applying a function to
a vector:

securities <- c("SPGLOB Index", "SPX Index", "SPE Index", "SPTPX
Index", "SPTSX60 Index", "AS31 Index", "SPA50 Index", "SPLAC Index")
fields <- c("PX_OPEN", "PX_HIGH", "PX_LOW", "PX_LAST", "PX_VOLUME")
startdate <- as.Date("2005-11-16")
enddate <- as.Date(Sys.Date())

bbg2xts <- function(security, ...) {
    bbg <- bdh(conn, security, ...)
    bbg$date <- NULL
    as.xts(bbg, order.by = as.Date(rownames(bbg)))
}

BBGData <- lapply(securities, bbg2xts, fields, startdate, enddate,
    option_names = c("nonTradingDayFillOption", "nonTradingDayFillMethod"),
    option_values = c("NON_TRADING_WEEKDAYS", "PREVIOUS_VALUE"))
names(BBGData) <- securities

You get back is a list of XTS objects, with elements named appropriately.

-John

On Thu, Jun 14, 2012 at 10:35 AM, Belgarath <marco.cora at googlemail.com> wrote:
> Hello Everyone,
>
> I have the following code:
>
> -----
> securities <- c("SPGLOB", "SPX Index", "SPE Index", "SPTPX Index",
>            "SPTSX60 Index", "AS31 Index", "SPA50 Index", "SPLAC Index")
> fields <- c("PX_OPEN", "PX_HIGH", "PX_LOW", "PX_LAST", "PX_VOLUME")
> startdate <- as.Date("2005-11-16")
> enddate <- as.Date(Sys.Date())
> BBGData <- bdh(conn, paste(securities[1],"Index",sep=" "),
>               fields, startdate, enddate,
>               option_names = c("nonTradingDayFillOption",
> "nonTradingDayFillMethod"),
>               option_values = c("NON_TRADING_WEEKDAYS", "PREVIOUS_VALUE"))
> BBGData$date=NULL
> BBGData2 <- as.xts(BBGData, order.by =as.Date(rownames(BBGData)))
>
> ----
>
> I would like to assign BBGData2 to a new variable with the name as
> securities[1] in order to be able to package it in a loop and download all
> the info for all the securities and create new variables for each.
>
> More generally how do I create new r variables in a Loop, sort of:
>
> name(securities[1]) <- BBGData2
>
> Alternatively how do I change a variable name, how to change the name of
> BBGData2 into SPGLOB for instance; at the next run of the loop the BBGData2
> would be recreated anyway.
>
> Thank you.
> Regards,
> Marco
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/create-a-new-variable-in-a-loop-Rbbg-RBloomberg-tp4633384.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> 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.



More information about the R-SIG-Finance mailing list