2010/12/5 Stephen Choularton <stephen@organicfoodmarkets.com.au>

> 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:
>
> Nice to hear that you got this working


> 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?
>
> If you end the endless look in myCALLBACK with Ctrl+C don't count on it
that the twsDisconnect() will execute.
You could try to disconnect both connections before they are created in the
script. This way you will be sure that the clientIds you are using are
"free". Another tip: After you end the endless loop with Ctrl+C you could
call the twsDisconnect() from the R-Prompt manually...
Does this solve the freezes?


> 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?
>
> I don't have a list of all data fields but I think you could find out. Have
a look at the code in eWrapper.data(). Just type "eWrapper.data" at the R
prompt. This will give you a clue. You could also just print the entire
vector in myCALLBACK runtime (i.e. print(c(eWrapper$.Data$data[[1]] ))) and
deduct what the data fields are (compare with what's in TWS). Another way
would be to download the TWS API documentation on the IB website and look at
the msg structure and hold that against the eWrapper.data() code...

>
>
> --
> Stephen Choularton Ph.D., FIoD
>
> _______________________________________________
> R-SIG-Finance@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.
>

	[[alternative HTML version deleted]]

