[R-SIG-Finance] IBrokers
Stephen Choularton
stephen at organicfoodmarkets.com.au
Tue Dec 7 20:13:45 CET 2010
So back again!
I have two problem in converting my callback to two handle two symbols
so I can set up spreads. (Or at least that's what I can see it at the
moment):
When I run this using myCALLBACK function below:
con = twsConnect()
reqMktData(con, list(twsSTK("MSFT"),twsSTK("AAPL")),
event=eWrapper.data(1), CALLBACK=myCALLBACK)
I get this
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " NA "LastSize
: " NA
[13] "Volume: " NA
2 -1 2104 Market data farm connection is OK:aufarm
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " NA "LastSize
: " NA
[13] "Volume: " NA
2 -1 2104 Market data farm connection is OK:usfarm
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " NA "LastSize
: " NA
[13] "Volume: " NA
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " NA "LastSize
: " NA
[13] "Volume: " NA
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " NA "LastSize
: " NA
[13] "Volume: " NA
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " NA
[13] "Volume: " NA
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " NA
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " NA "BidPrice: " NA "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" NA
[7] "AskSize: " NA "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "4"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343529"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "1435" "BidPrice: " "26.96" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.97"
[7] "AskSize: " "828" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
[1] "BidSize: " "970" "BidPrice: " "26.97" "AskPrice:
" "26.98"
[7] "AskSize: " "847" "Last: " "26.97" "LastSize
: " "2"
[13] "Volume: " "343550"
Error in `*tmp*`[[id]] : subscript out of bounds
I think what is happening is that the function fails on its first
attempt to get data on AAPT and the above data is on MSFT but I am quite
unsure as to what direction to take to amend the function. Any pointers
most welcome.
Secondly I need to be able to know which symbol I am dealing with. The
output for:
reqMktData(con, list(twsEquity("MSFT"),twsEquity("AAPL")))
is
TWS Message: 2 -1 2104 Market data farm connection is OK:aufarm
TWS Message: 2 -1 2104 Market data farm connection is OK:usfarm
<20101208 05:55:28.974000> id=1 symbol=MSFT Volume: 343560
<20101208 05:55:28.978000> id=1 symbol=MSFT highPrice: 27.09
<20101208 05:55:28.983000> id=1 symbol=MSFT lowPrice: 26.88
<20101208 05:55:28.988000> id=2 symbol=AAPL Volume: 79079
<20101208 05:55:28.993000> id=2 symbol=AAPL highPrice: 323.99
and shows the symbol I need but when I try to find something that refers
to it in myCALLBACK I seem to fail. Is it somewhere in the data
object? Is it somewhere esle someone can point me to?
Stephen Choularton Ph.D., FIoD
On 07/12/2010 5:24 PM, Stephen Choularton wrote:
> Just for the record I though I should post this as it did actually
> place the order I was after:
>
> library(IBrokers)
>
> myCALLBACK <-
> function(twsCon, eWrapper, timestamp, file, playback = 1, ...) {
>
> con <- twsCon[[1]]
> traded <- FALSE
> while (TRUE) {
> socketSelect(list(con), FALSE, NULL)
> #data
> curMsg <- readBin(con, character(), 1)
> if (!is.null(timestamp)) {
> processMsg(curMsg, con, eWrapper, format(Sys.time(),
> timestamp), file, ...)
> } else {
> processMsg(curMsg, con, eWrapper, timestamp, file, ...)
> }
> # EXTRACT DATA HERE
> BidSize <- as.numeric(eWrapper$.Data$data[[1]][,1])
> BidPrice <- as.numeric(eWrapper$.Data$data[[1]][,2])
> AskPrice <- as.numeric(eWrapper$.Data$data[[1]][,3])
> AskSize <- as.numeric(eWrapper$.Data$data[[1]][,4])
> Last <- as.numeric(eWrapper$.Data$data[[1]][,5])
> LastSize <- as.numeric(eWrapper$.Data$data[[1]][,6])
> Volume <- as.numeric(eWrapper$.Data$data[[1]][,7])
> print(c("BidSize: ", BidSize, "BidPrice: ", BidPrice, "AskPrice:
> ", AskPrice, "AskSize: ", AskSize, "Last: ", Last, "LastSize : ",
> LastSize , "Volume: ", Volume ))
> # TRADE LOGIC HERE
> if(!traded && !is.na(Last) && Last >= 49) {
>
> print(c("inside the trading logic "))
> id <- reqIds(twsCon)
> #placeOrder(tws, twsEquity("CBA","ASX","AUD"), twsOrder(id,
> "BUY", "1", "MKT"))
> traded <- TRUE
> }
> }
> }
>
>
>
> con = twsConnect()
>
>
>
> #reqMktData(con, twsEquity("GOOG"), event=eWrapper.data(1),
> CALLBACK=myCALLBACK)
>
>
> reqMktData(con, twsEquity("CBA","ASX","AUD"), event=eWrapper.data(1),
> CALLBACK=myCALLBACK)
>
>
>
> closeAllConnections()
>
>
> I imagine I will be back as I am actually trying to use this for
> spreads - so now I have to move on and try two symbols, etc but for
> what it is worth the code is there for anyone who wants to use it.
>
> Stephen Choularton Ph.D., FIoD
>
>
> On 06/12/2010 10:17 AM, Jeff Ryan wrote:
>> Stephen,
>>
>> Comments inline:
>>
>> On Sun, Dec 5, 2010 at 1:23 PM, Stephen Choularton
>> <stephen at organicfoodmarkets.com.au> wrote:
>>> Hi
>>>
>>> If you read this list you will have noticed I have been working on
>>> callbacks
>>> and I finally got to some code that appears to work so I though I would
>>> share it:
>>>
>>> library(IBrokers)
>>>
>>> myCALLBACK<-
>>> function(twsCon, eWrapper, timestamp, file, playback = 1, ...) {
>>> con<- twsCon[[1]]
>>> twsOC<- twsConnect(2) # our order connection
>>> con2<- twsOC[[1]]
>>> ocWrapper<- eWrapper(TRUE)
>>> traded<- FALSE
>>> while (TRUE) {
>>> cons<- socketSelect(list(con, con2), FALSE, 0.01)
>>> if(cons[1]) { #data
>>> curMsg<- readBin(con, character(), 1)
>>> if (!is.null(timestamp)) {
>>> processMsg(curMsg, con, eWrapper, format(Sys.time(),
>>> timestamp), file, ...)
>>> } else {
>>> processMsg(curMsg, con, eWrapper, timestamp, file, ...)
>>> }
>>> } else if(cons[2]) {
>>> curMsg<- readBin(con2, character(), 1)
>>> if (!is.null(timestamp)) {
>>> processMsg(curMsg, con2, ocWrapper, format(Sys.time(),
>>> timestamp), file, ...)
>>> } else {
>>> processMsg(curMsg, con2, ocWrapper, timestamp, file,
>>> ...)
>>> }
>>> } # TRADE LOGIC HERE
>>> curBID<- as.numeric(eWrapper$.Data$data[[1]][3])
>>> if(!traded ) { # add back on open:&& !is.na(curBID)&&
>>> curBID>
>>> 141.00
>>>
>>> print(c("the cur bid is ", curBID))
>>> # IBrokers:::.placeOrder(twsOC,
>>> twsEquity("CBA","ASX","AUD"),
>>> twsOrder(1053, "BUY", "0", "MKT"))
>>> traded<- TRUE
>>> }
>>> }
>>> twsDisconnect(twsOC)
>>> }
>>>
>>> con = twsConnect(1)
>>>
>>> # contracts<- list(twsSTK("MSFT"),twsSTK("AAPL")))
>>>
>>>
>>> reqMktData(con, twsEquity("GOOG"), CALLBACK=myCALLBACK)
>>>
>>> twsDisconnect(con)
>>>
>>> However, even though I have tried to close down my twsCnnections, if
>>> I run
>>> the code twice I get a warning along the lines:
>>>
>>> unable to connect. client id already in use. retry with unique
>>> client id
>>>
>>> and R freezes. Does anyone know how to solve the problem?
>> R is likely "freezing" due to buffering on Windows (which you can turn
>> off in the GUI), but that isn't really the core issue. You don't need
>> to have a second connection opened in the CALLBACK. It is in the
>> examples of one of my talks to illustrate having an external mkt data
>> feed and IB execution. If you are only looking at IB (for data and
>> execution), DON'T use a second connection. Seriously it is not needed
>> at all, and it is just making things more confusing I suspect.
>>
>> > From there (on connection) close(con) or twsDisconnect(con) should
>> indeed free up the client id. You can always go the route of
>> closeAllConnections() route in R to 'start over'.
>>
>>> Incidentally I notice that the code gets curBID<-
>>> as.numeric(eWrapper$.Data$data[[1]][3]). Does anyone know what the
>>> appropriate indices are for the other data in eWrapper$.Data and
>>> what that
>>> data is?
>>>
>> As Mark points out in his reply, look at the source for the
>> answers/hints:
>>
>> In order: BidSize, BidPrice, AskPrice, AskSize, Last, LastSize and
>> Volume. The first four ordering mimic what it looks like when you
>> stand in the pit on the floor and look at the CRTs (or your own custom
>> screen...) ;-)
>>
>>
>> # from the source
>>> eWrapper.data
>> function (n)
>> {
>> eW<- eWrapper(NULL)
>> eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,
>> 7), nc = 7), 0), .Dimnames = list(NULL, c("BidSize",
>> "BidPrice", "AskPrice", "AskSize", "Last", "LastSize",
>> "Volume")))), n))
>> eW$tickPrice<- function(curMsg, msg, timestamp, file, ...) {
>> tickType = msg[3]
>> msg<- as.numeric(msg)
>> id<- msg[2]
>> data<- eW$get.Data("data")
>> attr(data[[id]], "index")<- as.numeric(Sys.time())
>> nr.data<- NROW(data[[id]])
>> if (tickType == .twsTickType$BID) {
>> data[[id]][nr.data, 1:2]<- msg[5:4]
>> }
>> else if (tickType == .twsTickType$ASK) {
>> data[[id]][nr.data, 3:4]<- msg[4:5]
>> }
>> else if (tickType == .twsTickType$LAST) {
>> data[[id]][nr.data, 5]<- msg[4]
>> }
>> eW$assign.Data("data", data)
>> c(curMsg, msg)
>> }
>> eW$tickSize<- function(curMsg, msg, timestamp, file, ...) {
>> data<- eW$get.Data("data")
>> tickType = msg[3]
>> msg<- as.numeric(msg)
>> id<- as.numeric(msg[2])
>> attr(data[[id]], "index")<- as.numeric(Sys.time())
>> nr.data<- NROW(data[[id]])
>> if (tickType == .twsTickType$BID_SIZE) {
>> data[[id]][nr.data, 1]<- msg[4]
>> }
>> else if (tickType == .twsTickType$ASK_SIZE) {
>> data[[id]][nr.data, 4]<- msg[4]
>> }
>> else if (tickType == .twsTickType$LAST_SIZE) {
>> data[[id]][nr.data, 6]<- msg[4]
>> }
>> else if (tickType == .twsTickType$VOLUME) {
>> data[[id]][nr.data, 7]<- msg[4]
>> }
>> eW$assign.Data("data", data)
>> c(curMsg, msg)
>> }
>> return(eW)
>> }
>> <environment: namespace:IBrokers>
>>
>> You can of course do whatever you like to make it more logical to you.
>> Reading IBrokers source and the official Java source (yikes) is the
>> way to get to a different end if you so require.
>>
>> Best,
>> Jeff
>>
>>>
>>>
>>> --
>>> Stephen Choularton Ph.D., FIoD
>>>
>>> _______________________________________________
>>> 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.
>>>
>>
>>
>
> _______________________________________________
> 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.
>
>
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 10.0.1170 / Virus Database: 426/3301 - Release Date: 12/06/10
>
>
More information about the R-SIG-Finance
mailing list