lib/ib-ruby/messages/incoming.rb in ib-ruby-0.5.16 vs lib/ib-ruby/messages/incoming.rb in ib-ruby-0.5.17

- old
+ new

@@ -53,12 +53,18 @@ # Object#id is always defined, we cannot rely on method_missing def id @data.has_key?(:id) ? @data[:id] : super end + def respond_to? method + getter = method.to_s.sub(/=$/, '').to_sym + @data.has_key?(method) || @data.has_key?(getter) || super + end + protected + # TODO: method compilation instead of method_missing def method_missing method, *args getter = method.to_s.sub(/=$/, '').to_sym if @data.has_key? method @data[method] elsif @data.has_key? getter @@ -66,15 +72,10 @@ else super method, *args end end - def respond_to? method - getter = method.to_s.sub(/=$/, '').to_sym - @data.has_key?(method) || @data.has_key?(getter) || super - end - # Every message loads received message version first def load @data[:version] = @socket.read_int end @@ -154,33 +155,33 @@ [:perm_id, :int], [:parent_id, :int], [:last_fill_price, :decimal], [:client_id, :int], [:why_held, :string] do - "<OrderStatus: #{@data[:status]} filled: #{@data[:filled]}/#{@data[:remaining]+@data[:filled]}" + - " @ last/avg: #{@data[:last_fill_price]}/#{@data[:average_fill_price]}" + - (@data[:parent_id] > 0 ? "parent_id: #{@data[:parent_id]}" : "") + - (@data[:why_held] != "" ? "why_held: #{@data[:why_held]}" : "") + - " id/perm: #{@data[:id]}/#{@data[:perm_id]}>" + "<OrderStatus: #{status} filled: #{filled}/#{remaining + filled}" + + " @ last/avg: #{last_fill_price}/#{average_fill_price}" + + (parent_id > 0 ? "parent_id: #{parent_id}" : "") + + (why_held != "" ? "why_held: #{why_held}" : "") + + " id/perm: #{id}/#{perm_id}>" end AccountValue = def_message(6, [:key, :string], [:value, :string], [:currency, :string], [:account_name, :string]) do - "<AccountValue: #{@data[:account_name]}, #{@data[:key]}=#{@data[:value]} #{@data[:currency]}>" + "<AccountValue: #{account_name}, #{key}=#{value} #{currency}>" end AccountUpdateTime = def_message(8, [:time_stamp, :string]) do - "<AccountUpdateTime: #{@data[:time_stamp]}>" + "<AccountUpdateTime: #{time_stamp}>" end # This message is always sent by TWS automatically at connect. # The IB::Connection class subscribes to it automatically and stores # the order id in its @next_order_id attribute. - NextValidID = def_message(9, [:id, :int]) { "<NextValidID: #{@data[:id]}>" } + NextValidID = def_message(9, [:id, :int]) { "<NextValidID: #{id}>" } NewsBulletins = def_message 14, [:id, :int], # unique incrementing bulletin ID. [:type, :int], # Type of bulletin. Valid values include: # 1 = Regular news bulletin @@ -212,22 +213,22 @@ # Receive Reuters global fundamental market data. There must be a subscription to # Reuters Fundamental set up in Account Management before you can receive this data. FundamentalData = def_message 50, [:id, :int], # request_id [:data, :string] - ContractDataEnd = def_message(52, [:id, :int]) { "<ContractDataEnd: #{@data[:id]}>" } # request_id + ContractDataEnd = def_message(52, [:id, :int]) { "<ContractDataEnd: #{id}>" } # request_id OpenOrderEnd = def_message(53) { "<OpenOrderEnd>" } AccountDownloadEnd = def_message(54, [:account_name, :string]) do - "<AccountDownloadEnd: #{@data[:account_name]}}>" + "<AccountDownloadEnd: #{account_name}}>" end # request_id - ExecutionDataEnd = def_message 55, [:id, :int] # request_id + ExecutionDataEnd = def_message(55, [:id, :int]) { "<ExecutionDataEnd: #{id}>" } # request_id - TickSnapshotEnd = def_message 57, [:id, :int] # request_id + TickSnapshotEnd = def_message(57, [:id, :int]) { "<TickSnapshotEnd: #{id}>" } # request_id ### Actual message classes (long definitions): # The IB code seems to dispatch up to two wrapped objects for this message, a tickPrice # and sometimes a tickSize, which seems to be identical to the TICK_SIZE object. @@ -342,14 +343,13 @@ read_computed :theta, -2 # -2 is the "not yet computed" indicator read_computed :under_price, -1 # -1 is the "not yet computed" indicator end def to_human - "<TickOption #{type} for #{@data[:id]}: underlying @ #{@data[:under_price]}, "+ - "option @ #{@data[:option_price]}, IV #{@data[:implied_volatility]}%, " + - "delta #{@data[:delta]}, gamma #{@data[:gamma]}, vega #{@data[:vega]}, " + - "theta #{@data[:theta]}, pv_dividend #{@data[:pv_dividend]}>" + "<TickOption #{type} for #{:id}: underlying @ #{under_price}, "+ + "option @ #{option_price}, IV #{implied_volatility}%, delta #{delta}, " + + "gamma #{gamma}, vega #{vega}, theta #{theta}, pv_dividend #{pv_dividend}>" end end # TickOption TickOptionComputation = TickOption MarketDepth = @@ -371,12 +371,12 @@ def operation @data[:operation] == 0 ? :insert : @data[:operation] == 1 ? :update : :delete end def to_human - "<#{self.class.to_s.split('::').last}: #{operation} #{side} @ "+ - "#{@data[:position]} = #{@data[:price]} x #{@data[:size]}>" + "<#{self.class.to_s.split(/::/).last}: #{operation} #{side} @ "+ + "#{position} = #{price} x #{size}>" end end MarketDepthL2 = def_message 13, MarketDepth, @@ -413,17 +413,18 @@ def to_human "TWS #{ error? ? 'Error' : system? ? 'System' : 'Warning' } Message #{code}: #{message}" end - end # class ErrorMessage + end # class Alert Error = Alert ErrorMessage = Alert class OpenOrder < AbstractMessage @message_id = 5 + # TODO: Add id accessor to unify with OrderStatus message attr_accessor :order, :contract def load super @@ -655,10 +656,14 @@ :client_id => @socket.read_int, :liquidation => @socket.read_int, :cumulative_quantity => @socket.read_int, :average_price => @socket.read_decimal end + + def to_human + "<HistoricalData: req: #{id}, #{item_count} items, #{start_date} to #{end_date}>" + end end # ExecutionData # HistoricalData contains following @data: # General: # :id - The ID of the request to which this is responding @@ -700,12 +705,11 @@ :trades => @socket.read_int end end def to_human - "<HistoricalData: req id #{@data[:id]}, #{@data[:item_count] - } items, from #{@data[:start_date_str]} to #{@data[:end_date_str]}>" + "<HistoricalData: req: #{id}, #{item_count} items, #{start_date} to #{end_date}>" end end # HistoricalData class BondContractData < AbstractMessage @message_id = 18 @@ -816,10 +820,10 @@ :wap => @socket.read_decimal, :trades => @socket.read_int end def to_human - "<RealTimeBar: req id #{@data[:id]}, #{@bar}>" + "<RealTimeBar: req: #{id}, #{bar}>" end end # RealTimeBar RealTimeBars = RealTimeBar # The server sends this message upon accepting a Delta-Neutral DN RFQ