To use the features described, include the ib-extensions Gem in the Gemfile and use
require 'ib-api' require 'ib/symbols' require 'ib/order-prototypes'in your scripts.
This page covers order placement using B::Connection.
The approach using IB::Gateway is described in ib-doc/workflow
The approach using IB::Gateway is described in ib-doc/workflow
Place by sending a Message
To place an order, basically a :PlaceOrder
- Message has to be transmitted
> ib = IB::Connection.current
> ib.send_message :PlaceOrder, contract: IB::Symbols::Stocks.wfc ,
order: Limit.order( action: :buy, size: 100, price: 22 ),
local_id: ib.next_local_id
TWS responses with :OpenOrder
and OrderStatus
messages.
The PlaceOrder
message is also used to modify orders.
A new order requires an incremented :local_id
.
By transmitting a :PlaceOrder
-Message with an unchanged :local_id
the order is modified (without warning).
Place with IB::Connection-methods
To avoid unintended order-modifications, explicit calls to :place_order
and :modify_order
are implemented.
IB-Api provides handy IB::Connection#place_order
and IB::Connection#modify_order
methods.
> ib = IB::Connection.current
> the_order = Limit.order action: :buy, size: 100, price: 50
> the_contract = IB::Symbols::Stocks.wfc
# call _place_order_ and get the current _order_id
> order_id = ib.place_order the_order, the_contract
# adjust the order and transmit the modification
> the_order.price = 51
> the_order.local_id = order_id
> ib.modify_order the_order, the_contract
The response is stored in IB::Connection.received
.
Preview and Place
To preview the impact of an order, what_if=true
is added to the :PlaceOrder
- Message
> ib = IB::Connection.current
> ib.send_message :PlaceOrder, contract: IB::Symbols::Stocks.wfc ,
order: Limit.order( action: :buy, size: 100, price: 22 ,
what_if: true, account: 'DU1111222'),
local_id: ib.next_local_id
> ib.received[:OpenOrder].to_human
=> ["<OpenOrder: <Stock: WFC USD NYSE> <Order: LMT GTC buy 100.0 @ 21.0 PreSubmitted #22/1729171652 from 0/DU167349 fee 1.0>>"]
> ib.received[:OpenOrder].first.order.init_margin.to_f => 54164.28
> ib.received[:OpenOrder].first.order.equity_with_loan.to_f => 894806.34
> ib.send_message :PlaceOrder, contract: IB::Symbols::Stocks.wfc ,
order: Limit.order( action: :buy, size: 100, price: 22 ,
what_if: false, account: 'DU1111222'),
local_id: ib.next_local_id
<OrderState: Submitted #24/1729171653 from 2000 filled 0.0/100.0 at 0.0/0.0 why_held > # log entry
> ib.received[:OpenOrder].to_human
=> ["<OpenOrder: <Stock: WFC USD NYSE> <Order: LMT GTC buy 100.0 @ 21.0 PreSubmitted #22/1729171652 from 0/DU167349 fee 1.0>>",
"<OpenOrder: <Stock: WFC USD NYSE> <Order: LMT GTC buy 100.0 @ 21.0 Submitted #24/1729171653 from 2000/DU167349>>"]
More details on reviewing orders: preview