Skip to content

IBrokers getting quotes

2 messages · Mark Breman, Jeff Ryan

#
Hi Mark,

There is functionality in IBrokers for quotes, though the quotes are
via the snapshot parameter and the official API.  This roughly
translates to they are not very timely (as far as I can tell).

Firstly, update to the newest version of IBrokers and the TWS (where
the following is tested).

reqMktData(tws, list(twsSTK("AAPL"),twsSTK("SBUX")), snapshot=TRUE)

        lastTimeStamp symbol bidSize bidPrice askPrice askSize lastPrice Volume
1 2009-08-26 14:22:18   AAPL       1   167.32   167.33       2    167.32  89233
2 2009-08-26 14:21:40   SBUX     142    19.32    19.33     210     19.33  67075
    Open   High    Low Close
1 168.94 169.55 166.76 169.4
2  19.45  19.68  19.21  19.5

As you can see, you can pass a list of symbols into the call to get a
data frame of quotes back.  The other thing that is rather obvious
from the output is that the lastTimeStamp is different for each quote.
 The official (IB) API docs would be where to look to identify exactly
what that implies.

Aside from that, the most recent version on CRAN (and R-forge) make
use of a few shortcuts to get what you want.  They also serve as
decent examples of what you can do.

?eWrapper.MktData.CSV and eWrapper.data (same help file)

This will print to the screen [file(s)] a csv representation of bid
and ask, as new prices arrive.
20090826 14:33:01.414505,143,19.31,,,,,
20090826 14:33:01.417832,,,19.32,211,,,
20090826 14:33:01.419186,,,,,19.31,,
20090826 14:33:01.420762,143,,,,,,
20090826 14:33:01.422060,,,,211,,,
20090826 14:33:01.423350,,,,,,1,
20090826 14:33:01.424725,,,,,,,68830
20090826 14:33:01.428188,143,19.31,,,,,
20090826 14:33:01.429453,143,,,,,,

The internal messages from the TWS are complex, so your original
approach is simply missing the actual raw data that you want.  Looking
at the above eWrapper.MktData.CSV function call for tickPrice:
function (curMsg, msg, timestamp, file, ...)
{
    tickType = msg[3]
    msg <- as.numeric(msg)
    id <- as.numeric(msg[2])
    file <- file[[id]]
    data <- eW$get.Data("data")
    attr(data[[id]], "index") <- as.numeric(Sys.time())
    nr.data <- NROW(data[[id]])
    if (tickType == .twsTickType$BID) {
        cat(paste(timestamp, msg[5], msg[4], "", "", "", "",
            "", sep = ","), "\n", file = file, append = TRUE)
        data[[id]][nr.data, 1:2] <- msg[5:4]
    }
    else if (tickType == .twsTickType$ASK) {
        cat(paste(timestamp, "", "", msg[4], msg[5], "", "",
            "", sep = ","), "\n", file = file, append = TRUE)
        data[[id]][nr.data, 3:4] <- msg[4:5]
    }
    else if (tickType == .twsTickType$LAST) {
        cat(paste(timestamp, "", "", "", "", msg[4], "", "",
            sep = ","), "\n", file = file, append = TRUE)
        data[[id]][nr.data, 5] <- msg[4]
    }
    eW$assign.Data("data", data)
    c(curMsg, msg)
}
<environment: 0x30ee558>

Gives you a handle on what is happening on the IB/TWS side.  tickPrice
is a top level handler for many types of 'price's.  Essentially the
3rd element in the message is the "tickType".  This then has further
branching/processing.

One item of note, if you are just looking to capture last (current)
bid-ask, the variable "data" inside the eWrapper closure will be
maintaining the state you are looking for.
20090826 14:36:48.641869,126,19.28,,,,,
20090826 14:36:48.643727,,,19.29,230,,,
20090826 14:36:48.645176,,,,,19.28,,
20090826 14:36:48.646720,126,,,,,,
20090826 14:36:48.647979,,,,230,,,
20090826 14:36:48.649229,,,,,,1,
20090826 14:36:48.650573,,,,,,,70163
20090826 14:36:48.659182,126,19.28,,,,,
20090826 14:36:48.660954,126,,,,,,
20090826 14:36:48.662304,,,19.29,230,,,
20090826 14:36:48.663892,,,,230,,,
^C
[[1]]
                          BidSize BidPrice AskPrice AskSize  Last LastSize
2009-08-26 14:36:48.66338     126    19.28    19.29     230 19.28        1
                          Volume
2009-08-26 14:36:48.66338  70163

This can be useful in custom CALLBACK calls.

To create your own eWrapper, look at the eWrapper.MktData.CSV code, as
it should give guidance as to what is doable.

HTH
Jeff
On Thu, Aug 27, 2009 at 1:55 AM, Mark Breman<breman.mark at gmail.com> wrote: