Sha256: 43d90444d3cc8fc3dfd4d0016205f6954902708cef1dc3d2094b37c65dcd5724

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

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

require 'pathname'
LIB_DIR = (Pathname.new(__FILE__).dirname + '../lib/').realpath.to_s
$LOAD_PATH.unshift LIB_DIR unless $LOAD_PATH.include?(LIB_DIR)

require 'rubygems'
require 'bundler/setup'
require 'ib-ruby'

# Definition of what we want market 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 = {13 => IB::Symbols::Options[:wfc20],
           15 => IB::Symbols::Options[:z50],
           17 => IB::Symbols::Options[:spy75],
           19 => IB::Symbols::Options[:spy100]}

# First, connect to IB TWS. Arbitrary :client_id is used to identify your script
ib = IB::Connection.new :client_id => 1112 #, :port => 7496 # TWS

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

# Subscribe to Ticker... events.  The code passed in the block will be executed when
# any message of that type is received, with the received message as its argument.
# In this case, we just print out the tick.
#
# (N.B. The description field is not from IB TWS. It is defined
#  locally in forex.rb, and is just arbitrary text.)
ib.subscribe(:TickPrice, :TickSize, :TickOption, :TickString) do |msg|
  puts @market[msg.ticker_id].description + ": " + msg.to_human
end

# Now we actually request market data for the symbols we're interested in.
@market.each_pair do |id, contract|
  ib.send_message :RequestMarketData, :ticker_id => id, :contract => contract
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 :CancelMarketData, :id => id }

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ib-ruby-0.7.3 bin/option_data
ib-ruby-0.7.2 bin/option_data