require 'ib-api' ib = IB::Connection.new ib.activate_plugin :process_orders, :advanced_accountin your scripts.
IB::Account.preview
The TWS displays the margin-impact of an order via »Order -> Preview«.
IB::Gateway
keeps this methodology.
Account.preview order: {IB::Order} , contract: {IB::Contract}
==> <Order: (...) @ {price} PreSubmitted #/{order-id} from {client-id}/{account-id}>
Behind the scene, the what_if
-Flag on the Limit.order
is set. Then the order is placed the usual way.
The response from the TWS is recognized through the OpenOrder
-Message, which is returned.
Margin Impact
Lets estimate the margin-impact of a limit-order for a S&P 500 Mini-Future.
> ib = IB::Connection.current
> the_order = IB::Limit.order size: 1, price: 5800,
> the_contract = IB::Symbols::Futures.es
> preview = ib.clients.first.preview order: the_order, contract: the_contract
=> <Order: LMT GTC buy 1.0 @ 5800.0 PreSubmitted #/1654228057 from {client-id>}/{account-id}
preview.order_state
=>#<IB::OrderState:0x000075170d1463f8
@attributes=
{:status=>"PreSubmitted",
:init_margin_before=>0.5xxen,
:maint_margin_before=>0.44xxen,
:equity_with_loan_before=>0.70xxen,
:init_margin_change=>0.22574690000000002e5,
:maint_margin_change=>0.13502849999999999e5,
:equity_with_loan_change=>-0.20599999999976717e1,
:init_margin_after=>0.75xxen,
:maint_margin_after=>0.57xxen,
:equity_with_loan_after=>0.70xxen,
Check Margin
To estimate, if an order will met basic risk-requirements, a handy order.check_margin
method is provided.
> ib = IB::Connection.current
> the_order = IB::Limit.order size: 100, price: 10
> ford = IB::Stock.new symbol: 'F'
> ib_clients.first.preview( order: the_order, contract: ford) &.check_margin(0.25) &.place
This example sets up a limit-order to buy Ford-stocks. client.preview
provides a new order-object.
The check_margin
-method analyses the OrderState records and checks it the margin utilization is
below the specified treshold. In this case, the order is only placed, if a minimum of 25 % of unused margin
is left.