[R-SIG-Finance] IBrokers getting quotes
Jeff Ryan
jeff.a.ryan at gmail.com
Thu Aug 27 16:29:03 CEST 2009
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.
> reqMktData(tws, twsSTK("SBUX"), eventWrapper=eWrapper.MktData.CSV(1))
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:
> eWrapper.MktData.CSV()$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.
> myWrapper <- eWrapper.MktData.CSV(1) # create a closure, which can maintain state
> reqMktData(tws, twsSTK("SBUX"), eventWrapper=myWrapper)
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
> myWrapper$get.Data("data")
[[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:
> Hi,
> I am using the IBrokers package to get data from IB, but now I have run into
> a little bit of trouble.
>
> I have a lot of stock symbols I have to get a quote for (bid/ask price
> only).
>
> It looks like there is not a specific function in IBrokers to get quotes, so
> I thought about filtering out the bid/ask price from the reqMktData()
> function as in the highlighted output (is there a easier way?):
>> reqMktData(tws, twsSTK("SBUX"))
> <20090826 18:18:52.859000> id=1 symbol= Volume: 53727
> <20090826 18:18:52.860000> id=1 symbol= highPrice: 19.68
> <20090826 18:18:52.861000> id=1 symbol= lowPrice: 19.21
> <20090826 18:18:52.862000> id=1 symbol= shortable: 3.0
> <20090826 18:18:53.087000> id=1 symbol= lastTimestamp: 1251310542
> <20090826 18:18:53.088000> id=1 symbol= lastTimestamp: 1251310728
> <20090826 18:18:53.089000> id=1 symbol= lastPrice: 19.4
> <20090826 18:18:53.090000> id=1 symbol= lastSize: 1
> <20090826 18:18:53.091000> id=1 symbol= Volume: 54057
> <20090826 18:18:53.091000> id=1 symbol= highPrice: 19.68
> <20090826 18:18:53.093000> id=1 symbol= lowPrice: 19.21
> <20090826 18:18:53.094000> id=1 symbol= closePrice: 19.5
> <20090826 18:18:53.094000> id=1 symbol= openPrice: 19.45
> <20090826 18:18:53.098000> id=1 symbol= bidPrice: 19.39 bidSize: 214
> <20090826 18:18:53.099000> id=1 symbol= askPrice: 19.4 askSize: 156
> <20090826 18:18:53.100000> id=1 symbol= bidSize: 214
> <20090826 18:18:53.100000> id=1 symbol= askSize: 156
>
> I tried to override the tickPrice function in my eWrapper (as in the
> documentation example):
>
>> myWrapper <- eWrapper()
>> myWrapper$tickPrice <- function(msg, timestamp, file, ...) {}
>>
>> # add new tickPrice action
>> myWrapper$tickPrice <- function(msg, timestamp, file, ...) {
> cat("tickPrice",msg) }
>
> Now when I call the reqMktData I get the followng output:
>
>> reqMktData(tws, twsSTK("SBUX"), eventWrapper=myWrapper)
> <20090826 17:56:13.473000> id=1 symbol= Volume: 51409
> tickPrice 1tickPrice 1tickPrice 1<20090826 17:56:13.592000> id=1 symbol=
> Volume: 51409
> tickPrice 1tickPrice 1<20090826 17:56:14.214000> id=1 symbol= lastTimestamp:
> 1251309292
> <20090826 17:56:14.231000> id=1 symbol= lastTimestamp: 1251309383
> tickPrice 1<20090826 17:56:14.240000> id=1 symbol= lastSize: 1
> <20090826 17:56:14.254000> id=1 symbol= Volume: 51515
> tickPrice 1tickPrice 1tickPrice 1tickPrice 1<20090826 17:56:14.269000> id=1
> symbol= lastTimestamp: 1251309292
> <20090826 17:56:14.276000> id=1 symbol= lastTimestamp: 1251309383
> tickPrice 1<20090826 17:56:14.289000> id=1 symbol= lastSize: 1
> tickPrice 1tickPrice 1tickPrice 1tickPrice 1tickPrice 1<20090826
> 17:56:14.300000> id=1 symbol= bidSize: 134
> <20090826 17:56:14.310000> id=1 symbol= askSize: 202
> <20090826 17:56:14.324000> id=1 symbol= optionCallOpenInterest: 183787
> <20090826 17:56:14.331000> id=1 symbol= optionPutOpenInterest: 150993
> <20090826 17:56:14.345000> id=1 symbol= optionImpliedVol:
> 0.3800353909944552
> <20090826 17:56:14.358000> id=1 symbol= averageVolume: 133558
>
> If you look at the highlighted rows in the output, what strikes me as odd is
> that:
> 1) it looks like the tickPrice function is called again before the message
> could be printed. Is this correct, in other words are these functions
> re-entrant?
> 2) where are the bid and ask prices?
>
> I also tried overriding other functions in myWrapper like tickGeneric() and
> tickString() but they also don't give me the bid/ask price.
> I am probably doing something very wrong here but I can't figure out what it
> is.
>
> I am on windows (R: 2.8.1) and IBrokers version: IBrokers_0.2-2
>
> Any help appreciated,
>
> Regards,
>
> -Mark-
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
--
Jeffrey Ryan
jeffrey.ryan at insightalgo.com
ia: insight algorithmics
www.insightalgo.com
More information about the R-SIG-Finance
mailing list