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



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

def bond_type
  @bond_type
end

#callableObject

Bond values



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

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.



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

def combo_legs
  @combo_legs
end

#convertibleObject

Bond values



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

def convertible
  @convertible
end

#couponObject

Bond values



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

def coupon
  @coupon
end

#coupon_typeObject

Bond values



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

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.



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

def currency
  @currency
end

#cusipObject

Bond values



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

def cusip
  @cusip
end

#desc_appendObject

Bond values



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

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.



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

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.



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

def exchange
  @exchange
end

#expiryObject

Returns the value of attribute expiry.



180
181
182
# File 'lib/ib-ruby/datatypes.rb', line 180

def expiry
  @expiry
end

#issue_dateObject

Bond values



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

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.



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

def local_symbol
  @local_symbol
end

#maturityObject

Bond values



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

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.



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

def multiplier
  @multiplier
end

#primary_exchangeObject

Returns the value of attribute primary_exchange.



180
181
182
# File 'lib/ib-ruby/datatypes.rb', line 180

def primary_exchange
  @primary_exchange
end

#puttableObject

Bond values



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

def puttable
  @puttable
end

#ratingsObject

Bond values



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

def ratings
  @ratings
end

#rightObject

Returns the value of attribute right.



180
181
182
# File 'lib/ib-ruby/datatypes.rb', line 180

def right
  @right
end

#sec_typeObject

Returns the value of attribute sec_type.



180
181
182
# File 'lib/ib-ruby/datatypes.rb', line 180

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.



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

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.



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

def symbol
  @symbol
end

Class Method Details

.from_ib_ruby(string) ⇒ Object

This returns a Contract initialized from the serialize_ib_ruby format string.



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

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



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

def init
  super

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

#resetObject



214
215
216
217
# File 'lib/ib-ruby/datatypes.rb', line 214

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.



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

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:


271
272
273
# File 'lib/ib-ruby/datatypes.rb', line 271

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.



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

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.



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

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



300
301
302
# File 'lib/ib-ruby/datatypes.rb', line 300

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

#to_sObject



308
309
310
# File 'lib/ib-ruby/datatypes.rb', line 308

def to_s
  to_human
end

#to_shortObject



304
305
306
# File 'lib/ib-ruby/datatypes.rb', line 304

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