[Chicago-talk] Can't make my Java callbacks work

Jay Strauss me at heyjay.com
Tue Oct 21 10:13:35 CDT 2003


I'm playing with this stock trading platform (interactivebrokers.com), which
has a java API, so that one can write programs to do trading stuff (get
quotes, place orders, check positions...).  As a programmer I implement this
set of functions, which are callbacks, then platform calls these functions
when events happen (tickPrice, tickSize, openOrder, connectionClosed...).

I've written 2 pieces, 1 is the public implementation of the API, the other
is a stub to new the API and start a connection.  When I run the stub the
thing stays resident, and keeps spitting out data (I'm just doing
System.out.println) when events happen.

When I try to implement the stub as perl using java.pm, the thing falls
right thru the code and disconnects.  I've tried putting the perl into a
while (1) loop, but then my perl callbacks never get called.  If instead I
sleep() the perl, some of my callbacks occur, but only after it stops
sleeping, and then it ends.

any ideas?

Below is all the code (TWSAPI is the java implementation of the API,
callTWSAPI is the java call of TWSAPI, and x.pl is my perl to call the
TWSAPI):

[o901]:~/InteractiveBrokers> cat TWSAPI.java

import com.ib.client.*;

public class TWSAPI implements EWrapper{

    Callback p;
    int id = 0;

    //public static void TWSAPI(Callback p) {
    public TWSAPI(Callback p) {
        this.p = p;
    }

    public void connectionClosed() {
        System.out.println("connectionClosed");
    }

    public void error(int id, int errorCode, String errorMsg) {
        System.out.println("Error: "+id+","+errorCode+","+errorMsg);
    }

    public void nextValidId(int orderId) {
        id = orderId;
    }

    public void tickPrice(int tickerId, int field, double price) {
        String s = tickerId + "^" + field + "^" + price;
        if (p != null) {
            p.eval("&tickPrice(\"" + s + "\")" );
        }
        System.out.println("tickprice: " + tickerId + "," + field +","+
price);
    }

    public void tickSize(int tickerId, int field, int size) {
        System.out.println("tickSize: "+tickerId+","+field+","+size);
    }
}

[o901]:~/InteractiveBrokers> cat callTWSAPI.java
import com.ib.client.*;

public class callTWSAPI {

    public static void main(String[] args) {

        Contract contract = new Contract();

        Callback c = null;

        TWSAPI ib = new TWSAPI(c);
        EClientSocket m_client = new EClientSocket(ib);

        m_client.eConnect("",7496, 0);

        contract.m_symbol = "YHOO";
        contract.m_secType = "STK";
        contract.m_exchange = "SMART";

        m_client.reqMktData(ib.id,contract);
    }
}

[o901]:~/InteractiveBrokers> cat x.pl
#!/usr/bin/perl
$|++;

use Java;
$java = new Java(event_port=>-1);

my $contract = $java->create_object("com.ib.client.Contract");
my $callback = $java->get_callback_object();

my $ib = $java->create_object("TWSAPI",$callback);
my $m_client = $java->create_object("com.ib.client.EClientSocket",$ib);
$m_client->eConnect("",7496,0);

$contract->set_field("m_symbol","YHOO");
$contract->set_field("m_secType","STK");
$contract->set_field("m_exchange","SMART");

$m_client->reqMktData(1, $contract);

sleep(60);

$m_client->eDisconnect();

sub tickPrice {
    my @arg = split(/\^/,shift);
    print join("\t", at arg),"\n";
}

sub tickSize {
    my @arg = split(/\^/,shift);
    print join("\t", at arg),"\n";
}






More information about the Chicago-talk mailing list