#!/usr/bin/env ruby # # This script downloads historic data for specific symbols 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' ### Configurable Options Quiet = false # if Quiet == false, status data will be printed to STDERR Timeout = 10 # How long to wait for messages from TWS before exiting, sec # Definition of what we want 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[:ym], 789 => IB::Symbols::Forex[:gbpusd] # No historical market data for GBP/CASH@IDEALPRO Last 60 } # Connect to IB TWS. ib = IB::Connection.new # Subscribe to TWS alerts/errors ib.subscribe(:Alert) { |msg| puts msg.to_human } # Now, subscribe to HistoricalData 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. # # Note that we have to look the ticker id of each incoming message # up in local memory to figure out what it's for. ib.subscribe(IB::Messages::Incoming::HistoricalData) do |msg| STDERR.puts @market[msg.data[:id]].description + ": " + msg.data[:count].to_s + " items:" unless Quiet msg.data[:results].each do |datum| @last_msg_time = Time.now.to_i STDERR.puts " " + datum.to_s unless Quiet end end # 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 IB::Messages::Outgoing::RequestHistoricalData.new( :id => id, :contract => contract, :end_date_time => Time.now.to_ib, :duration => '2 D', # ? :bar_size => '1 min', #IB::OutgoingMessages::BAR_SIZES.key(:hour), :what_to_show => :trades, :use_rth => 1, :format_date => 1) end sleep 5 # Wait for IB to respond to our request