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' 
require 'ib/gateway' 
in 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}
  ==> Hash: { :init_margin , :maint_margin, :equity_with_loan, 
              :commission, :commission_currency, :warning  }

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. Parts of the Order-State-Response are returned.

Use the TWS to validate and inspect Complex Orders

A neat feature of the TWS-Platform is the appearance of whatIf-Order-Requests in the API-Section of the Trading-Desk. After firing an IB::Account.preview(order,contract)-Request, one can comfortably inspect any part with TWS-Tools.

Margin Impact

Lets estimate the margin-impact of a limit-order for a S&P 500 Mini-Future. For convenience we use the IB::Gateway variant.

> G =  IB::Gateway.current
> the_order =  Limit.order action: :buy, size: 1, price: 3500,
                           contract: IB::Symbols::Futures.es.verify.first
> G.clients.first.preview order: the_order 
 => {:init_margin=>0.23887e5, :maint_margin=>0.2079412e5, 
     :equity_with_loan=>0.119007517e7, :commission=>0.21e1, 
		:commission_currency=>"USD", :warning=>""} 
> G.active_accounts[1].place order: the_order   
  => 123   # local_id

The TWS offers a comparison of current and projected values. This can be achieved through

> z= G.clients.first.preview order: Limit.order( total_quantity: 1, price: 3500, action: :sell, contract: Symbols::Futures.es.verify.first)
> d={}
> G.clients.first.account_data_scan( '^Equity' ).key 
  => [:EquityWithLoanValue, :"EquityWithLoanValue-C", :"EquityWithLoanValue-S"] 
> e= G.clients.first.account_data_scan( '^Equity' ).value
   => ["1190427.52", "99960.00", "1090467.52"]    #  compare the first value with z
> f = G.clients.first.account_data_scan( '^FullMaintMargin' ).value
 => ["7331.49", "0.00", "7331.49"]      # complete, commodity account, securities account
> g = G.clients.first.account_data_scan( '^FullInitMargin' ).value
 => ["9092.67", "0.00", "9092.67"]      # complete, commodity account, securities account


> d[:equity_with_loan]=[e.first, e.first.to_f - z[:equity_with_loan].to_f , z[:equity_with_loan]]              
> d[:maint_margin ]=   [f.first, f.first.to_f - z[:maint_margin].to_f, z[:maint_margin] ]                  
> d[:init_margin  ]=   [g.first, g.first.to_f - z[:init_margin].to_f , z[:init_margin] ]
> d.each{|x,y| puts "#{x} : \t #{ y.map{|z| z.to_f.round(10.2)}.join(" : ")} "}
 -------------------- current    --- change  ---- projection
 equity_with_loan : 	 1190427.52 : 352.3500000001 : 1190075.17 
 maint_margin     : 	 7331.49 : -13462.63 : 20794.12 
 init_margin      : 	 9092.67 : -14794.33 : 23887.0 



Tags: