Class: IB::ComboLeg

Inherits:
Model show all
Includes:
BaseProperties
Defined in:
lib/models/ib/combo_leg.rb

Overview

ComboLeg is essentially a join Model between Combo (BAG) Contract and individual Contracts (securities) that this BAG contains.

Instance Method Summary collapse

Methods included from BaseProperties

#content_attributes, #set_attribute_defaults, #update_missing

Instance Method Details

#==(other) ⇒ Object

Order comparison



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/models/ib/combo_leg.rb', line 89

def == other
  super(other) ||
    other.is_a?(self.class) &&
    con_id == other.con_id &&
    ratio == other.ratio &&
    open_close == other.open_close && 
    short_sale_slot == other.short_sale_slot &&
    exempt_code == other.exempt_code &&
    side == other.side &&
    exchange == other.exchange &&
    designated_location == other.designated_location
end

#default_attributesObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/models/ib/combo_leg.rb', line 42

def default_attributes
  super.merge :con_id => 0,
    :ratio => 1,
    :side => :buy,
    :open_close => :same, # The only option for retail customers.
    :short_sale_slot => :default,
    :designated_location => '',
    :exchange => 'SMART', # Unless SMART, Order modification fails
    :exempt_code => -1
end

#serialize(*fields) ⇒ Object

Some messages include open_close, some don’t. wtf.



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/models/ib/combo_leg.rb', line 70

def serialize *fields
  [con_id,
   ratio,
   side.to_sup,
   exchange,
   (fields.include?(:extended) ?
    [self[:open_close],
     self[:short_sale_slot],
     designated_location,
     exempt_code] :
    [])
   ].flatten
end

#to_humanObject



84
85
86
# File 'lib/models/ib/combo_leg.rb', line 84

def to_human
  "<ComboLeg: #{side} #{ratio} con_id #{con_id} at #{exchange}>"
end

#weightObject

Leg’s weight is a combination of action and ratio



54
55
56
# File 'lib/models/ib/combo_leg.rb', line 54

def weight
  side == :buy ? ratio : -ratio
end

#weight=(value) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/models/ib/combo_leg.rb', line 58

def weight= value
  value = value.to_i
  if value > 0
    self.side = :buy
    self.ratio = value
  else
    self.side = :sell
    self.ratio = -value
  end
end