Skip to content

Commit 96b53fe

Browse files
author
Hartmut Bischoff
committed
Support for HistoricalDataUpdates (IB-Message 90)
1 parent fb0a938 commit 96b53fe

File tree

3 files changed

+56
-21
lines changed

3 files changed

+56
-21
lines changed

lib/ib/messages/incoming/historical_data.rb

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def load
4141
IB::Bar.new :time => buffer.read_int_date, # conversion of epoche-time-integer to Dateime
4242
# requires format_date in request to be "2"
4343
# (outgoing/bar_requests # RequestHistoricalData#Encoding)
44-
:open => buffer.read_decimal,
45-
:high => buffer.read_decimal,
46-
:low => buffer.read_decimal,
47-
:close => buffer.read_decimal,
44+
:open => buffer.read_float,
45+
:high => buffer.read_float,
46+
:low => buffer.read_float,
47+
:close => buffer.read_float,
4848
:volume => buffer.read_int,
49-
:wap => buffer.read_decimal,
49+
:wap => buffer.read_float,
5050
# :has_gaps => buffer.read_string, # only in ServerVersion < 124
5151
:trades => buffer.read_int
5252
end
@@ -79,6 +79,26 @@ def load
7979
end
8080
end
8181

82+
HistoricalDataUpdate = def_message [90, 0] ,
83+
[:request_id, :int] ,
84+
[:count, :int],
85+
[:bar, :bar] # defined in support.rb
86+
87+
class HistoricalDataUpdate
88+
attr_accessor :results
89+
using IBSupport # extended Array-Class from abstract_message
90+
91+
def bar
92+
@bar = IB::Bar.new @data[:bar]
93+
end
94+
95+
def to_human
96+
"<HistDataUpdate #{request_id} #{bar}>"
97+
end
98+
end
99+
100+
101+
82102
end # module Incoming
83103
end # module Messages
84104
end # module IB

lib/ib/support.rb

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def read_xml
6262

6363
def read_int_date
6464
t= read_int
65-
s= Time.at(t)
65+
s= Time.at(t.to_i)
6666
# s.year == 1970 --> data is most likely a date-string
6767
s.year == 1970 ? Date.parse(t.to_s) : s
6868
end
@@ -139,20 +139,35 @@ def read_hash
139139
end
140140
#
141141

142-
def read_contract # read a standard contract and return als hash
143-
{ con_id: read_int,
144-
symbol: read_string,
145-
sec_type: read_string,
146-
expiry: read_string,
147-
strike: read_decimal,
148-
right: read_string,
149-
multiplier: read_int,
150-
exchange: read_string,
151-
currency: read_string,
152-
local_symbol: read_string,
153-
trading_class: read_string } # new Version 8
154-
155-
end
142+
def read_contract # read a standard contract and return als hash
143+
{ con_id: read_int,
144+
symbol: read_string,
145+
sec_type: read_string,
146+
expiry: read_string,
147+
strike: read_decimal,
148+
right: read_string,
149+
multiplier: read_int,
150+
exchange: read_string,
151+
currency: read_string,
152+
local_symbol: read_string,
153+
trading_class: read_string } # new Version 8
154+
end
155+
156+
157+
def read_bar # read a standard bar (Historical data bars)
158+
{ :time => read_int_date, # conversion of epoche-time-integer to Dateime
159+
# requires format_date in request to be "2"
160+
# (outgoing/bar_requests # RequestHistoricalData#Encoding)
161+
:open => read_float,
162+
:high => read_float,
163+
:low => read_float,
164+
:close => read_float,
165+
:wap => read_float,
166+
:volume => read_int,
167+
# :has_gaps => read_string, # only in ServerVersion < 124
168+
:trades => read_int }
169+
170+
end
156171

157172

158173
alias read_bool read_boolean

lib/models/ib/bar.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Bar < IB::Model
2222
validates_numericality_of :open, :high, :low, :close, :volume
2323

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

0 commit comments

Comments
 (0)