The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Finance::InteractiveBrokers::TWS - A wrapper around InteractiveBroker's Trader's Workstation (TWS) Java interface, that lets one interact with the TWS using Perl, via the vendor supplied API.

VERSION

This document describes Finance::InteractiveBrokers::TWS version 0.0.1

SYNOPSIS

package Callback;
use strict;

sub new {
    bless {}, shift;
}

sub tickPrice {
    my $self = shift;
    print "tickPrice called with: ", join(" ", @_), "\n";
}

sub tickSize {
    my $self = shift;
    print "tickSize called with: ", join(" ", @_), "\n";
}

sub nextValidId {
    my $self = shift;
    print "nextValidId called with: ", join(" ", @_), "\n";
}

sub error {
    my $self = shift;
    print "error: ", join(" ", @_), "\n";
}

package main;

use strict;
use Finance::InteractiveBrokers::TWS;

my $callback = Callback->new();
my $tws      = Finance::InteractiveBrokers::TWS->new(callback=>$callback);
my $ib       = $tws->get_EClientSocket();
my $api      = $tws->get_api();

####                        Host         Port    Client_ID
####                        ----         ----    ---------
my @tws_GUI_location = qw/  127.0.0.1    7496       15     /;

$ib->eConnect(@tws_GUI_location);
$api->OpenCallbackStream() ;

my $flag = 1;

my $contract = $tws->new_Contract();
$contract->{m_symbol} = 'YHOO';
$contract->{m_secType} = 'STK';
$contract->{m_exchange} = 'SMART';

while ((my $got_data = $api->WaitForCallback(.01)) > -1) {

    if ($got_data) {
        $api->ProcessNextCallback() ;
    }
    else {
        if ($flag) {
            $flag=0;
            my $contract_id = 50;
            $ib->reqMktData($contract_id, $contract);
        }
    }
}

DESCRIPTION

Finance::InteractiveBrokers::TWS - Is a wrapper around InteractiveBrokers Traders Workstation (TWS) Java interface, that lets one interact with the TWS using Perl, via the vendor supplied API.

It uses Inline::Java to wrap InteractiveBrokers' Java API that IB supplies to communicate with the TWS. After numerous attempts at writing a pure perl module I opted for this solution because:

  • Using Inline::Java resulted in a much simpler and smaller module

  • The interaction and call syntax is identical to the Java API (because it is the Java API) and as such, you can ask questions on the IB bulletin board and yahoo, and be using the same method names and call syntax as they are. In other words, people will know what you're talking about.

  • IB changes their interface with some frequency, which required re-writing my interface every time

  • IB changes there message stream, which required me to modify my parser

  • Whenever IB changes something I'd have to diff the old API versus the new API and try to figure out what changed and how that affected my code

INTERFACE

The interface is identical to the Java API supplied by InteractiveBrokers. There are 2 interfaces to be exact. The calling interface, i.e. the methods you call to send data to the TWS, and the callback interface, that is the data that is sent back to your custom event handler (callback).

I'm not going to document the interface (unless you make me) since InteractiveBrokers already did.

Calling Interface

IB's documentation is pretty weak. But here is a list of methods you can call once you have created a Finance::InteractiveBrokers::TWS object

http://www.interactivebrokers.com/php/webhelp/Interoperability/Socket_Client_Java/java_eclientsocket.htm

Callback Interface

http://www.interactivebrokers.com/php/webhelp/Interoperability/Socket_Client_Java/java_ewrapper.htm

DIAGNOSTICS

Finance::InteractiveBrokers::TWS::java::lang::NullPointerException=HASH(0x8b671cc) - This means you did not supply a callback object when you instantiated a Finance::IB:TWS object.

CONFIGURATION AND ENVIRONMENT

You need to compile the *.java API source files into java class files prior to using this module. Do it like:

$ cd ~/IBJts/java/com/ib/client/
$ javac *.java

Furthermore Finance::InteractiveBrokers::TWS does require that you set your CLASSPATH environmental variable to the location of the IBJts/java directory where you installed the IB API. Such as:

$ export CLASSPATH=~/IBJts/java

DEPENDENCIES

  • Java SDK

  • Inline::Java

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to bug-finance-ib-tws@rt.cpan.org, or through the web interface at http://rt.cpan.org.

AUTHOR

Jay Strauss <me@heyjay.com>

LICENCE AND COPYRIGHT

Copyright (c) 2006, Jay Strauss <me@heyjay.com>. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.

IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.