Sha256: 2f3476d80130af19ef896c8dfc70c8d212b981307c707bda08c195bddd8d5a88

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

#!/usr/bin/env ruby
#
# This script connects to IB API and subscribes to L2 (depth of market) data for
# specific symbols

require 'bundler/setup'
require 'ib/symbols'

# Definition of what we want L2 data for.  We have to keep track of what ticker id
# corresponds to what symbol ourselves, because the ticks don't include any other
# identifying information. The choice of ticker ids is, as far as I can tell, arbitrary.
market = {123 => IB::Symbols::Stocks[:wfc],
           456 => IB::Symbols::Futures[:es],
           789 => IB::Symbols::Forex[:gbpusd]
}

# First, connect to IB TWS.
ib = IB::Connection.new client_id: 1112, port: 4002 do | gw | #, :port => 7496 # TWS

	# Subscribe to TWS alerts/errors
	gw.subscribe(:Alert) { |msg| puts msg.to_human }

	# Subscribe to MarketDepth events.
	gw.subscribe(:MarketDepth) do |msg|
		puts market[msg.request_id].description + ": " + msg.to_human
	end

	# Set log level
	gw.logger.level = Logger::FATAL # DEBUG #FATAL

end
# Now we actually request L2 market data for the symbols we're interested in.
market.each_pair do |id, contract|
  ib.send_message :RequestMarketDepth,
                  :id => id,
                  :contract => contract,
                  :num_rows => 5
end

puts "\nSubscribed to market data"
puts "\n******** Press <Enter> to cancel... *********\n\n"
STDIN.gets
puts "Cancelling market data subscription.."

market.each_pair { |id, contract| ib.send_message :CancelMarketDepth, :id => id }

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ib-symbols-1.0 examples/depth_of_market