Friday, January 30, 2009

Proof Of Concept - 8 of 8 : Stumbling Block

Here's where things get interesting. Now that I'm able to successfully connect to TWS, the next step is to do something useful such as getting current market quote. I found a nice site called StockBotProgramming.com which has sample code to do this in Java. I find that this code is more direct and easier to follow than the one in JavaAPIGettingStarted.pdf.

Some notes about this Java code:
  • Main class implements EWrapper interface
  • A class must implement all of the methods described in the interface
  • The methods in the example is specific to that TWS API, and will not be the same as my version of API (9.51)
  • Methods TickPrice() and TickSize() are callback methods that automatically get called when the quote become available

My task would be to create a class in Ruby which implements this EWrapper interface. More specifically, my version of the EWrapper. So the code will be a little different. Per Java interface definition, all the functions of EWrapper class needs to be declared. The easiest way to do this is to go directly the the EWrapper Java class file, copy, paste into Ruby class and convert to Ruby syntax.

This isn't quite as easy as it looks and I'm getting some strange behavior in the coding. Before handling this issue, I noticed there's a problem with prior post concerning Skipping Incoming connection. So I'm not defining a trusted IP anymore.

In order for readers to follow along, I have to post my code here. This is going to be hard to see. But I've found Google's syntax highlighter tool, so I'll be incorporating that into this blog from now on. The instructions are not too clear. I found this blog article to be much better.

A) This is my test code to request market data:


require 'java'
require 'TwsApi.jar'

include_class 'ib.client.EClientSocket'
include_class 'ib.client.Contract'
include_class 'ib.client.AnyWrapper'
include_class 'ib.client.EWrapper'

# classes for interface method parameters. Not sure if necessary. Can't hurt.
include_class 'ib.client.ContractDetails'
include_class 'ib.client.Execution'
include_class 'ib.client.OrderState'

class TestMktData include EWrapper #note class TestMktData < EWrapper syntax is no longer supported in JRuby 1.0

@mySocket

def initialize
puts 'init'
@mySocket = EClientSocket.new(self)
end

def eConnect
@mySocket.eConnect("127.0.0.1", 7496, 0)
end

def requestQuote
puts 'running requestQuote'
# --------------------------------
myContract = Contract.new
myContract.m_symbol = 'SPY'
myContract.m_secType = 'STK'
myContract.m_exchange = 'SMART'
myContract.m_currency = 'USD'
# options specific below ---
myContract.m_expiry = ''
myContract.m_right = ''
request_id = 1
ticklist = ''
puts 'created new contract, calling reqMktData'
@mySocket.reqMktData(request_id, myContract, ticklist, true)
puts 'sleeping for 10 sec...'
sleep(10)
end

def eDisconnect
@mySocket.eDisconnect
end

#///////////////////////////////////////////////////////////////////////
#// Interface methods
#///////////////////////////////////////////////////////////////////////

def tickPrice( ticker_id, field, price, can_auto_execute)
puts 'in tickPrice'
#puts 'tickPrice() id=' + ticker_id.to_s + ', price = ' + price.to_s
end

def tickSize( tickerId, field, size)
puts 'in tickSize'
#puts 'tickSize() Id: ' + tickerId.to_s + ' Type: ' + field.to_s + ' Size: ' + size.to_s
end

def tickOptionComputation( ticker_id, field, implied_vol, delta, model_price, pv_dividend)
end

def tickGeneric( ticker_id, tick_type, value)
end

def tickString( ticker_id, tick_type, value)
end

def tickEFP( ticker_id, tick_type, basis_points, formatted_basis_points, implied_future, hold_days, future_expiry, dividend_impact, dividends_to_expiry)
end

def orderStatus( order_id, status, filled, remaining, avg_fill_price, perm_id, parent_id, last_fill_price, client_id, why_held)
end

def openOrder( order_id, contract, order, order_state)
end

def updateAccountValue( key, value, currency, account_name)
end

def updatePortfolio( contract, position, market_price, market_value, average_cost, unrealized_pnl, realized_pnl, account_name)
end

def updateAccountTime( time_stamp)
end

def nextValidId( order_id)
end

def contractDetails( req_id, contract_details)
end

def bondContractDetails( req_id, contract_details)
end

def contractDetailsEnd( req_id)
end

def execDetails( order_id, contract, execution)
end

def updateMktDepth( ticker_id, position, operation, side, price, size)
end

def updateMktDepthL2( ticker_id, position, market_maker, operation, side, price, size)
end

