#!/usr/bin/env ruby # # This script downloads historic data for specific symbols from IB require 'bundler/setup' require 'ib/symbols' # 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. # Predefined Symbol-definitions --> lib/ib/symbols contracts = {123 => IB::Symbols::Futures.mini_dax, 456 => IB::Stock.new( symbol: 'BAS', currency: 'EUR') , # BASF 789 => IB::Symbols::Forex.eurusd } # Connect to IB TWS. #ib = IB::Connection.new( :client_id => 1102, port: 7496, host: '10.222.148.177' ) do | gw | ib = IB::Connection.new :client_id => 1112, :port => 7496 do | gw | #, :port => 7497 # TWS # Subscribe to TWS alerts/errors gw.subscribe(:Alert, :ManagedAccounts) { |msg| puts msg.to_human } # 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. gw.subscribe(IB::Messages::Incoming::HistoricalData) do |msg| puts contracts[msg.request_id].description + ": #{msg.count} items:" msg.results.each { |entry| puts " #{entry}" } 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. contracts.each_pair do |request_id, contract| ib.send_message IB::Messages::Outgoing::RequestHistoricalData.new( :request_id => request_id, :contract => contract, :end_date_time => Time.now.to_ib, :duration => '1 D', # ? :bar_size => :hour1, # IB::BAR_SIZES.key(:hour)? :what_to_show => :trades, :use_rth => 0, :keep_up_todate => 0) end # IB does not send any indication when all historic data is done being delivered. # So we need to interrupt manually when our request is answered. puts "\n******** Press to exit... *********\n\n" STDIN.gets __END__ ## EXTRACTED FROM THE PYTHON IMPLEMENTATION (Jan 2018) contract:Contract - This object contains a description of the contract for which market data is being requested. endDateTime:str - Defines a query end date and time at any point during the past 6 mos. Valid values include any date/time within the past six months in the format: yyyymmdd HH:mm:ss ttt where "ttt" is the optional time zone. durationStr:str - Set the query duration up to one week, using a time unit of seconds, days or weeks. Valid values include any integer followed by a space and then S (seconds), D (days) or W (week). If no unit is specified, seconds is used. barSizeSetting:str - Specifies the size of the bars that will be returned (within IB/TWS listimits). Valid values include: 1 sec 5 secs 15 secs 30 secs 1 min 2 mins 3 mins 5 mins 15 mins 30 mins 1 hour 1 day whatToShow:str - Determines the nature of data beinging extracted. Valid values include: TRADES MIDPOINT BID ASK BID_ASK HISTORICAL_VOLATILITY OPTION_IMPLIED_VOLATILITY useRTH:int - Determines whether to return all data available during the requested time span, or only data that falls within regular trading hours. Valid values include: 0 - all data is returned even where the market in question was outside of its regular trading hours. 1 - only data within the regular trading hours is returned, even if the requested time span falls partially or completely outside of the RTH. formatDate: int - Determines the date format applied to returned bars. validd values include: 1 - dates applying to bars returned in the format: yyyymmdd{space}{space}hh:mm:dd 2 - dates are returned as a long integer specifying the number of seconds since 1/1/1970 GMT. chartOptions:TagValueList - For internal use only. Use default value XYZ. """