Saturday, May 5, 2012

Perl Finance - announcing AnyEvent::TWS and Protocol::TWS

I know there are already two CPAN modules to access InteractiveBrokers Traders Workstation (IB TWS) API:
And both of them work, I used them successfully.

But the API is just \0 terminated lines over a socket, so I always wanted to write a pure Perl API for it.

The data structures, request and response messages are in Protocol::TWS (which later also could generate suitable objects for the two native APIs). And AnyEvent::TWS is an asynchronous pure Perl implemenation of the TWS API.

It is still in the early stages, but you can already play around with it. For example with contract.pl (in AnyEvent::TWS). Just correct the include path and the IP (localhost is default).

$ perl contract.pl -s IBM -t OPT -st 150 -r PUT
conId:  78782644
symbol: IBM
expiry: 20130118
strike: 150
right:  P

conId:  94203928
symbol: IBM
expiry: 20140117
strike: 150
right:  P

conId:  97819043
symbol: IBM
expiry: 20120720
strike: 150
right:  P

conId:  102789332
symbol: IBM
expiry: 20121019
strike: 150
right:  P

conId:  104498834
symbol: IBM
expiry: 20120518
strike: 150
right:  P

conId:  106288439
symbol: IBM
expiry: 20120615
strike: 150
right:  P

The parameters are -s (symbol), -t (security type), -st (strike), -r (right PUT/CALL) and some more (just look in the source code).

You can also request historical or current market data and place trades. But do not use it with your real money account, for sure there are lots of bugs in it.

9 comments:

  1. awesome ! I'll try some stuff with it... Then use it in real life !

    ReplyDelete
  2. Thanks for making this available. Question: I'm on Win7 64, Perl 5.10, TWS 926. When I run connect.pl I get some response from TWS: SEND: ... RECV: ... etc, but then this:
    Can't locate object method "lines" via package "Protocol::TWS::Response::managedAccounts"
    I don't see "lines" method in "managedAccounts.pm".

    ReplyDelete
  3. Oh, that is strange. I will have a look at it. Can you please send me the stacktrace (e. g. with Carp::Always "perl -MCarp::Always connect.pl") to uwe@uwevoelker.de?
    Thanks a lot.

    ReplyDelete
  4. Regarding connect.pl - please do not use that script. I will delete it from the repository. It was a first test and is now broken.

    Have a look at the examples in the AnyEvent-TWS repository.

    I also started a simplified blocking API Finance::TWS::Simple. You will find it on Github too.

    Sorry for this confusion.

    ReplyDelete
  5. Hi Uwe, I've developed a small utility using your "realtimebars-multi.pl" example from AnyEvent-TWS, The goal is to retrieve single, realtime quotes for a number of symbols. It works for the first symbol, but I'm having trouble managing the request ID's. I'd like to send you the relevant piece of the code for review, but I don't see your contact info on this page. Can you send your email to "for-uwe@mailinator.com" (a temporary mailbox), I'll delete it from there as soon as I get it. Thanks.

    ReplyDelete
  6. My email is no secret: uwe@uwevoelker.de - but I will send you an email as well.

    ReplyDelete
  7. When I try to run the example realtimebars.pl against IB's demo account, its prints out:
    $VAR1 = bless( {
    'errorCode' => '2107',
    'id' => '-1',
    'errorString' => 'HMDS data farm connection is inactive but should be available upon demand.demohmds'
    }, 'Protocol::TWS::Response::error' );
    The scripts seems hung after that. Any idea?

    ReplyDelete
  8. I tried it today (weekend) and get the following error:
    'Invalid Real-time Query:No historical market data for EUR/CASH@IDEALPRO Last 0'

    I will check again during trading hours and report back.

    ReplyDelete
  9. Today I tried it during trading hours - it worked as expected. So this seems to be an issue with the demo account. It worked on my real money account.

    ReplyDelete