def updateNewsBulletin( msg_id, msgType, message, orig_exchange)
end

def managedAccounts( accounts_list)
end

def receiveFA( fa_data_type, xml)
end

def historicalData( req_id, date, open, high, low, close, volume, count, eWAP, has_gaps)
end

def scannerParameters( xml)
end

def scannerData( req_id, rank, contract_details, distance, benchmark, projection, legs_str)
end

def scannerDataEnd( req_id)
end

def realtimeBar( req_id, time, open, high, low, close, volume, wap, count)
end

def currentTime( time)
end

def fundamentalData( req_id, data)
end

end # class TestMktData

# *********************************************************************************************
# Running the code
# *********************************************************************************************

x = TestMktData.new()
begin
x.eConnect
puts 'connected ...'
x.requestQuote
puts 'wait 10 sec'
sleep(10)
x.eDisconnect
puts 'disconnected'
rescue Exception => e
puts 'can not connect'
puts 'Exception' + e.message
x.eDisconnect
end


Line 14: specifies that I'm using a Java interface from TWS API called EWrapper
Line 41: submits market quote for ticker SPY
Lines 54-62: methods that automatically gets called when market data is received
Lines 64-140: the rest of method specifications as required by EWrapper interface
Lines 148-161: runs the code by connecting, requesting market quote, and disconnecting

Running the code produces error:

init
Server Version:43

TWS Time at connection:20090201 22:48:24 EST

connected ...
running requestQuote
created new contract, calling reqMktData
sleeping for 10 sec...
Exception in thread "EReader" :1: #< nameerror::message:0x1de0c09 > (NoMethodError)
...internal jruby stack elided...
from (unknown).(unknown)(:1)

disconnected


What causes this 'stack elided' error? It seems like it has something to do with requesting market data, waiting (sleep), and receiving market data. So I went about commenting out lines of code to see what the behavior is and here's what I've found:

B) Commenting out sleep():

puts 'created new contract, calling reqMktData'
@mySocket.reqMktData(request_id, myContract, ticklist, true)
puts 'sleeping for 10 sec...'
# sleep(10)



produces no errors, but then again, the program didn't wait around to receive quote either:


init
Server Version:43

TWS Time at connection:20090201 23:09:01 EST

connected ...
running requestQuote
created new contract, calling reqMktData
sleeping for 10 sec...
disconnected


C) Can it be that sleep() is the actual problem and has nothing to do with reqMktData()? I tried just connecting, sleeping, and disconnecting:


x = TestMktData.new()
begin
x.eConnect
puts 'connected ...'
# x.requestQuote
puts 'wait 10 sec'
sleep(10)
x.eDisconnect
puts 'disconnected'
rescue Exception => e
puts 'can not connect'
puts 'Exception' + e.message
x.eDisconnect
end


Produces the same 'stack elided' error:


init
Server Version:43

TWS Time at connection:20090201 23:24:53 EST

connected ...
wait 10 sec
Exception in thread "EReader" :1: #< nameerror::message:0xd510e8> (NoMethodError) ...internal jruby stack elided... from (unknown).(unknown)(:1)

disconnected


D) So going back to my previous post on TWS Note - Skipping 'Incoming Connection'. I modify the connect and disconnect code to add the sleep():


require 'java'
require 'TwsApi.jar'
include_class 'ib.client.EClientSocket'

mySocket = EClientSocket.new(self)
begin
mySocket.eConnect("127.0.0.1", 7496, 0) # api
puts 'connected ...'
sleep(10) # this produces error
mySocket.eDisconnect
puts 'disconnected'

rescue Exception => e
puts 'can not connect'
puts e.message
end


Produces 'EReader' error, which is different from 'stack elided' error from before (although consistent with error in post on TWS Note - Skipping 'Incoming Connection'):

Server Version:43

TWS Time at connection:20090201 23:31:00 EST

connected ...
wait 10 sec
Exception in thread "EReader" java.lang.ClassCastException: InterfaceImpl326315435 at ib.client.EReader.eWrapper(EReader.java:47) at ib.client.EReader.run(EReader.java:66)

was connected, now disconnected


So what gives? Why do A and C give 'stack elided' errors and D give an 'EReader' error? Both have sleep() but the difference is that A and C define a new class which implements EWrapper. D doesn't do that. It could be that there are two different error conditions going on. I'm not sure what the answer is yet, but I will look into it some more.

Stay tuned ..

No comments: