class IB::Order

Public Instance Methods

==(other) click to toggle source

Order comparison

Calls superclass method IB::BaseProperties#==
# 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_attributes() click to toggle source
Calls superclass method IB::BaseProperties#default_attributes
# 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) click to toggle source

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

# 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_state() click to toggle source
# File lib/models/ib/order.rb, line 239
def order_state
  order_states.last
end
order_state=(state) click to toggle source
# 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) click to toggle source

Placement

# 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.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_algo() click to toggle source
# 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_human() click to toggle source
# 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} " +
    (limit_price ? "#{limit_price} " : '') + "#{status} " +
    ((aux_price && aux_price != 0) ? "/#{aux_price}" : '') +
    "##{local_id}/#{perm_id} from #{client_id}" +
    (account ? "/#{account}" : '') +
    (commission ? " fee #{commission}" : '') + ">"
end
to_s() click to toggle source
# 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