Class: IB::Option

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

Direct Known Subclasses

FutureOption

Constant Summary

Constants inherited from Contract

Contract::Subclasses

Instance Attribute Summary

Attributes inherited from Contract

#description

Class Method Summary collapse

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

Class Method Details

.from_osi(osi) ⇒ Object

Make valid IB Contract definition from OSI (Option Symbology Initiative) code. NB: Simply making a new Contract with local_symbol (osi) property set to a valid OSI code works just as well, just do NOT set expiry, right or strike properties in this case. This class method provided as a backup and shows how to analyse OSI codes.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/models/ib/option.rb', line 32

def self.from_osi osi

  # Parse contract's OSI (OCC Option Symbology Initiative) code
  args = osi.match(/(\w+)\s?(\d\d)(\d\d)(\d\d)([pcPC])(\d+)/).to_a.drop(1)
  symbol = args.shift
  year = 2000 + args.shift.to_i
  month = args.shift.to_i
  day = args.shift.to_i
  right = args.shift.upcase
  strike = args.shift.to_i/1000.0

  # Set correct expiry date - IB expiry date differs from OSI if expiry date
  # falls on Saturday (see https://github.com/arvicco/option_mower/issues/4)
  expiry_date = Time.utc(year, month, day)
  expiry_date = Time.utc(year, month, day-1) if expiry_date.wday == 6

  new :symbol => symbol,
    :exchange => "SMART",
    :expiry => expiry_date.to_ib[2..7], # YYMMDD
    :right => right,
    :strike => strike
end

Instance Method Details

#==(other) ⇒ Object

self ||= osi ? osi : “#symbol #strike #right #Contract#expiry



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

def == other
    super(other) || (  # finish positive, if contract#== is true
										  # otherwise, we most probably compare the response from IB with our selfmade input
	exchange == other.exchange &&
	include_expired == other.include_expired &&
	sec_type == other.sec_type  &&
	multiplier == other.multiplier &&
	strike == other.strike &&
	right == other.right &&
	multiplier == other.multiplier &&
	expiry == other.expiry )

end

#default_attributesObject



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

def default_attributes
  super.merge :sec_type => :option
  #self[:description] ||= osi ? osi : "#{symbol} #{strike} #{right} #{expiry}"
end

#osi=(value) ⇒ Object



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

def osi= value
  # Normalize to 21 char
  self.local_symbol = value.sub(/ /, ' '*(22-value.size))
end

#to_humanObject



73
74
75
# File 'lib/models/ib/option.rb', line 73

def to_human
  "<Option: " + [symbol, expiry, right, strike, exchange, currency].join(" ") + ">"
end