NAME

AnyEvent::TWS - inofficial InteractiveBrokers Trader Workstation (TWS) API

VERSION

version 0.000_01

SYNOPSIS

my $tws = AnyEvent::TWS->new;
$tws->connect->recv;

my $req = $tws->request(reqContractDetails => {
    id       => $tws->next_valid_id,
    contract => $tws->struct(Contract => {...}),
});
$tws->call($req, sub { my $res = shift; ... });

# simulate event loop
AE::cv->recv;

DESCRIPTION

This is an inofficial Perl port of InteractiveBrokers Trader Workstation API. It is based of the C++ API (http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm#apiguide/c/c.htm).

Because it is based on AnyEvent it can also be used in graphical programs. In the examples directory is a simple Tk program (and others).

In general it works like this: You create a request with a Protocol::TWS::Request subclass and hand it over to call, togehter with a callback. Whenever a response to your request comes in (which can be repeatedly), your callback is called, together with a Protocol::TWS::Response subclass as first parameter. If you want to stop receiving repeated responses, there is usually a API request to do that (starting with "cancel...").

CONSTRUCTOR

new

Accepts the following parameters:

host - default: 127.0.0.1
port - default: 7496
client_id - default: 0

It does not establish a connection, you have to call connect for that.

METHODS

connect

Initiates a connection to InteractiveBrokers API. It returns a condition variable. You can call recv on it to block till the connection is established.

next_valid_id

Returns the next unused request ID.

It is important to always use a new request ID as this module uses the request ID to match incoming messages to outstanding callbacks. So never reuse a request ID.

call

Sends off a request. The first parameter is a Protocol::TWS::Request subclass, the second a callback. The callback is called everytime a response to your original request comes in. This can be repeatedly. But there are also a lot of requests, that just send exactly one response.

The first parameter to your callback is a Protocol::TWS::Response subclass. Use closures (or the request ID) to attach/match objects or other parameters.

struct

Shortcut for instanciating Protocol::TWS::Struct subclasses. First parameter name (equals class name), second parameter (hashref) arguments to the constructor.

request

Shortcut for instanciating Protocol::TWS::Request subclasses. First parameter name (equals class name), second parameter (hashref) arguments to the constructor.

INTERNAL METHODS

process_message

Called internally whenever a new message on the socket arrives.

DEBUGGING

Set the AE_VERBOSE environment variable to 5 (warn) or 8 (debug) to get debugging output.

Be careful to always use a new request ID (see next_valid_id), otherwise the module might get confused.

If you are missing some responses, it could also be a bug. I have not tested every type of request. If you find a bug, please email me a code example together with a description what you expect as result.

If you have any questions or suggestions feel free to email me as well.

Also, if you have any examples that I can include, I would appreciate it.

SEE ALSO

http://www.interactivebrokers.com/en/p.php?f=programInterface, http://www.interactivebrokers.com/php/apiUsersGuide/apiguide.htm#apiguide/c/c.htm, Protocol::TWS, Finance::TWS::Simple

AUTHOR

Uwe Voelker <uwe@uwevoelker.de>

COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Uwe Voelker.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.