Obtaining market quote is performed with reqMktData() method. I was able to successfully get quotes for ETF, stock, index, and options. The code to request SPY ETF is as follows:
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'
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 requestStockQuote(id, ticker)
# --------------------------------
myContract = Contract.new
myContract.m_symbol = ticker
myContract.m_secType = 'STK'
myContract.m_exchange = 'SMART'
myContract.m_currency = 'USD'
request_id = id
ticklist = ''
puts '>requestStockQuote - calling reqMktData id='+id.to_s+', ticker='+ticker
@mySocket.reqMktData(request_id, myContract, ticklist, true)
end
def requestIdxQuote(id, ticker)
# --------------------------------
myContract = Contract.new
myContract.m_symbol = ticker
myContract.m_secType = 'IND'
myContract.m_exchange = 'CBOE'
myContract.m_currency = 'USD'
request_id = id
ticklist = ''
puts '>requestIdxQuote - calling reqMktData id='+id.to_s+', ticker='+ticker
@mySocket.reqMktData(request_id, myContract, ticklist, true)
end
# expiry - string format YYYYMM
# strike - number
# call_put - CALL or PUT
def requestOptionQuote(id, ticker, expiry, strike, call_put)
# --------------------------------
myContract = Contract.new
myContract.m_symbol = ticker
myContract.m_secType = 'OPT'
myContract.m_exchange = 'SMART'
myContract.m_currency = 'USD'
# options specific below ---
myContract.m_expiry = expiry # string format YYYYMM
myContract.m_right = call_put # CALL, PUT
myContract.m_strike = strike # number
request_id = id
ticklist = ''
puts '>requestOptionQuote - calling reqMktData id='+id.to_s+', ticker='+ticker+', '+expiry+', '+strike.to_s+', '+call_put
@mySocket.reqMktData(request_id, myContract, ticklist, true)
end
def eDisconnect
@mySocket.eDisconnect
end
#///////////////////////////////////////////////////////////////////////
#// Interface methods
#///////////////////////////////////////////////////////////////////////
def tickPrice( ticker_id, field, price, can_auto_execute)
puts 'tickPrice() Price Update: Id: ' + ticker_id.to_s + ' Type: '+field.to_s+' Price: ' + price.to_s
end
def tickSize( ticker_id, field, size)
puts 'tickSize() Volume Update: Id: '+ticker_id.to_s+' Type: '+field.to_s+' Size: '+size.to_s
end
def tickOptionComputation( ticker_id, field, implied_vol,
delta, model_price, pv_dividend)
puts 'tickOptionComputation() Update: Id:'+ticker_id.to_s+' Type: '+field.to_s+' IV: '+implied_vol.to_s+' Delta: '+delta.to_s
end
def tickGeneric( ticker_id, tick_type, value)
puts 'tickGeneric() Update: Id: '+ticker_id.to_s+' Type: '+tick_type.to_s+' Value: '+value.to_s
end
def tickString( ticker_id, tick_type, value)
puts 'tickString() Update: Id: '+ticker_id.to_s+' Type: '+tick_type.to_s+' Value: '+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
# ---------------------------------------------------
# -- methods from AnyWrapper -- must be declared
# ---------------------------------------------------
def connectionClosed()
puts ' [API.connectionClosed] Closed connection with TWS'
end
def error(err_obj_str)
puts ' [API.msg1] ' + err_obj_str if err_obj_str.is_a?(String)
puts ' [API.msg3] ' + err_obj_str.getMessage() if err_obj_str.is_a?(Exception)
end
def error(one, two, str)
puts ' [API.msg2] ' + str + ' {' + one.to_s + ', ' + two.to_s + '}'
end
end # class TestMktData
# *********************************************************************************************
# Running the code
# *********************************************************************************************
x = TestMktData.new()
begin
x.eConnect
puts 'connected ...'
req_id = 0
x.requestStockQuote(req_id += 1, 'SPY') # ETF
puts 'wait 5 sec'
sleep(5)
x.eDisconnect
puts 'disconnected'
rescue Exception => e
puts 'can not connect'
puts 'Exception' + e.message
x.eDisconnect
end
Lines 184-195: contain methods that were missing from my initial code. Note that in the Java version, there were actually three error() methods, with two of them having just one parameter (of different types). Since Ruby is a loosely typed language, creating two error() methods with one parameter will generate an error. To get around this, Ruby's version of error() actually handles the job of Java's two methods. Look at lines 189-190 to see how the parameter's types are determined.
Line 209: illustrates the calling method to request quote for SPY exchange traded fund
Running this produces the following output:
Server Version:43
TWS Time at connection:20090213 14:13:43 EST
connected ...
>requestStockQuote - calling reqMktData id=1, ticker=SPY [API.msg2] Market data farm connection is OK:usopt {-1, 2104}
wait 5 sec
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
tickPrice() Price Update: Id: 1 Type: 1 Price: 83.68
tickSize() Volume Update: Id: 1 Type: 0 Size: 258
tickPrice() Price Update: Id: 1 Type: 2 Price: 83.69
tickSize() Volume Update: Id: 1 Type: 3 Size: 68
tickPrice() Price Update: Id: 1 Type: 4 Price: 83.69
tickSize() Volume Update: Id: 1 Type: 5 Size: 1
tickSize() Volume Update: Id: 1 Type: 8 Size: 1881804
tickPrice() Price Update: Id: 1 Type: 6 Price: 84.24
tickPrice() Price Update: Id: 1 Type: 7 Price: 82.75
tickPrice() Price Update: Id: 1 Type: 9 Price: 83.66
tickPrice() Price Update: Id: 1 Type: 14 Price: 83.55
tickString() Update: Id: 1 Type: 45 Value: 1234552423
disconnected
Line 209 can also be modified to request quote for American Express
x.requestStockQuote(req_id += 1, 'AXP') # stock
produces the following output:
Server Version:43TWS Time at connection:20090213 22:13:09 ESTconnected ...
>requestStockQuote - calling reqMktData id=1, ticker=AXP
wait 5 sec
[API.msg2] Market data farm connection is OK:usopt {-1, 2104}
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
tickSize() Volume Update: Id: 1 Type: 8 Size: 0
tickString() Update: Id: 1 Type: 45 Value: 1234562108
tickPrice() Price Update: Id: 1 Type: 4 Price: 15.69
tickSize() Volume Update: Id: 1 Type: 5 Size: 1
tickPrice() Price Update: Id: 1 Type: 6 Price: 16.33
tickPrice() Price Update: Id: 1 Type: 7 Price: 15.58
tickPrice() Price Update: Id: 1 Type: 9 Price: 16.19
tickPrice() Price Update: Id: 1 Type: 14 Price: 16.05
tickPrice() Price Update: Id: 1 Type: 1 Price: 15.56
tickSize() Volume Update: Id: 1 Type: 0 Size: 2
tickPrice() Price Update: Id: 1 Type: 2 Price: 15.95
tickSize() Volume Update: Id: 1 Type: 3 Size: 1
disconnected
To request Russell 2000 small cap index from Chicago Board Options Exchange
x.requestIdxQuote(req_id += 1, 'RUT') # index
produces the following output:
Server Version:43
TWS Time at connection:20090213 14:14:19 EST
connected ...
>requestIdxQuote - calling reqMktData id=1, ticker=RUT
[API.msg2] Market data farm connection is OK:usopt {-1, 2104}
wait 5 sec
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
tickPrice() Price Update: Id: 1 Type: 4 Price: 451.93
tickSize() Volume Update: Id: 1 Type: 5 Size: 0
tickSize() Volume Update: Id: 1 Type: 8 Size: 0
tickPrice() Price Update: Id: 1 Type: 6 Price: 0.0
tickPrice() Price Update: Id: 1 Type: 7 Price: 0.0
tickPrice() Price Update: Id: 1 Type: 9 Price: 450.42
tickString() Update: Id: 1 Type: 45 Value: 1234552454
disconnected
To request options quote for GE September, 2009 15 Call
x.requestOptionQuote(req_id += 1, 'GE', '200909', 15, 'CALL' )
produces the following output:
Server Version:43
TWS Time at connection:20090213 14:14:44 EST
connected ...
[API.msg2] Market data farm connection is OK:usopt {-1, 2104}
>requestOptionQuote - calling reqMktData id=1, ticker=GE, 200909, 15, CALL
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
wait 5 sec
tickPrice() Price Update: Id: 1 Type: 1 Price: 0.9
tickSize() Volume Update: Id: 1 Type: 0 Size: 1200
tickPrice() Price Update: Id: 1 Type: 2 Price: 0.94
tickSize() Volume Update: Id: 1 Type: 3 Size: 648
tickPrice() Price Update: Id: 1 Type: 4 Price: 0.94
tickSize() Volume Update: Id: 1 Type: 5 Size: 9
tickSize() Volume Update: Id: 1 Type: 8 Size: 116
tickPrice() Price Update: Id: 1 Type: 6 Price: 1.0
tickPrice() Price Update: Id: 1 Type: 7 Price: 0.88
tickPrice() Price Update: Id: 1 Type: 9 Price: 0.97
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.6091767213831093 Delta: 3.3175946061912392e-87
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.6211516922231228 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.6211516922231228 Delta: -0.0
tickString() Update: Id: 1 Type: 32 Value: CIXPB
tickString() Update: Id: 1 Type: 33 Value: CIXPB
tickString() Update: Id: 1 Type: 45 Value: 1234551366
tickString() Update: Id: 1 Type: 33 Value: CIPB
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.6091767213831093 Delta: 3.3175946061941284e-87
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.6211516922231228 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.6211516922231228 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 13 IV: 0.615161765120708 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.606880041775532 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.6188416326918191 Delta: -5.379410170061903e+268
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.6188416326918191 Delta: -5.379410170061903e+268
tickOptionComputation() Update: Id:1 Type: 13 IV: 0.6128581636676679 Delta: -0.0
disconnected
To request options quote for Russell 2000 index December, 2009 450 Put
x.requestOptionQuote(req_id += 1, 'RUT', '200912', 450, 'PUT' )
produces the following output:
Server Version:43
TWS Time at connection:20090213 14:15:15 EST
connected ...
[API.msg2] Market data farm connection is OK:usopt {-1, 2104}
>requestOptionQuote - calling reqMktData id=1, ticker=RUT, 200912, 450, PUT
wait 5 sec
[API.msg2] Market data farm connection is OK:usfarm {-1, 2104}
tickPrice() Price Update: Id: 1 Type: 1 Price: 71.2
tickSize() Volume Update: Id: 1 Type: 0 Size: 311
tickPrice() Price Update: Id: 1 Type: 2 Price: 73.2
tickSize() Volume Update: Id: 1 Type: 3 Size: 201
tickSize() Volume Update: Id: 1 Type: 8 Size: 0
tickPrice() Price Update: Id: 1 Type: 9 Price: 71.85
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.4371941774336616 Delta: 1.5035109594887676e+46
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.44973728365842186 Delta: -1.7139093657526427e+170
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.44126908872009857 Delta: -0.0
tickString() Update: Id: 1 Type: 32 Value: CIX
tickString() Update: Id: 1 Type: 33 Value: CIB
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.4371941774336616 Delta: 1.503510959488021e+46
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.44973728365842186 Delta: -1.713909365751876e+170
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.44126908872009857 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 13 IV: 0.4434638974508833 Delta: 8.778371095955947e+183
tickOptionComputation() Update: Id:1 Type: 10 IV: 0.438042894164516 Delta: 8.899823344244101e-231
tickOptionComputation() Update: Id:1 Type: 11 IV: 0.45058242706938006 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 12 IV: 0.4421166435405054 Delta: -0.0
tickOptionComputation() Update: Id:1 Type: 13 IV: 0.44431082241353415 Delta: -0.0
tickString() Update: Id: 1 Type: 32 Value: CIXB
tickString() Update: Id: 1 Type: 32 Value: CIX
disconnected
03/09/09 Update
Upgrading to API v. 9.60 requires addition of these new callback methods.
# ---------------------------------------------------
# new callback methods added in API 9.60
# ---------------------------------------------------
def openOrderEnd() # invoked when all orders are sent to a client as a response to reqOpenOrders()
end
def accountDownloadEnd(account_name) #invoked when a complete snapshot of an account state is sent to a client as a response to reqAccountUpdates()
end
def execDetailsEnd( req_id) # invoked when all executions are sent to a client as a response to reqExecutions()
end
def deltaNeutralValidation(req_id, under_comp) # for Delta-Neutral DN RFQ
end
No comments:
Post a Comment