Skip to content

Commit

Permalink
enable real time quote
Browse files Browse the repository at this point in the history
  • Loading branch information
taohu88 committed Jan 16, 2013
1 parent 75b5e1c commit 89d4fda
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
42 changes: 42 additions & 0 deletions example/real_quote
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env ruby
#
# This script downloads historic data for specific symbols from IB

require 'rubygems'
require 'bundler/setup'
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
require 'ib-ruby'

@market = {20000 => IB::Symbols::Stocks[:vxx]}


# 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 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::RealTimeBar) do |msg|
puts @market[msg.request_id].description + ": #{msg.to_human}"
end

@market.each_pair do |id, contract|
ib.send_message IB::Messages::Outgoing::RequestRealTimeBars.new(
:request_id => id,
:contract => contract,
:bar_size => 5, # IB::BAR_SIZES.key(:hour)?
:data_type => :trades,
:use_rth => 1)
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 <Enter> to exit... *********\n\n"
STDIN.gets
4 changes: 2 additions & 2 deletions lib/ib/messages/incoming/real_time_bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Incoming
# :time - The date-time stamp of the start of the bar. The format is offset in
# seconds from the beginning of 1970, same format as the UNIX epoch time
# :bar - received RT Bar
RealTimeBar = def_message 50,
RealTimeBar = def_message [50, 3],
[:request_id, :int],
[:bar, :time, :int],
[:bar, :open, :decimal],
Expand All @@ -23,7 +23,7 @@ def bar
end

def to_human
"<RealTimeBar: #{request_id} #{time}, #{bar}>"
"<RealTimeBar: #{request_id} #{bar}>"
end
end # RealTimeBar

Expand Down
31 changes: 25 additions & 6 deletions lib/ib/messages/outgoing/bar_requests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ def parse data
error ":data_type must be one of #{DATA_TYPES.inspect}", :args
end

size = data[:bar_size] || data[:size]
bar_size = BAR_SIZES.invert[size] || size
unless BAR_SIZES.keys.include?(bar_size)
error ":bar_size must be one of #{BAR_SIZES.inspect}", :args
end
#size = data[:bar_size] || data[:size]
#bar_size = BAR_SIZES.invert[size] || size
# unless BAR_SIZES.keys.include?(bar_size)
# error ":bar_size must be one of #{BAR_SIZES.inspect}", :args
# end

contract = data[:contract].is_a?(IB::Contract) ?
data[:contract] : IB::Contract.from_ib_ruby(data[:contract])

[data_type, bar_size, contract]
[data_type, nil, contract]
end
end

Expand All @@ -44,6 +44,14 @@ def parse data
RequestRealTimeBars = def_message 50, BarRequestMessage

class RequestRealTimeBars
def parse data
data_type, bar_size, contract = super data

size = data[:bar_size] || data[:size]
bar_size = size.to_i
[data_type, bar_size, contract]
end

def encode
data_type, bar_size, contract = parse @data

Expand Down Expand Up @@ -150,6 +158,17 @@ def encode
RequestHistoricalData = def_message [20, 4], BarRequestMessage

class RequestHistoricalData
def parse data
data_type, bar_size, contract = super data

size = data[:bar_size] || data[:size]
bar_size = BAR_SIZES.invert[size] || size
unless BAR_SIZES.keys.include?(bar_size)
error ":bar_size must be one of #{BAR_SIZES.inspect}", :args
end
[data_type, bar_size, contract]
end

def encode
data_type, bar_size, contract = parse @data

Expand Down

0 comments on commit 89d4fda

Please sign in to comment.