To use the features described, activate process-orders plugin
 
require 'ib-api' 
ib =  IB::Connection.new
ib.activate_plugin :process_orders 

The :process_orders plugin works on the account-based-ordermanagement state of IB::Connection. It provides useful automations for the order-process.

Workflow

ib =  IB::Connection.new                      # initialize with default parameter 
ib.activate_plugin :process_order, :order_prototypes, :symbols, :auto_adjust
u =  ib.clients.first
c =  IB::Symbols::Stocks.msft  # Microsoft
o =  IB::Limit.order size: 100, price: 500 

preview

z= u.preview order: o, contract: c
 => <Order: LMT GTC buy 100.0  @ 500.0 PreSubmitted  ... >
z.order_state.forcast
 => 
   { :init_margin=>0.5916xxxex, 
     :maint_margin=>0.503xxxex,
     :equity_with_loan=>0.70xxxex,
     :commission=>nil,
     :commission_currency=>"USD",
     :warning=>"" } 

place

u.place order: o, contract: c
o.to_human
  => "<Order: LMT GTC buy 100.0  @ 300 New /#40/ from 2000/DU4035275>"

or

 u.preview( order: o, contract: c ) &.place

or

 u.preview( order: o, contract: c ) &.check_margin(0.25) &.place

modify

o.limit_price = 350
u.modify order: o
o.to_human
  => "<Order: LMT GTC buy 100.0  @ 350 New /#41/ from 2000/DU4035275>"

cancel

u.cancel order: o
u.orders.to_human
 =>
 ["<Order: LMT GTC buy 100.0  @ 350.0 Submitted #41/1699720638 from 2000/DU4035275>",
  "<Order: LMT GTC buy 100.0  @ 375.0 Cancelled #42/1699720639 from 2000/DU4035275>"]

Notice: The order-object is updated in the process. It’s OK to use the same order-object for each step. Then the history is kept in OrderState-Objects. But never use the same order object for different users or different contracts.

Tags: