[R-SIG-Finance] Linux and IBrokers

Mark Harrison harrisonmark1 at gmail.com
Mon May 14 06:14:45 CEST 2012


I am having some trouble getting the IBrokers, TWS, and TWS API
running in linux.

I have a dual boot system running 64Bit Windows 7 and xubuntu 12.04.
I installed Oracle Java v7, TWS, and the TWS API.  I have an IBJts
directory off my home dir and I unpacked both the TWS and TWS API into
this directory.

I put the TWS start command into a small script and can run it, log
into TWS, and see everything working as it should - i.e. quotes show,
positions show, etc.

For the R I am running RStudio 95.265 and R 2.14.1.

I can connect to TWS from R & RStudio and but some of the commands
work some of the time while listening for messages tends to produce
errors every time.  I am running the same script in Windows as in
Linux and everything works in Windows.

This is running against the paper account.

# Session Info

R version 2.14.1 (2011-12-22)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8
 [7] LC_PAPER=C                 LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base

other attached packages:
 [1] blotter_0.8.8              twsInstrument_1.4-3
 [3] IBrokers_0.9-3             qmao_1.2.4
 [5] FinancialInstrument_0.14.4 quantmod_0.3-18
 [7] TTR_0.21-1                 Defaults_1.1-1
 [9] xts_0.8-7                  zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.14.1    lattice_0.20-0 tools_2.14.1

# connect to IB TWS
tws <- twsConnect()

# Check Connection
isConnected(tws)

# Response from IB
[1] TRUE

# set symbol and build contract
symbol <- c("GC")
contract <- buildIBcontract(twsFUT(symbol, "NYMEX", '201206'))

# Response from IB
Connected with clientId 100.
Request complete: GC FUT USD.
Disconnected.

# Request ID, build Order
id <- reqIds(tws)
ord <- twsOrder(id, action = 'BUY', totalQuantity = '1', orderType = 'MKT')

# Place Order  - Sometimes the order goes through but most times nothing happens
placeOrder(tws, contract, ord)


# Listen for messages - this fails all the time
myWrapper <- eWrapper()
waiting <- TRUE
while (waiting) {

con <- tws[[1]]
socketSelect(list(con), FALSE, NULL)
curMsg <- readBin(con, character(), 1)

if (curMsg == .twsIncomingMSG$EXECUTION_DATA) {
  header <- readBin(con, character(), 25)
  waiting <- FALSE
} else
  processMsg(curMsg, con, myWrapper, format(Sys.time(),timestamp), file)
}



More information about the R-SIG-Finance mailing list