To use the features described, include the ib-extensions Gem in the Gemfile and use
require 'ib-api' require 'ib/gateway'in your scripts.
The current status of the Account is available through the :RequestAccountSummary
-Message.
The TWS responds with :AccountSummary
-Messages.
> ib = IB::Connection.current
> ags = %w( NetLiquidation InitMarginReq DayTradesRemaining )
=> ["NetLiquidation", "InitMarginReq", "DayTradesRemaining"]
> ib.subscribe(:Alert, :AccountSummary){ |msg| puts msg.to_human } => 135899964
> request_id = C.send_message :RequestAccountSummary, tags: ags.join(',') => 5775
<AccountValue: DF167347, <InitMarginReq=0.00 EUR>
<AccountValue: DF167347, <NetLiquidation=0.00 EUR>
<AccountValue: DU167348, <DayTradesRemaining=-1 USD>
<AccountValue: DU167348, <InitMarginReq=49838.14 EUR>
<AccountValue: DU167348, <NetLiquidation=926441.24 EUR>
<AccountValue: DU167349, <DayTradesRemaining=-1 USD>
<AccountValue: DU167349, <InitMarginReq=90168.53 EUR>
<AccountValue: DU167349, <NetLiquidation=958049.83 EUR>
> ib.send_message :CancelAccountSummary, id: request_id => true
If the IB::Gateway
is used, the method :get_account_data
requests the data from the TWS and
assigns :AccountValues
and :PortfolioValues
to the IB::Account
.
In the Gateway-Console (bin/gateway.rb)
> u= G.clients.first
> G.get_account_data u
11:13:52 DU167348 => Count of AccountValues: 243 <--- Logger output
> u.account_values.size => 234
> u.account_values.keys
=> [:AccountType, :AccountCode, :AccountReady, :Cushion, :DayTradesRemaining,
:"DayTradesRemainingT+1", :"DayTradesRemainingT+2", :"DayTradesRemainingT+3",
:"DayTradesRemainingT+4", :"Leverage-S", :LookAheadNextChange,
:NLVAndMarginInReview, :"SegmentTitle-C", :"SegmentTitle-S", :"TradingType-S",
:WhatIfPMEnabled, :AccruedCash, :"AccruedCash-C", :"AccruedCash-S",
:AccruedDividend, :"AccruedDividend-C", :"AccruedDividend-S", :AvailableFunds,
:"AvailableFunds-C", :"AvailableFunds-S", :Billable, :"Billable-C",
:"Billable-S", :BuyingPower, :EquityWithLoanValue, :"EquityWithLoanValue-C",
:"EquityWithLoanValue-S", :ExcessLiquidity, :"ExcessLiquidity-C",
:"ExcessLiquidity-S", :FullAvailableFunds, :"FullAvailableFunds-C",
:"FullAvailableFunds-S", :FullExcessLiquidity, :"FullExcessLiquidity-C",
:"FullExcessLiquidity-S", :FullInitMarginReq, :"FullInitMarginReq-C",
(...)
:StockMarketValue, :OptionMarketValue, :FutureOptionValue, :FuturesPNL,
:NetLiquidationByCurrency, :UnrealizedPnL, :RealizedPnL, :ExchangeRate,
:FundValue, :NetDividend, :MutualFundValue, :MoneyMarketFundValue,
:CorporateBondValue, :TBondValue, :TBillValue, :WarrantValue, :FxCashBalance,
:AccountOrGroup, :RealCurrency, :IssuerOptionValue]
To access specific entries, :account_data_scan
is available. It takes a regular expression as argument:
> u.account_data_scan( /MaintMarginReq$/ ).to_human
=> ["<FullMaintMarginReq=44052.56 EUR>", "<LookAheadMaintMarginReq=44052.56 EUR>", "<MaintMarginReq=44052.56 EUR>"]