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.