lib/ib-ruby/models/contract.rb in ib-ruby-0.5.2 vs lib/ib-ruby/models/contract.rb in ib-ruby-0.5.7

- old
+ new

@@ -166,11 +166,11 @@ end # This returns an Array of data from the given contract. # Different messages serialize contracts differently. Go figure. # Note that it does NOT include the combo legs. - def serialize(*fields) + def serialize *fields [(fields.include?(:con_id) ? [con_id] : []), symbol, sec_type, (fields.include?(:option) ? [expiry, strike, right, multiplier] : []), exchange, @@ -180,16 +180,16 @@ (fields.include?(:sec_id) ? [sec_id_type, sec_id] : []), (fields.include?(:include_expired) ? [include_expired] : []), ].flatten end - def serialize_long(*fields) - serialize(:option, :primary_exchange, *fields) + def serialize_long *fields + serialize :option, :primary_exchange, *fields end - def serialize_short(*fields) - serialize(:option, *fields) + def serialize_short *fields + serialize :option, *fields end # This produces a string uniquely identifying this contract, in the format used # for command line arguments in the IB-Ruby examples. The format is: # @@ -200,24 +200,24 @@ # # For example, to query the British pound futures contract trading on Globex # expiring in September, 2008, the string is: # # GBP:FUT:200809:::62500:GLOBEX::USD: - def serialize_ib_ruby(version) + def serialize_ib_ruby version serialize.join(":") end # This returns a Contract initialized from the serialize_ib_ruby format string. - def self.from_ib_ruby(string) + def self.from_ib_ruby string c = Contract.new c.symbol, c.sec_type, c.expiry, c.strike, c.right, c.multiplier, c.exchange, c.primary_exchange, c.currency, c.local_symbol = string.split(":") c end # Serialize under_comp parameters - def serialize_under_comp(*args) + def serialize_under_comp *args # EClientSocket.java, line 471: if under_comp [true, under_con_id, under_delta, @@ -226,16 +226,18 @@ [false] end end # Some messages send open_close too, some don't. WTF. - def serialize_combo_legs(type = :short) - # No idea what "BAG" means. Copied from the Java code. + # "BAG" is not really a contract, but a combination (combo) of securities. + # AKA basket or bag of securities. Individual securities in combo are represented + # by ComboLeg objects. + def serialize_combo_legs *fields return [] unless sec_type.upcase == "BAG" return [0] if combo_legs.empty? || combo_legs.nil? [combo_legs.size, - combo_legs.map { |leg| leg.serialize(type) }] + combo_legs.map { |leg| leg.serialize *fields }] end def to_human "<Contract: " + [symbol, expiry, sec_type, strike, right, exchange, currency].join("-") + ">" end @@ -245,62 +247,9 @@ end def to_s to_human end - - # ComboLeg is an internal class of Contract, as it should be - class ComboLeg < Model - # // open/close leg value is same as combo - # Specifies whether the order is an open or close order. Valid values are: - SAME = 0 # Same as the parent security. The only option for retail customers. - OPEN = 1 # Open. This value is only valid for institutional customers. - CLOSE = 2 # Close. This value is only valid for institutional customers. - UNKNOWN = 3 - - - attr_accessor :con_id, # int: The unique contract identifier specifying the security. - :ratio, # int: Select the relative number of contracts for the leg you - # are constructing. To help determine the ratio for a - # specific combination order, refer to the Interactive - # Analytics section of the User's Guide. - - :action, # String: BUY/SELL/SSHORT/SSHORTX - # The side (buy or sell) for the leg you are constructing. - :exchange, # String: exchange to which the complete combination - # order will be routed. - :open_close, # int: Specifies whether the order is an open or close order. - # Valid values: ComboLeg::SAME/OPEN/CLOSE/UNKNOWN - - # For institutional customers only! For stock legs when doing short sale - :short_sale_slot, # int: 0 - retail, 1 = clearing broker, 2 = third party - :designated_location, # String: Only for shortSaleSlot == 2. - # Otherwise leave blank or orders will be rejected. - :exempt_code # int: ? - - def initialize opts = {} - @con_id = 0 - @ratio = 0 - @open_close = 0 - @short_sale_slot = 0 - @designated_location = '' - @exempt_code = -1 - - super opts - end - - # Some messages include open_close, some don't. wtf. - def serialize(type = :short) - [con_id, - ratio, - action, - exchange] + - type == :short ? [] : [open_close, - short_sale_slot, - designated_location, - exempt_code] - end - end # ComboLeg end # class Contract end # module Models end # module IB