#!/usr/bin/env ruby # This script reproduces https://github.com/ib-ruby/ib-ruby/issues/12 require 'bundler/setup' require 'ib-api' contract = IB::Stock.new :symbol=> 'AAPL' # First, connect to IB Gateway. ib = IB::Connection.new :client_id => 1112 # id to identify your script # :port => 7497 # TWS connection (instead of default Gateway) # :received => false # Do not keep all received messages in memory ib.subscribe(:Alert) { |msg| puts msg.to_human } ib.subscribe(:MarketDataType) { |msg| puts msg.to_human } ib.subscribe(:TickGeneric, :TickString, :TickPrice, :TickSize) { |msg| puts msg.inspect } ib.send_message :RequestMarketDataType, :market_data_type => :delayed ib.send_message :RequestMarketData, id: 123, contract: contract puts "\nSubscribed to market data" puts "\n******** Press to cancel... *********\n\n" gets puts "Cancelling market data subscription.." ib.send_message :CancelMarketData, id: 123 sleep 1 puts "Done."