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.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseProperties

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

Class Method Details

.build(*fields) ⇒ Object

z= Strangle.build from: Symbols::Index.stoxx, p: 3700, c: 4000, expiry: 202106 zc= z.combo_legs.serialize :extended

> [[321584786, 1, “BUY”, “DTB”, 0, 0, “”, -1], [321584637, 1, “BUY”, “DTB”, 0, 0, “”, -1]]

nz = zc.map{|o| ComboLeg.build *o } zc.map{|o| ComboLeg.build o } => # is equivalent

> [#<IB::ComboLeg:0x0000000001c36bc0 @attributes={:con_id=>321584786, :ratio=>1, :side=>“B”, :exchange=>“DTB”, …

nz.first == z.combo_legs.first => true



97
98
99
100
101
102
103
104
105
106
# File 'lib/models/ib/combo_leg.rb', line 97

def self.build *fields
  self.new Hash[[:con_id,
             :ratio,
             :side,    #  reverse to_sup?
             :exchange,
             :open_close,
             :short_sale_slot,
             :designated_location,
             :exempt_code].zip fields]
end

Instance Method Details

#==(other) ⇒ Object

Order comparison



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/models/ib/combo_leg.rb', line 113

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



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

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.



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

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



108
109
110
# File 'lib/models/ib/combo_leg.rb', line 108

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

#weightObject

Leg’s weight is a combination of action and ratio



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

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

#weight=(value) ⇒ Object



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

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