Class: IB::Bag

Inherits:
Contract show all
Defined in:
lib/models/ib/bag.rb

Overview

“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.

Direct Known Subclasses

Spread

Constant Summary

Constants inherited from Contract

Contract::Subclasses

Instance Attribute Summary

Attributes inherited from Contract

#description

Instance Method Summary collapse

Methods inherited from Contract

#bag?, #bond?, build, #essential, #expiry, #index?, #merge, #option?, #order_requirements, #serialize, #serialize_ib_ruby, #serialize_legs, #serialize_long, #serialize_short, #serialize_supershort, #serialize_under_comp, #stock?, #table_header, #table_row, #to_s, #to_short, #verify

Methods included from BaseProperties

#as_table, #content_attributes, #invariant_attributes, #set_attribute_defaults, #table_header, #table_row, #update_missing

Instance Method Details

#==(other) ⇒ Object

Contract comparison



56
57
58
# File 'lib/models/ib/bag.rb', line 56

def == other
  super && same_legs?(other)
end

#con_id=(arg) ⇒ Object



36
37
38
# File 'lib/models/ib/bag.rb', line 36

def con_id= arg
	# dont' update con_id
end

#default_attributesObject



24
25
26
# File 'lib/models/ib/bag.rb', line 24

def default_attributes
  super.merge :sec_type => :bag #,:legs => Array.new,
end

#legs_descriptionObject

TODO: Rewrite with legs and legs_description being strictly in sync… TODO: Find a way to serialize legs without references… IB-equivalent leg description.



45
46
47
# File 'lib/models/ib/bag.rb', line 45

def legs_description
  self[:legs_description] || combo_legs.map { |the_leg| "#{the_leg.con_id}|#{the_leg.weight}" }.join(',')
end

#same_legs?(other) ⇒ Boolean

Check if two Contracts have same legs (maybe in different order)

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/models/ib/bag.rb', line 50

def same_legs? other
  combo_legs == other.combo_legs ||
    legs_description.split(',').sort == other.legs_description.split(',').sort
end

#to_humanObject

def description

  self[:description] || to_human
end


32
33
34
# File 'lib/models/ib/bag.rb', line 32

def to_human
  "<Bag: #{[symbol, exchange, currency].join(' ')} legs: #{legs_description} >"
end