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.
A more condensed approach using the gateway:
require 'bundler/setup'
require 'ib-gateway'
IB::Gateway.new client_id:1134
if ( ib = IB::Gateway.tws ).present?
contracts = [ IB::Symbols::Futures.es, IB::Symbols::Forex.gbpusd ]
ib.subscribe(:TickPrice, :TickSize, :TickString) do |msg|
puts contracts.find{|x| x.con_id == msg.ticker_id}.symbol + ": " + msg.to_human + "\t (press enter to cancel)"
end
contracts.each do | contract|
c= contract.verify.first # ensure presence of con_id
ib.send_message :RequestMarketData, :id => c.con_id, :contract => c
end
STDIN.gets
else
puts 'No TWS'
end
This small script follows exact the paradigm of IB-Ruby. It sends messages and allocates the received information accordingly. Its not very rubish, though.
The data gathered through send_message :RequestMarketData
are related to IB::Contract
. It seems natural to assign them directly.
IB::Contract
offers two link-possibilities: :bars
and misc
, which offers a more sophisticated, object orientated solution.
More Details in Contracts/Market-Data