class IB::Contract

Constants

Subclasses

Contract subclasses representing specialized security types. Most security types do not have their own subclass, they use generic Contract class.

Attributes

description[RW]

Public Class Methods

build(opts = {}) click to toggle source

This builds an appropriate Contract subclass based on its type

# File lib/models/ib/contract.rb, line 278
def self.build opts = {}
  subclass = VALUES[:sec_type][opts[:sec_type]] || opts[:sec_type].to_sym
  Contract::Subclasses[subclass].new opts
end
from_ib_ruby(string) click to toggle source

This returns a Contract initialized from the serialize_ib_ruby format string.

# File lib/models/ib/contract.rb, line 284
def self.from_ib_ruby string
  keys = [:symbol, :sec_type, :expiry, :strike, :right, :multiplier,
          :exchange, :primary_exchange, :currency, :local_symbol]
  props = Hash[keys.zip(string.split(":"))]
  props.delete_if { |k, v| v.nil? || v.empty? }
  Contract.build props
end

Public Instance Methods

==(other) click to toggle source

Contract comparison

Calls superclass method IB::BaseProperties#==
# File lib/models/ib/contract.rb, line 181
def == other
  return true if super(other)

  return false unless other.is_a?(self.class)

  # Different sec_id_type
  return false if sec_id_type && other.sec_id_type && sec_id_type != other.sec_id_type

  # Different sec_id
  return false if sec_id && other.sec_id && sec_id != other.sec_id

  # Different symbols
  return false if symbol && other.symbol && symbol != other.symbol

  # Different currency
  return false if currency && other.currency && currency != other.currency

  # Same con_id for all Bags, but unknown for new Contracts...
  # 0 or nil con_id  matches any
  return false if con_id != 0 && other.con_id != 0 &&
    con_id && other.con_id && con_id != other.con_id

  # SMART or nil exchange matches any
  return false if exchange != 'SMART' && other.exchange != 'SMART' &&
    exchange && other.exchange && exchange != other.exchange

  # Comparison for Bonds and Options
  if bond? || option?
    return false if right != other.right || strike != other.strike
    return false if multiplier && other.multiplier &&
      multiplier != other.multiplier
    return false if expiry && expiry[0..5] != other.expiry[0..5]
    return false unless expiry && (expiry[6..7] == other.expiry[6..7] ||
                                   expiry[6..7].empty? || other.expiry[6..7].empty?)
  end

  # All else being equal...
  sec_type == other.sec_type
end
bag?() click to toggle source

Testing for type of contract:

# File lib/models/ib/contract.rb, line 246
def bag?
  self[:sec_type] == 'BAG'
end
bond?() click to toggle source
# File lib/models/ib/contract.rb, line 250
def bond?
  self[:sec_type] == 'BOND'
end
default_attributes() click to toggle source
Calls superclass method IB::BaseProperties#default_attributes
# File lib/models/ib/contract.rb, line 109
def default_attributes
  super.merge :con_id => 0,
    :strike => 0.0,
    :right => :none, # Not an option
    :exchange => 'SMART',
    :include_expired => false
end
option?() click to toggle source
# File lib/models/ib/contract.rb, line 258
def option?
  self[:sec_type] == 'OPT'
end
serialize(*fields) click to toggle source

This returns an Array of data from the given contract. Different messages serialize contracts differently. Go figure. Note that it does NOT include the combo legs. serialize [:option, :con_id, :include_expired, :sec_id]

# File lib/models/ib/contract.rb, line 121
def serialize *fields
  [(fields.include?(:con_id) ? [con_id] : []),
   symbol,
   self[:sec_type],
   (fields.include?(:option) ?
    [expiry,
     strike,
     self[:right],
     multiplier] : []),
   exchange,
   (fields.include?(:primary_exchange) ? [primary_exchange] : []),
   currency,
   local_symbol,
   (fields.include?(:sec_id) ? [sec_id_type, sec_id] : []),
   (fields.include?(:include_expired) ? [include_expired] : []),
   ].flatten
end
serialize_ib_ruby() click to toggle source

This produces a string uniquely identifying this contract, in the format used for command line arguments in the IB-Ruby examples. The format is:

symbol:sec_type:expiry:strike:right:multiplier:exchange:primary_exchange:currency:local_symbol

Fields not needed for a particular security should be left blank (e.g. strike and right are only relevant for options.)

For example, to query the British pound futures contract trading on Globex expiring in September, 2008, the string is:

GBP:FUT:200809:::62500:GLOBEX::USD:
# File lib/models/ib/contract.rb, line 176
def serialize_ib_ruby
  serialize_long.join(":")
end
serialize_legs(*fields) click to toggle source

Defined in Contract, not BAG subclass to keep code DRY

# File lib/models/ib/contract.rb, line 153
def serialize_legs *fields
  case
  when !bag?
    []
  when legs.empty?
    [0]
  else
    [legs.size, legs.map { |leg| leg.serialize *fields }].flatten
  end
end
serialize_long(*fields) click to toggle source
# File lib/models/ib/contract.rb, line 139
def serialize_long *fields
  serialize :option, :primary_exchange, *fields
end
serialize_short(*fields) click to toggle source
# File lib/models/ib/contract.rb, line 143
def serialize_short *fields
  serialize :option, *fields
end
serialize_under_comp(*args) click to toggle source

Serialize under_comp parameters: EClientSocket.java, line 471

# File lib/models/ib/contract.rb, line 148
def serialize_under_comp *args
  under_comp ? under_comp.serialize : [false]
end
stock?() click to toggle source
# File lib/models/ib/contract.rb, line 254
def stock?
  self[:sec_type] == 'STK'
end
to_human() click to toggle source
# File lib/models/ib/contract.rb, line 228
def to_human
  "<Contract: " +
    [symbol,
     sec_type,
     (expiry == '' ? nil : expiry),
     (right == :none ? nil : right),
     (strike == 0 ? nil : strike),
     exchange,
     currency
     ].compact.join(" ") + ">"
end
to_s() click to toggle source
# File lib/models/ib/contract.rb, line 221
def to_s
  "<Contract: " + instance_variables.map do |key|
    value = send(key[1..-1])
    " #{key}=#{value}" unless value.nil? || value == '' || value == 0
  end.compact.join(',') + " >"
end
to_short() click to toggle source
# File lib/models/ib/contract.rb, line 240
def to_short
  "#{symbol}#{expiry}#{strike}#{right}#{exchange}#{currency}"
end