Class: IB::Datatypes::Contract

Inherits:
AbstractDatum show all
Defined in:
lib/ib-ruby/datatypes.rb

Constant Summary collapse

SECURITY_TYPES =

Valid security types (sec_type attribute)

{
  :stock => "STK",
  :option => "OPT",
  :future => "FUT",
  :index => "IND",
  :futures_option => "FOP",
  :forex => "CASH",
  :bag => "BAG"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractDatum

#initialize

Constructor Details

This class inherits a constructor from IB::Datatypes::AbstractDatum

Instance Attribute Details

#bond_typeObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def bond_type
  @bond_type
end

#callableObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def callable
  @callable
end

#combo_legsObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def combo_legs
  @combo_legs
end

#convertibleObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def convertible
  @convertible
end

#couponObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def coupon
  @coupon
end

#coupon_typeObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def coupon_type
  @coupon_type
end

#currencyObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def currency
  @currency
end

#cusipObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def cusip
  @cusip
end

#desc_appendObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def desc_append
  @desc_append
end

#descriptionObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def description
  @description
end

#exchangeObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def exchange
  @exchange
end

#expiryObject

Returns the value of attribute expiry.



176
177
178
# File 'lib/ib-ruby/datatypes.rb', line 176

def expiry
  @expiry
end

#issue_dateObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def issue_date
  @issue_date
end

#local_symbolObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def local_symbol
  @local_symbol
end

#maturityObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def maturity
  @maturity
end

#multiplierObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def multiplier
  @multiplier
end

#primary_exchangeObject

Returns the value of attribute primary_exchange.



176
177
178
# File 'lib/ib-ruby/datatypes.rb', line 176

def primary_exchange
  @primary_exchange
end

#puttableObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def puttable
  @puttable
end

#ratingsObject

Bond values



173
174
175
# File 'lib/ib-ruby/datatypes.rb', line 173

def ratings
  @ratings
end

#rightObject

Returns the value of attribute right.



176
177
178
# File 'lib/ib-ruby/datatypes.rb', line 176

def right
  @right
end

#sec_typeObject

Returns the value of attribute sec_type.



176
177
178
# File 'lib/ib-ruby/datatypes.rb', line 176

def sec_type
  @sec_type
end

#strikeObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def strike
  @strike
end

#symbolObject

note that the :description field is entirely local to ib-ruby, and not part of TWS. You can use it to store whatever arbitrary data you want.



169
170
171
# File 'lib/ib-ruby/datatypes.rb', line 169

def symbol
  @symbol
end

Class Method Details

.from_ib_ruby(string) ⇒ Object

This returns a Contract initialized from the serialize_ib_ruby format string.



274
275
276
277
278
279
# File 'lib/ib-ruby/datatypes.rb', line 274

def self.from_ib_ruby(string)
  c = Contract.new
  c.symbol, c.sec_type, c.expiry, c.strike, c.right, c.multiplier, c.exchange, c.primary_exchange, c.currency, c.local_symbol = string.split(":")

  c
end

Instance Method Details

#initObject



290
291
292
293
294
295
296
# File 'lib/ib-ruby/datatypes.rb', line 290

def init
  super

  @combo_legs = Array.new
  @strike = 0
  @sec_type = ''
end

#resetObject



212
213
214
215
# File 'lib/ib-ruby/datatypes.rb', line 212

def reset
  @combo_legs = Array.new
  @strike = 0
end

#serialize_combo_legs(include_open_close = false) ⇒ Object

Some messages send open_close too, some don’t. WTF.



282
283
284
285
286
287
288
# File 'lib/ib-ruby/datatypes.rb', line 282

def serialize_combo_legs(include_open_close = false)
  if self.combo_legs.nil?
    [0]
  else
    [ self.combo_legs.size ].concat(self.combo_legs.serialize(include_open_close))
  end
end

#serialize_ib_ruby(version) ⇒ Object

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:


269
270
271
# File 'lib/ib-ruby/datatypes.rb', line 269

def serialize_ib_ruby(version)
  serialize_long(version).join(":")
end

#serialize_long(version) ⇒ Object

This returns an Array of data from the given contract, in standard format. Note that it does not include the combo legs.



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ib-ruby/datatypes.rb', line 237

def serialize_long(version)
  queue = [
            self.symbol,
            self.sec_type,
            self.expiry,
            self.strike,
            self.right
          ]

  queue.push(self.multiplier) if version >= 15
  queue.push(self.exchange)
  queue.push(self.primary_exchange) if version >= 14
  queue.push(self.currency)
  queue.push(self.local_symbol) if version >= 2

  queue
end

#serialize_short(version) ⇒ Object

Different messages serialize contracts differently. Go figure.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/ib-ruby/datatypes.rb', line 218

def serialize_short(version)
  q = [ self.symbol,
        self.sec_type,
        self.expiry,
        self.strike,
        self.right ]

  q.push(self.multiplier) if version >= 15
  q.concat([
            self.exchange,
            self.currency,
            self.local_symbol
           ])

  q
end

#to_humanObject



298
299
300
# File 'lib/ib-ruby/datatypes.rb', line 298

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

#to_sObject



306
307
308
# File 'lib/ib-ruby/datatypes.rb', line 306

def to_s
  to_human
end

#to_shortObject



302
303
304
# File 'lib/ib-ruby/datatypes.rb', line 302

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