[R-SIG-Finance] ibrokers - new callback

Stephen Choularton stephen at organicfoodmarkets.com.au
Thu Dec 2 20:29:07 CET 2010


I'm trying to work through Jeff's example for a new callback from his 
presentation Real Time Trading in R at R/Rmetrics 2009:

twsOC <- twsConnect(2) # our order connection
ocWrapper <- eWrapper(TRUE)
traded <- FALSE
while (TRUE) {
     cons <- socketSelect(list(con, twsOC[[1]]), 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(twsOC[[1]], character(), 1)
         if (!is.null(timestamp)) {
             processMsg(curMsg, twsOC[[1]], ocWrapper, 
format(Sys.time(), timestamp), file, ...)
         } else {
             processMsg(curMsg, twsOC[[1]], ocWrapper, timestamp, file, ...)
         }
     } #TRADE LOGIC HERE
     curBID <- as.numeric(eWrapper$.Data$data[[1]][3])
     if(!traded && !is.na(curBID) && curBID > 141.00) {
         c("this is the order line")
         # IBrokers:::.placeOrder(twsOC, twsSTK("AAPL"), twsOrder(1053, 
"BUY", "10", "MKT"))
         traded <- TRUE
     }
}

First, I think that there was a missing } at the end of the loop on the 
slide, so I added one.  However, when I run it I get:

Error in socketSelect(list(con, twsOC[[1]]), FALSE, 0.01) :
   object 'con' not found


I know I have a connection:

twsOC
<twsConnection,2 @ 20101203 06:16:05 Eastern Standard Time (New South 
Wales), nextId=-1>
 >


  Can anyone advise me what's wrong?

  Incidentally, I don't see how this code gets info on AAPL.  The first 
time its mentioned is down in the TRADE LOGIC HERE section.  Can anyone 
clarify?



-- 
Stephen Choularton Ph.D., FIoD



More information about the R-SIG-Finance mailing list