Sha256: 3e052ce57d76c85242c90e1167d32df9c3281fba250efd86136965cd22727cf4

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

#!/usr/bin/env ruby
#
# This script gets details for specific symbol from IB

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 = {123 => IB::Symbols::Stocks[:wfc],
           125 => IB::Symbols::Options[:wfc20],
           129 => IB::Symbols::Stocks[:wrong]}

# Connect to IB TWS.
ib = IB::Connection.new

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

# Now, subscribe to ContractData incoming 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 data.
ib.subscribe(:ContractData) { |msg| puts msg.contract.inspect }

# Now we actually request historical data for the symbols we're interested in. TWS will
# respond with a HistoricalData message, which will be processed by the code above.
@market.each_pair do |id, contract|
  ib.send_message :RequestContractData, :id => id, :contract => contract
end

sleep 3 # Wait for IB to respond to our request

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ib-ruby-0.5.0 bin/contract_details