Class: IB::Models::Contract

Inherits:
Model
  • Object
show all
Defined in:
lib/ib-ruby/models/contract.rb,
lib/ib-ruby/models/contract/option.rb

Direct Known Subclasses

Option

Defined Under Namespace

Classes: Option

Constant Summary collapse

TYPES =

Specialized Contract subclasses representing different security types

{}
DEFAULT_PROPS =
{:con_id => 0,
:strike => 0,
:exchange => 'SMART',
:include_expired => false,

# These properties are from ContractDetails
:under_con_id => 0,
:min_tick => 0,
:callable => false,
:puttable => false,
:coupon => 0,
:convertible => false,
:next_option_partial => false, }

Instance Attribute Summary collapse

Attributes inherited from Model

#created_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#[], #[]=, #initialize

Methods included from ModelProperties

#define_property, #define_property_methods, #prop

Constructor Details

This class inherits a constructor from IB::Models::Model

Instance Attribute Details

#descriptionObject

NB: local to ib-ruby, not part of TWS.



165
166
167
# File 'lib/ib-ruby/models/contract.rb', line 165

def description
  @description
end

#legsObject Also known as: combo_legs

Legs arriving via OpenOrder message, need to define them here



159
160
161
# File 'lib/ib-ruby/models/contract.rb', line 159

def legs
  @legs
end

Class Method Details

.build(opts = {}) ⇒ Object

This returns a Contract initialized from the serialize_ib_ruby format string.



12
13
14
15
16
17
18
19
# File 'lib/ib-ruby/models/contract.rb', line 12

def self.build opts = {}
  type = opts[:sec_type]
  if TYPES[type]
    TYPES[type].new opts
  else
    Contract.new opts
  end
end

.from_ib_ruby(string) ⇒ Object

This returns a Contract initialized from the serialize_ib_ruby format string.



22
23
24
25
26
27
28
# File 'lib/ib-ruby/models/contract.rb', line 22

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.new props
end

Instance Method Details

#==(other) ⇒ Object

Contract comparison



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/ib-ruby/models/contract.rb', line 246

def == 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 under_comp
  return false if under_comp && other.under_comp && under_comp != other.under_comp

  # 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 sec_type == SECURITY_TYPES[:bond] || sec_type == SECURITY_TYPES[:option]
    return false if right != other.right || strike != other.strike
    return false if multiplier && other.multiplier && multiplier != other.multiplier
    return false if expiry[0..5] != other.expiry[0..5]
    return false unless 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

#serialize(*fields) ⇒ Object

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.



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ib-ruby/models/contract.rb', line 189

def serialize *fields
  [(fields.include?(:con_id) ? [con_id] : []),
   symbol,
   sec_type,
   (fields.include?(:option) ? [expiry, strike, 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_rubyObject

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:security_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:


241
242
243
# File 'lib/ib-ruby/models/contract.rb', line 241

def serialize_ib_ruby
  serialize_long.join(":")
end

#serialize_legs(*fields) ⇒ Object

Redefined in BAG subclass



225
226
227
# File 'lib/ib-ruby/models/contract.rb', line 225

def serialize_legs *fields
  []
end

#serialize_long(*fields) ⇒ Object



203
204
205
# File 'lib/ib-ruby/models/contract.rb', line 203

def serialize_long *fields
  serialize :option, :primary_exchange, *fields
end

#serialize_short(*fields) ⇒ Object



207
208
209
# File 'lib/ib-ruby/models/contract.rb', line 207

def serialize_short *fields
  serialize :option, *fields
end

#serialize_under_comp(*args) ⇒ Object

Serialize under_comp parameters



212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ib-ruby/models/contract.rb', line 212

def serialize_under_comp *args
  # EClientSocket.java, line 471:
  if under_comp
    [true,
     under_con_id,
     under_delta,
     under_price]
  else
    [false]
  end
end

#summaryObject

NB: ContractDetails reference - to self!



182
183
184
# File 'lib/ib-ruby/models/contract.rb', line 182

def summary
  self
end

#to_humanObject



293
294
295
# File 'lib/ib-ruby/models/contract.rb', line 293

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

#to_sObject



286
287
288
289
290
291
# File 'lib/ib-ruby/models/contract.rb', line 286

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_shortObject



297
298
299
# File 'lib/ib-ruby/models/contract.rb', line 297

def to_short
  "#{symbol}#{expiry}#{strike}#{right}#{exchange}#{currency}"
end