Sunday, February 8, 2009

Proof Of Concept - 8 of 8 : Going Native

After playing around with JRuby and TWS API trying to resolve the problems described in my prior post, there has been no real progress. I have decided to go native and try out the sample code in Java to ascertain if the problem is with Java or JRuby. In NetBeans, I created a new Java Application called IBJava, created a new class and copied TWS API classes under the IBJava\Src folder. As with everything in this project, nothing is as easy as it seems. The final code to just connect and disconnect from TWS is as follows:


package ibjava;

import com.ib.client.*;


public class StockBot implements EWrapper {

public StockBot() {
EClientSocket m_client = new EClientSocket(this);
m_client.eConnect("127.0.0.1",7496, 0);
System.out.println("Connected.");
while (i<990000) i++;
m_client.eDisconnect();
}

public static void main(String[] args) {

System.out.println("Hello");
StockBot myBot= new StockBot();
}
// ---------------------------------------------------
// -- methods from EWrapper -- must be declared
// ---------------------------------------------------
public void tickPrice( int tickerId, int field, double price, int canAutoExecute){
System.out.println("Price Update: Id: "+tickerId+" Type: "+field+" Price: "+price);
}
public void tickSize( int tickerId, int field, int size){
System.out.println("Volume Update: Id: "+tickerId+" Type: "+field+" Size: "+size);
}
public void tickOptionComputation( int tickerId, int field, double impliedVol,
double delta, double modelPrice, double pvDividend){
}
public void tickGeneric(int tickerId, int tickType, double value){
}
public void tickString(int tickerId, int tickType, String value){
}
public void tickEFP(int tickerId, int tickType, double basisPoints,
String formattedBasisPoints, double impliedFuture, int holdDays,
String futureExpiry, double dividendImpact, double dividendsToExpiry){}
public void orderStatus( int orderId, String status, int filled, int remaining,
double avgFillPrice, int permId, int parentId, double lastFillPrice,
int clientId, String whyHeld){
}
public void openOrder( int orderId, Contract contract, Order order, OrderState orderState){
}

public void updateAccountValue(String key, String value, String currency, String accountName){}
public void updatePortfolio(Contract contract, int position, double marketPrice, double marketValue,
double averageCost, double unrealizedPNL, double realizedPNL, String accountName){}
public void updateAccountTime(String timeStamp){}
public void nextValidId( int orderId){}
public void contractDetails(int reqId, ContractDetails contractDetails){}
public void bondContractDetails(int reqId, ContractDetails contractDetails){}
public void contractDetailsEnd(int reqId){}
public void execDetails( int orderId, Contract contract, Execution execution){}
public void updateMktDepth( int tickerId, int position, int operation, int side, double price, int size){}
public void updateMktDepthL2( int tickerId, int position, String marketMaker, int operation,
int side, double price, int size){}
public void updateNewsBulletin( int msgId, int msgType, String message, String origExchange){}
public void managedAccounts( String accountsList){}
public void receiveFA(int faDataType, String xml){}
public void historicalData(int reqId, String date, double open, double high, double low,
double close, int volume, int count, double WAP, boolean hasGaps){}
public void scannerParameters(String xml){}
public void scannerData(int reqId, int rank, ContractDetails contractDetails, String distance,
String benchmark, String projection, String legsStr){}
public void scannerDataEnd(int reqId){}
public void realtimeBar(int reqId, long time, double open, double high, double low, double close, long volume, double wap, int count){}
public void currentTime(long time){}
public void fundamentalData(int reqId, String data){}

// ---------------------------------------------------
// -- methods from AnyWrapper -- must be declared
// ---------------------------------------------------

public void connectionClosed() {
System.out.println(" [API.connectionClosed] Closed connection with TWS");
}
public void error(String str) {
System.out.println(" [API.msg1] " + str);
}

public void error(int one, int two, String str) {
System.out.println(" [API.msg2] " + str + " {" + one + ", " + two + "}");
}

public void error(Exception e) {
System.out.println(" [API.msg3] " + e.getMessage());
}

}


Some interesting things to point out here:

1) If lines 76-90 are omitted, the following error is generated:


java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Uncompilable source code - ibjava.StockBot is not abstract and does not override abstract method connectionClosed() in com.ib.client.AnyWrapper
at ibjava.StockBot.(StockBot.java:11)
Exception in thread "main"
Exception in thread "main" Java Result: 1


This is due to the fact that EWrapper interface is a subclass of AnyWrapper interface, which means that I have to create AnyWrapper interface methods in addition to EWrapper interface methods. I did not do this in my JRuby code due to my limited Java knowledge. This error needs to be fixed in the JRuby version.


2) Running the code above with 'Incoming Connection' dialog ON seems to work. The output is:


run:
Hello
Server Version:43
TWS Time at connection:20090208 21:45:55 EST
Connected.
[API.connectionClosed] Closed connection with TWS
BUILD SUCCESSFUL (total time: 2 seconds)



3) Running the code above with 'Incoming Connection' dialog OFF generates the error:

run:
Hello
Server Version:43
TWS Time at connection:20090208 21:49:48 EST
Connected.
[API.msg2] Market data farm connection is OK:usopt {-1, 2104}
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
[API.msg3] socket closed
[API.connectionClosed] Closed connection with TWS
BUILD SUCCESSFUL (total time: 0 seconds)


It's clear to me now that there are issues in Java that I need to understand and resolve before even attempting to get JRuby to work. By working at Java level, I at least get some useful error messages instead of JRuby's cryptic ones.

C'est la vie! My initial approach to this project was to be free from working with Java, but that turns out not to be the case. I obviously need to get my hands 'dirty' and this process will take a little more time. The bright side to this is that Java resources are everywhere and I'm sure I'll be able to get this fixed. Stay tuned...




02/11/09 Update
It occurred to me that the 'errors' I experienced may not actually be errors at all. After some research, it appears that just because the error() methods were triggered, doesn't actually mean errors occurred. They may just be messages that I can ignore. This is good news. With this in mind, I will proceed with Java coding.


No comments: