The Basics

The API offers the :RequestMarketData message to address the gathering of market-related information

> ib =  IB::Connection.current
> ib.send_message :RequestMarketData, contract: IB::Symbols::Stocks.wfc, snapshot: true
# or
> ib.send_message :RequestMarketData, contract: IB::Symbols::Stocks.wfc, ticklist: [106,107]

where items in :ticklist further specify the request (details in messages/outgoing/request_marketdata). Most significant are:

 100 - Option Volume (stocks) -> returned Tick-IDs: 29, 30
 101 - Option Open Interest (stocks)             -> 27, 28
 104 - Historical Volatility                     -> 23
 105 - Average Opt Volume
 106 - Option Implied Volatility (impvolat)      -> 24
 107                             (climpvlt)        
 411 - Realtime Historical Volatility            -> 58

The example dir contains a simple market-data script.

Verbose solution using :send_message and :subscribe

ib = IB::Connection.new
ib.activate_plugin :symbols, :verify
ib.try_connection!
     
contracts = [ IB::Symbols::Futures.mini_dax, 
              Stock.new( symbol: :sie, currency: :eur ) ].map{|y| y.verify.first}

subscription = ib.subscribe(:TickPrice, :TickSize, :TickString) do |msg|
   puts contracts.find{ |x| x.con_id == msg.ticker_id }.symbol + ": " + msg.to_human 
end

contracts.each do | c |
   ib.send_message :RequestMarketData, :id => c.con_id, :contract => c
end
													 
STDIN.gets  # press a key to finish the script
contracts.each { |c| ib.send_message :CancelMarketData, :id => c.con_id  }
ib.unsubscribe subscription

This small script follows the paradigm of IB-Ruby. It sends messages and responses accordingly to the received information.

The market-price plugin

Fetching the actual market-price of an asset is a common task. IB-Ruby provides plugin which adds current data to :bars and misc attributes of the used IB::Contract.

ib = IB::Connection.new
ib.activate_plugin :market_price
ib.try_connection!

s = Stock.new symbol: 'FMG', currency: :aud
s.market_price => 0.2197e2
s.bars   => [{:bid=>0.2197e2, :ask=>0.2198e2, :close=>0.2159e2, :last=>0.2197e2}]
s.misc   => {:delayed=>0.2197e2}

More Details in Contracts/Market-Data

Tags: