Class: IB::Order

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

Instance Method Summary collapse

Methods included from BaseProperties

#as_table, #content_attributes, #set_attribute_defaults, #update_missing

Instance Method Details

#==(other) ⇒ Object

Order comparison



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'lib/models/ib/order.rb', line 478

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 &&
 total_quantity == other.total_quantity &&
 limit_price == other.limit_price  &&
 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

default valus are taken from order.java



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/models/ib/order.rb', line 359

def default_attributes				# default valus are taken from order.java 
									#  public Order() { }
      super.merge(
      :active_start_time => "",		# order.java # 470		# Vers 69
      :active_stop_time => "",		#order.java # 471	# Vers 69
      :algo_strategy => '',
			:algo_id => '' ,								# order.java # 495
			:auction_strategy => :none,
			:conditions => [],
      :continuous_update => 0,
      :designated_location => '', # order.java # 487
      :display_size => 0,
      :discretionary_amount => 0,
			:etrade_only => true,	# stolen from python client
      :exempt_code => -1,
			:ext_operator  => '' ,  # order.java # 499
			:firm_quote_only  => true,  # stolen from python client
			:not_held => false,  # order.java # 494
      :oca_type => :none,
			:order_type => :limit,
      :open_close => :open,	  # order.java # 
			:opt_out_smart_routing => false,
      :origin => :customer,
			:outside_rth => false, # order.java # 472
      :parent_id => 0,
			:random_size => false,	  #oder.java 497			# Vers 76
			:random_price => false,	  # order.java # 498		# Vers 76
			:scale_auto_reset => false,  # order.java # 490
			:scale_random_percent => false, # order.java # 491
			:scale_table => "", # order.java # 492
      :short_sale_slot => :default,
      :solicided =>  false,  # order.java #  496
      :tif => :day,
      :transmit => true,
			:trigger_method => :default,
      :what_if => false,  # order.java # 493
      :leg_prices => [],
      :algo_params => Hash.new, #{},
      :combo_params =>[], #{},
  #      :soft_dollar_tier_params => HashWithIndifferentAccess.new( 
	#				    :name => "",
	#				    :val => "",
	#				    :display_name => ''),
       :order_state => IB::OrderState.new(:status => 'New',
                                   :filled => 0,
                                   :remaining => 0,
                                   :price => 0,
                                   :average_price => 0)
      )  # closing of merge
end

#invariant_attributesObject

expell uncommon attributes



541
542
543
544
# File 'lib/models/ib/order.rb', line 541

def invariant_attributes
  attributes.reject{ |x| [ :designated_location, :display_size, :etrade_only, :exempt_code, :ext_operator, :random_size, :random_price, :firm_quote_only, :scale_auto_reset, :scale_random_percent, :scale_table, :short_sale_slot, :solicided, :created_at, :modified_at ].include? x }

end

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

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



465
466
467
468
469
470
471
472
473
474
475
# File 'lib/models/ib/order.rb', line 465

def modify the_contract=nil, connection=nil, time=Time.now
			error "Unable to modify order; local_id not specified" if local_id.nil?
			self.contract =  the_contract unless the_contract.nil?
			connection ||= IB::Connection.current
  self.modified_at = time
  connection.send_message :PlaceOrder,
    :order => self,
    :contract => contract,
    :local_id => local_id
  local_id
end

#order_stateObject



309
310
311
# File 'lib/models/ib/order.rb', line 309

def order_state
  order_states.last
end

#order_state=(state) ⇒ Object



313
314
315
316
317
318
319
320
# File 'lib/models/ib/order.rb', line 313

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(the_contract = nil, connection = nil) ⇒ Object

The Order is only placed, if local_id is not set

Modifies the Order-Object and returns the assigned local_id



453
454
455
456
457
458
459
460
461
462
# File 'lib/models/ib/order.rb', line 453

def place the_contract=nil, connection=nil
			connection ||= IB::Connection.current
  error "Unable to place order, next_local_id not known" unless connection.next_local_id
			error "local_id present. Order is already placed.  Do you  want to modify?"  unless  local_id.nil?
  self.client_id = connection.client_id
  self.local_id = connection.next_local_id
  connection.next_local_id += 1
  self.placed_at = Time.now
  modify the_contract, connection, self.placed_at
end

#serialize_algoObject



425
426
427
428
429
430
431
432
433
434
# File 'lib/models/ib/order.rb', line 425

def serialize_algo
  if algo_strategy.nil? || algo_strategy.empty?
    [algo_strategy, algo_id]  # just omit the size and content-field
  else
    [algo_strategy,
     algo_params.size,
     algo_params.to_a,
 algo_id ]	    # Vers 71
  end
end

#serialize_conditionsObject

Format of serialisation

count of records for each condition: conditiontype, condition-fields



417
418
419
420
421
422
423
# File 'lib/models/ib/order.rb', line 417

def serialize_conditions
	if conditions.empty?
	0
	else
	[ conditions.count ]	+ conditions.map( &:serialize )+  [ conditions_ignore_rth, conditions_cancel_order]
	end
end

#serialize_misc_optionsObject

def initialize_soft_dollar_tier *fields

self.soft_dollar_tier_params= HashWithIndifferentAccess.new(
name:   fields.pop, val:  fields.pop, display_name:  fields.pop )

end



445
446
447
# File 'lib/models/ib/order.rb', line 445

def serialize_misc_options
  ""		  # Vers. 70  
end

#serialize_rabbitObject



534
535
536
537
538
# File 'lib/models/ib/order.rb', line 534

def serialize_rabbit
	{ 'Contract' => contract.present? ? contract.serialize( :option, :trading_class ): '' ,
 'Order' =>  self,
 'OrderState' => order_state}
end

#table_headerObject



519
520
521
# File 'lib/models/ib/order.rb', line 519

def table_header
  [ 'account','status' ,'', 'Type', 'tif', 'action', 'amount','price' , 'id/fee' ]
end

#table_rowObject



523
524
525
526
527
528
529
530
531
532
# File 'lib/models/ib/order.rb', line 523

def table_row
  [ ,  order_ref.present? ? order_ref.to_s : status,
    contract.to_human[1..-2],
    self[:order_type] ,
    self[:tif],
    action,
    total_quantity,
    (limit_price ? "#{limit_price} " : '') + ((aux_price && aux_price != 0) ? "/#{aux_price}" : '') ,
    commission ? " fee #{commission}" : local_id ]
end

#to_humanObject



508
509
510
511
512
513
514
515
516
# File 'lib/models/ib/order.rb', line 508

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

#to_sObject

human



501
502
503
504
505
506
# File 'lib/models/ib/order.rb', line 501

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