To use the features described, activate the account-management plugin
require 'ib-api' ib = IB::Connection.new ib.activate_plugin 'account-management' ib.activate_managed_accounts! ib.get_account_data
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"]
> sub = ib.subscribe(:Alert, :AccountSummary){ |msg| puts msg.to_human }
> request_id = C.send_message :RequestAccountSummary, tags: ags.join(',')
<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
> ib.unsubscribe sub
If the managed-accounts
-plugin is used, the method get_account_data
requests the TWS and
assigns :AccountValues
and :PortfolioValues
to IB::Account
.
ib = Connection.new
ib.activate_plugin 'managed-accounts'
#--> W: Workflow:: -> gateway_mode # transition to gateway-mode
ib.initialize_managed_accounts!
#--> W: Workflow:: gateway_mode -> ready
#--> W: Workflow:: ready -> account_based_operations
ib.get_account_data # populates ib.clients
# or
u= ib.clients.first
ib.get_account_data u # update account specific data
Then AccountValues, Contracts and PortfolioValues are available through
puts u.portfolio_values.as_table
┌────────────┬──────────────────────────┬──────┬─────────┬──────────┬───────────┬────────────┬──────────┐
│ │ │ pos │ entry │ market │ value │ unrealized │ realized │
╞════════════╪══════════════════════════╪══════╪═════════╪══════════╪═══════════╪════════════╪══════════╡
│ DU4035276 │ Future: DAX 20240920 EUR │ -1 │ 18399.6 │ 18729.68 │ -18729.68 │ -330.08 │ │
│ DU4035276 │ Stock: DBK EUR IBIS │ 800 │ 10.511 │ 15.296 │ 12236.8 │ 3828.0 │ │
│ DU4035276 │ Stock: F USD NYSE │ 500 │ 14.204 │ 13.45 │ 6725.0 │ -377.05 │ │
│ DU4035276 │ Stock: IFX EUR IBIS │ 400 │ 32.361 │ 35.265 │ 14106.0 │ 1161.6 │ │
│ DU4035276 │ Stock: LNZ EUR VSE │ 228 │ 45.949 │ 33.1 │ 7546.8 │ -2929.54 │ │
│ DU4035276 │ Stock: TKA EUR IBIS │ 500 │ 8.871 │ 4.037 │ 2018.5 │ -2417.0 │ │
│ DU4035276 │ Stock: TLT USD NASDAQ │ -200 │ 90.772 │ 93.51 │ -18702.0 │ -547.56 │ │
│ DU4035276 │ Stock: TUI1 EUR IBIS │ 500 │ 13.051 │ 6.738 │ 3369.0 │ -3156.4 │ │
└────────────┴──────────────────────────┴──────┴─────────┴──────────┴───────────┴────────────┴──────────┘
puts u.contracts.to_human
<Future: DAX 20240920 EUR>
<Stock: DBK EUR IBIS>
<Stock: F USD NYSE>
<Stock: IFX EUR IBIS>
<Stock: LNZ EUR VSE>
<Stock: TKA EUR IBIS>
<Stock: TLT USD NASDAQ>
<Stock: TUI1 EUR IBIS>
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,
(...)
: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:
puts u.account_data_scan( /MaintMarginReq$/ ).map &:to_human
=> <FullMaintMarginReq=44052.56 EUR>
<LookAheadMaintMarginReq=44052.56 EUR>
<MaintMarginReq=44052.56 EUR>