1

I'm sorry this example won't be reproducible by those who aren't Bloomberg users.

For the others, I'm using Rblpapi and its subscribe function. I would like to create something like a data frame, a matrix or an array and fill it with values that are streamed by the subscription.

Assuming your BBComm component is up and running, my example says:

require(Rblpapi)
con <- blpConnect()
securities <- c('SX5E 07/20/18 C3400 Index',
            'SX5E 07/20/18 C3450 Index',
            'SX5E 07/20/18 C3500 Index')

I would like to fill a 3 x 2 matrix with these fields:

fields <- c('BID', 'ASK')

I guess I can create a matrix like this with almost no performance overhead:

mat <- matrix(data = NA,
          nrow = 3,
          ncol = 2)

Now I use subscribe and its argument fun for filling purposes, so something like this (albeit ugly to see and likely inefficient):

i <- 1
subscribe(securities = securities,
          fields = fields,
          fun = function(x){
            if (i > length(securities))
              i <<- 1
            tryCatch(
              expr = {
                mat[i, 1] <<- x$data$BID
                mat[i, 2] <<- x$data$ASK
                i <<- i + 1
              },
              error = function(e){
                message(e)
              },
              finally = {}
              )
          })

Result:

Error in subscribe_Impl(con, securities, fields, fun, options, identity) : 
  Evaluation error: number of items to replace is not a multiple of replacement length.

Of course, this doesn't work because I don't really know how to use indexing on streamed data. $ operator seems fine to retrieve data points by name - like I did with BID and ASK - but I cannot find a way to figure out which values are referring to, say, securities[1] or to securities[2]. It seems that I get a stream of numeric values that are indistinguishable one from each other because I cannot retrieve the ownership of the value among the securities.

Using an index on x$data$BID[1] throws the same error.

Lisa Ann
  • 3,345
  • 6
  • 31
  • 42
  • You should file a feature request with the Rblpapi-team. Alternatively, as a hack, run a separate R-session for each stock in a docker container - not ideal though... – sdittmar Jul 17 '19 at 12:21

2 Answers2

2

Ok your code looks fine, the only thing that does not work is x$data$BID, change to x$data["BID"] and then you can store it, Im working with your code and this is my result.

     fields=c("TIME","LAST_PRICE", "BID", "ASK")
     blpConnect()
     blpConnect()
     i <- 1

    subscribe(securities = securities,
    fields = fields,"interval=60",
      fun = function(x){
        if (i > length(securities))
          i <<- 1
        tryCatch(
          expr = {
            tim <- x$data["TIME"]
            last <<- x$data["LAST_PRICE"]
            ask <<- x$data["ASK"]
            bid <<- x$data["BID"]
            i <<- i + 1
          },
          error = function(e){
            message(e)
          },
          finally = {}
        )
        print(cbind(tim$TIME,last$LAST_PRICE,ask$ASK, bid$BID))
      })

result

  • Thank you for your help. Your example works, but from the output, I cannot infer which is the security currently displayed by `print`: is there any field with the security name or am I forced to call an additional field with some identifier (like security short name)? – Lisa Ann Sep 14 '18 at 09:06
1

A good way to take a look at the result object from the subscribe function is:

subscribe(securities=c("AAPL US Equity"),
          fields=c("LAST_PRICE"),
          fun=function(x) print(str(x)))

From there you can work your way into the data:

subscribe(securities=c("AAPL US Equity", "INTC US Equity"),
          fields=c("LAST_PRICE","BID","ASK"),
          fun=function(x) {
            if (!is.null(x$data$MKTDATA_EVENT_TYPE) && x$data$MKTDATA_EVENT_TYPE == "TRADE" && exists("LAST_PRICE", where = x$data)) {
              print(data.frame(Ticker = x$topic, DateTime = x$data$TRADE_UPDATE_STAMP_RT, Trade = x$data$LAST_PRICE))
            }
          })

I only printed the data.frame here. The data can be processed or stored directly using the FUN argument of subscribe.

sdittmar
  • 365
  • 2
  • 14