Place by sending a Message
To place an order, basically a :PlaceOrder
- Message has to be transmitted
ib = IB::Connection.new
ib.activate_plugin :order_prototypes
ib.try_connection!
ib.send_message :PlaceOrder, contract: IB::Symbols::Stocks.wfc ,
order: Limit.order( size: 100, price: 22 ),
local_id: ib.next_local_id
The TWS responds 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 = IB::Connection.new
ib.activate_plugin :order_prototypes, :symbols
ib.received = true
ib.try_connection!
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
ib.place_order the_order, the_contract
# adjust the order and transmit the modification
the_order.limit_price = 51
ib.modify_order the_order, the_contract
ib.cancel_order the_order.local_id
The response is stored in ib.received
.
Preview and Place
To preview the impact of an order, what_if=true
is added to the :PlaceOrder
- Message
ib = IB::Connection.new
ib.activate_plugin :order_prototypes, :symbols
ib.received = true
ib.try_connection!
ib.send_message :PlaceOrder, contract: IB::Symbols::Futures.mini_dax ,
order: Adaptive.order( size: 1, price: 18000 ,
what_if: true, account: 'DU4035279'),
local_id: ib.next_local_id
ib.wait_for :OpenOrder
ib.received[:OpenOrder].to_human
=> ["<OpenOrder: <Future: DAX 20240920 EUR>
<Order: LMT GTC buy 1.0 @ 18000.0 PreSubmitted #30/1699720617 from 2000/DU4035279 Adaptive fee: 0.8>>"]
ib.received[:OpenOrder].first.order.init_margin.to_f
=> 8146.610000000001
ib.received[:OpenOrder].first.order.equity_with_loan.to_f
=> -0.7999999999301508
ib.send_message :PlaceOrder, contract: IB::Symbols::Futures.mini_dax,
order: Adaptive.order( action: :buy, size: 1, price: 18043 ,
account: 'DU4035279'),
local_id: ib.next_local_id
More details on reviewing orders: preview