[R-SIG-Finance] IBrokers
Stephen Choularton
stephen at organicfoodmarkets.com.au
Sun Dec 5 20:23:17 CET 2010
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?
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?
--
Stephen Choularton Ph.D., FIoD
More information about the R-SIG-Finance
mailing list