diff --git a/lib/ib/connection.rb b/lib/ib/connection.rb index 5b22428..b09918a 100644 --- a/lib/ib/connection.rb +++ b/lib/ib/connection.rb @@ -4,6 +4,11 @@ require 'ib/logger' require 'ib/messages' +module TechnicalAnalysis + module Signals + end +end + module IB # Encapsulates API connection to TWS or Gateway class Connection @@ -100,7 +105,7 @@ def initialize host: '127.0.0.1', def update_next_order_id i,finish = 0, false sub = self.subscribe(:NextValidID) { finish = true } - connected? ? self.send_message( :RequestIds ) : open() + connected? ? self.send_message( :RequestIds ) : open() Timeout::timeout(1, IB::TransmissionError,"Could not get NextValidId" ) do loop { sleep 0.1; break if finish } end @@ -186,7 +191,13 @@ def subscribe *args, &block when what.is_a?(Class) && what < Messages::Incoming::AbstractMessage [what] when what.is_a?(Symbol) - [Messages::Incoming.const_get(what)] + if Messages::Incoming.const_defined?(what) + [Messages::Incoming.const_get(what)] + elsif TechnicalAnalysis::Signals.const_defined?(what) + [TechnicalAnalysis::Signals.const_get?(what)] + else + error "#{what} is no IB::Messages or TechnicalAnalyis::Signals class" + end when what.is_a?(Regexp) Messages::Incoming::Classes.values.find_all { |klass| klass.to_s =~ what } else diff --git a/lib/ib/messages/incoming/historical_data.rb b/lib/ib/messages/incoming/historical_data.rb index 2235d77..7d924a0 100644 --- a/lib/ib/messages/incoming/historical_data.rb +++ b/lib/ib/messages/incoming/historical_data.rb @@ -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 @@ -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 + "" + end + end + + + end # module Incoming end # module Messages end # module IB diff --git a/lib/ib/support.rb b/lib/ib/support.rb index 767614d..85471f9 100644 --- a/lib/ib/support.rb +++ b/lib/ib/support.rb @@ -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 @@ -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 diff --git a/lib/models/ib/bar.rb b/lib/models/ib/bar.rb index 734b4c4..8d02e51 100644 --- a/lib/models/ib/bar.rb +++ b/lib/models/ib/bar.rb @@ -22,7 +22,7 @@ class Bar < IB::Model validates_numericality_of :open, :high, :low, :close, :volume def to_human - "" end