Search code examples
ribrokers

R Package IBrokers placeOrder() function fails


I'm using the package: IBrokers. It works well for me when I request historical data. Also the call to reqAccountUpdates() works well.

I am having problems with this script:

# myscript.r

.libPaths("rpackages")
library(IBrokers)
tws2 = twsConnect(2)
print('Attempting BUY')
mytkr     = twsFuture("ES","GLOBEX","201412")
myorderid = sample(1001:3001, 1)
IBrokers:::.placeOrder(tws2, mytkr, twsOrder(myorderid, "BUY", "1", "MKT"))
twsDisconnect(tws2)

Sometimes the above script works okay. Usually though it fails. When it fails, it seems to connect okay.

Then I see this in my TWS console:

03:47:45:581 JTS-EServerSocket-290: [2:47:71:1:0:0:0:ERR] Message type -1.  Socket I/O error - 
03:47:45:581 JTS-EServerSocket-290: Anticipated error
jextend.d: Socket I/O error - 
    at jextend.sc.b(sc.java:364)
    at jextend.ch.sb(ch.java:1534)
    at jextend.ch.run(ch.java:1390)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.net.SocketInputStream.read(SocketInputStream.java:210)
    at jextend.xh.d(xh.java:45)
    at jextend.sc.c(sc.java:579)
    at jextend.sc.r(sc.java:227)
    at jextend.af.a(af.java:232)
    at jextend.sc.f(sc.java:650)
    at jextend.pd.a(pd.java:822)
    at jextend.sc.b(sc.java:358)
    ... 3 more
03:47:45:583 JTS-EServerSocket-290: [2:47:71:1:0:0:0:ERR] Socket connection for client{2} has closed. 
03:47:45:583 JTS-EWriter14-291: [2:47:71:1:0:0:0:ERR] Unable write to socket client{2} - 
03:47:45:584 JTS-EServerSocketNotifier-288: Terminating

Can you offer any ideas on how you would wrestle with this issue?

One other piece of info:

I think the call to reqIds() may be necessary. Sometimes reqIds() would return an id not high enough. Then, I'd use it and placeOrder() would fail. So, I call reqIds() but then use Sys.time() to give me an id which is larger than the last ID I used.

Another problem may have been some code-text I copied out of a PowerPoint. Some of the code-characters may have been corrupt.


Solution

  • The main problem was orderid.

    I need to be careful how I generate orderid.

    Also I may have moused in bad characters from a powerpoint preso which had an example.

    I posted some code which works in a comment in this thread.

    Dan