Class: IB::Order

Inherits:
Model show all
Includes:
BaseProperties
Defined in:
lib/models/ib/order.rb

Instance Method Summary collapse

Methods included from BaseProperties

#content_attributes, #set_attribute_defaults, #update_missing

Instance Method Details

#==(other) ⇒ Object

Order comparison



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/models/ib/order.rb', line 350

def == other
      super(other) ||
other.is_a?(self.class) &&
(perm_id && other.perm_id && perm_id == other.perm_id ||
 local_id == other.local_id && # ((p __LINE__)||true) &&
 (client_id == other.client_id || client_id == 0 || other.client_id == 0) &&
 parent_id == other.parent_id &&
 tif == other.tif &&
 action == other.action &&
 order_type == other.order_type &&
 quantity == other.quantity &&
 (limit_price == other.limit_price || # TODO Floats should be Decimals!
  (limit_price - other.limit_price).abs < 0.00001) &&
 aux_price == other.aux_price &&
 origin == other.origin &&
 designated_location == other.designated_location &&
 exempt_code == other.exempt_code &&
 what_if == other.what_if &&
 algo_strategy == other.algo_strategy &&
 algo_params == other.algo_params)

# TODO: compare more attributes!
end

#default_attributesObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/models/ib/order.rb', line 289

def default_attributes
      super.merge :aux_price => 0.0,
:discretionary_amount => 0.0,
:parent_id => 0,
:tif => :day,
:order_type => :limit,
:open_close => :open,
:origin => :customer,
:short_sale_slot => :default,
:trigger_method => :default,
:oca_type => :none,
:auction_strategy => :none,
:designated_location => '',
:exempt_code => -1,
:display_size => 0,
:continuous_update => 0,
:delta_neutral_con_id => 0,
:algo_strategy => '',
:transmit => true,
:what_if => false,
:leg_prices => [],
:algo_params => HashWithIndifferentAccess.new, #{},
:combo_params => HashWithIndifferentAccess.new, #{},
:order_state => IB::OrderState.new(:status => 'New',
                                   :filled => 0,
                                   :remaining => 0,
                                   :price => 0,
                                   :average_price => 0)
end

#modify(contract, connection, time = Time.now) ⇒ Object

Modify Order (convenience wrapper for send_message :PlaceOrder). Returns local_id.



340
341
342
343
344
345
346
347
# File 'lib/models/ib/order.rb', line 340

def modify contract, connection, time=Time.now
  self.modified_at = time
  connection.send_message :PlaceOrder,
    :order => self,
    :contract => contract,
    :local_id => local_id
  local_id
end

#order_stateObject



239
240
241
# File 'lib/models/ib/order.rb', line 239

def order_state
  order_states.last
end

#order_state=(state) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/models/ib/order.rb', line 243

def order_state= state
  self.order_states.push case state
  when IB::OrderState
    state
  when Symbol, String
    IB::OrderState.new :status => state
  end
end

#place(contract, connection) ⇒ Object

Placement



330
331
332
333
334
335
336
337
# File 'lib/models/ib/order.rb', line 330

def place contract, connection
  error "Unable to place order, next_local_id not known" unless connection.next_local_id
  self.client_id = connection.server[:client_id]
  self.local_id = connection.next_local_id
  connection.next_local_id += 1
  self.placed_at = Time.now
  modify contract, connection, self.placed_at
end

#serialize_algoObject



319
320
321
322
323
324
325
326
327
# File 'lib/models/ib/order.rb', line 319

def serialize_algo
  if algo_strategy.nil? || algo_strategy.empty?
    ''
  else
    [algo_strategy,
     algo_params.size,
     algo_params.to_a]
  end
end

#to_humanObject



381
382
383
384
385
386
387
388
389
# File 'lib/models/ib/order.rb', line 381

def to_human
  "<Order: " + ((order_ref && order_ref != '') ? "#{order_ref} " : '') +
    "#{self[:order_type]} #{self[:tif]} #{side} #{quantity} " +
    "#{status} " + (limit_price ? "#{limit_price} " : '') +
    ((aux_price && aux_price != 0) ? "/#{aux_price}" : '') +
    "##{local_id}/#{perm_id} from #{client_id}" +
    ( ? "/#{}" : '') +
    (commission ? " fee #{commission}" : '') + ">"
end

#to_sObject

human



374
375
376
377
378
379
# File 'lib/models/ib/order.rb', line 374

def to_s #human
  "<Order:" + instance_variables.map do |key|
    value = instance_variable_get(key)
    " #{key}=#{value}" unless value.nil? || value == '' || value == 0
  end.compact.join(',') + " >"
end