Sha256: c98682ba4a49f6650d3206fbba0d8151dddd4ff13d00084bd4e72995c51766cb

Contents?: true

Size: 1.6 KB

Versions: 1

Compression:

Stored size: 1.6 KB

Contents

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

require 'rubygems'
require 'pathname'
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 = {123 => IB::Symbols::Forex[:gbpusd],
           456 => IB::Symbols::Forex[:eurusd],
           789 => IB::Symbols::Forex[:usdcad]}

# First, connect to IB TWS.
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 TickerPrice and TickerSize events.  The code passed in the block will
# be executed when a message of that type is received, with the received message as its
# argument. In this case, we just print out the tick. NB: The description field is not
# from IB TWS. It is defined locally in forex.rb, and is just arbitrary text.
ib.subscribe(:TickPrice, :TickSize) 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, _| ib.send_message :CancelMarketData, :id => id }

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ib-ruby-0.7.4 bin/market_data