Skip to content

Commit

Permalink
Support for HistoricalDataUpdates (IB-Message 90)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hartmut Bischoff committed May 4, 2021
1 parent fb0a938 commit 96b53fe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
30 changes: 25 additions & 5 deletions lib/ib/messages/incoming/historical_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ def load
IB::Bar.new :time => buffer.read_int_date, # conversion of epoche-time-integer to Dateime
# requires format_date in request to be "2"
# (outgoing/bar_requests # RequestHistoricalData#Encoding)
:open => buffer.read_decimal,
:high => buffer.read_decimal,
:low => buffer.read_decimal,
:close => buffer.read_decimal,
:open => buffer.read_float,
:high => buffer.read_float,
:low => buffer.read_float,
:close => buffer.read_float,
:volume => buffer.read_int,
:wap => buffer.read_decimal,
:wap => buffer.read_float,
# :has_gaps => buffer.read_string, # only in ServerVersion < 124
:trades => buffer.read_int
end
Expand Down Expand Up @@ -79,6 +79,26 @@ def load
end
end

HistoricalDataUpdate = def_message [90, 0] ,
[:request_id, :int] ,
[:count, :int],
[:bar, :bar] # defined in support.rb

class HistoricalDataUpdate
attr_accessor :results
using IBSupport # extended Array-Class from abstract_message

def bar
@bar = IB::Bar.new @data[:bar]
end

def to_human
"<HistDataUpdate #{request_id} #{bar}>"
end
end



end # module Incoming
end # module Messages
end # module IB
45 changes: 30 additions & 15 deletions lib/ib/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def read_xml

def read_int_date
t= read_int
s= Time.at(t)
s= Time.at(t.to_i)
# s.year == 1970 --> data is most likely a date-string
s.year == 1970 ? Date.parse(t.to_s) : s
end
Expand Down Expand Up @@ -139,20 +139,35 @@ def read_hash
end
#

def read_contract # read a standard contract and return als hash
{ con_id: read_int,
symbol: read_string,
sec_type: read_string,
expiry: read_string,
strike: read_decimal,
right: read_string,
multiplier: read_int,
exchange: read_string,
currency: read_string,
local_symbol: read_string,
trading_class: read_string } # new Version 8

end
def read_contract # read a standard contract and return als hash
{ con_id: read_int,
symbol: read_string,
sec_type: read_string,
expiry: read_string,
strike: read_decimal,
right: read_string,
multiplier: read_int,
exchange: read_string,
currency: read_string,
local_symbol: read_string,
trading_class: read_string } # new Version 8
end


def read_bar # read a standard bar (Historical data bars)
{ :time => read_int_date, # conversion of epoche-time-integer to Dateime
# requires format_date in request to be "2"
# (outgoing/bar_requests # RequestHistoricalData#Encoding)
:open => read_float,
:high => read_float,
:low => read_float,
:close => read_float,
:wap => read_float,
:volume => read_int,
# :has_gaps => read_string, # only in ServerVersion < 124
:trades => read_int }

end


alias read_bool read_boolean
Expand Down
2 changes: 1 addition & 1 deletion lib/models/ib/bar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Bar < IB::Model
validates_numericality_of :open, :high, :low, :close, :volume

def to_human
"<Bar: #{time} wap #{wap} OHLC #{open} #{high} #{low} #{close} " +
"<Bar: #{time.strftime("(%d.%m.%y)%X")} wap #{wap.round(3)} OHLC #{open} #{high} #{low} #{close} " +
(trades ? "trades #{trades}" : "") + " vol #{volume}>"
end

Expand Down

0 comments on commit 96b53fe

Please sign in to comment.