#!/usr/bin/env ruby # # The Head Time Stamp -Request returns the first available date for historical data requests require 'bundler/setup' require 'ib/symbols' # First, connect to IB TWS and subscribe for events. ib = IB::Connection.new :client_id => 1112 do | gw | #, :port => 7497 # TWS # Subscribe to TWS alerts/errors gw.subscribe(:Alert, :HeadTimeStamp) { |msg| puts msg.to_human } # Set log level gw.logger.level = Logger::FATAL # DEBUG -- INFO -- WARN -- ERROR -- FATAL end # # We demonstrate it with Apple # ib.send_message :RequestHeadTimeStamp, contract: IB::Symbols::Stocks.aapl ib.wait_for :HeadTimeStamp puts " First datatset for #{IB::Symbols::Stocks.aapl.description} : #{ib.received[:HeadTimeStamp].date.join( " " )} " Thread.new do sleep 1 puts "\n******** Press to quit *********\n\n" end STDIN.gets puts "\n *** canceling Reqest ..." ib.send_message :CancelHeadTimeStamp # .. puts "Printing all gathered data \n\n" pp ib.received[ :HeadTimeStamp ]