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.

Constant Summary

Constants inherited from Contract

Contract::Subclasses

Instance Method Summary collapse

Methods inherited from Contract

#bag?, #bond?, build, from_ib_ruby, #option?, #serialize, #serialize_ib_ruby, #serialize_legs, #serialize_long, #serialize_short, #serialize_under_comp, #stock?, #to_s, #to_short

Methods included from BaseProperties

#content_attributes, #set_attribute_defaults, #update_missing

Instance Method Details

#==(other) ⇒ Object

Contract comparison



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

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

#default_attributesObject



18
19
20
# File 'lib/models/ib/bag.rb', line 18

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

#descriptionObject



22
23
24
# File 'lib/models/ib/bag.rb', line 22

def description
  self[:description] || to_human
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.



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

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

#same_legs?(other) ⇒ Boolean

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

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/models/ib/bag.rb', line 40

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

#to_humanObject



26
27
28
# File 'lib/models/ib/bag.rb', line 26

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