www.gusucode.com > trading工具箱matlab源码程序 > trading/trading/@ibtws/accounts.m

    function d = accounts(c,acctstr,callback)
%ACCOUNTS Interactive Brokers account data.
%   D = ACCOUNTS(C,ACCTSTR) requests Interactive Brokers account data given the 
%   connection handle, C, and account code, ACCTSTR.  The user can specify 
%   an event handler, CALLBACK.  If no event handler is specified, the data 
%   will be returned as the first output argument.
%
%   For example,
% 
%   d = ib.accounts('DU335877')
%
%   returns
%
%   d = 
% 
%                          AccountCode: 'DU335877'
%                         AccountReady: 'true'
%                          AccountType: 'LLC'
%                          AccruedCash: '0'
%                        AccruedCash_C: '0.00'
%                        AccruedCash_S: '0.00'
%                      AccruedDividend: '0.00'
%                    AccruedDividend_C: '0.00'
%                    AccruedDividend_S: '0.00'
%                       AvailableFunds: '796346.36'
%                     AvailableFunds_C: '0.00'
%                     AvailableFunds_S: '796346.36'
%                             Billable: '0.00'
%                           Billable_C: '0.00'
%                           Billable_S: '0.00'
%                          BuyingPower: '3159754.50'
%                          CashBalance: '19335'
%                   CorporateBondValue: '0'
%                             Currency: 'USD'
%                              Cushion: '0.75458'
%                   DayTradesRemaining: '-1'
%                DayTradesRemainingT_1: '-1'
%                DayTradesRemainingT_2: '-1'
%                DayTradesRemainingT_3: '-1'
%                DayTradesRemainingT_4: '-1'
%                  EquityWithLoanValue: '1055350.27'
%                EquityWithLoanValue_C: '0.00'
%                EquityWithLoanValue_S: '1055350.27'
%                      ExcessLiquidity: '796346.36'
%                    ExcessLiquidity_C: '0.00'
%                    ExcessLiquidity_S: '796346.36'
%                         ExchangeRate: '1.00'
%                   FullAvailableFunds: '796346.36'
%                 FullAvailableFunds_C: '0.00'
%                 FullAvailableFunds_S: '796346.36'
%                  FullExcessLiquidity: '796346.36'
%                FullExcessLiquidity_C: '0.00'
%                FullExcessLiquidity_S: '796346.36'
%                    FullInitMarginReq: '259003.92'
%                  FullInitMarginReq_C: '0.00'
%                  FullInitMarginReq_S: '259003.92'
%                   FullMaintMarginReq: '259003.92'
%                 FullMaintMarginReq_C: '0.00'
%                 FullMaintMarginReq_S: '259003.92'
%                            FundValue: '0'
%                    FutureOptionValue: '0'
%                           FuturesPNL: '0'
%                        FxCashBalance: '0'
%                   GrossPositionValue: '1036015.66'
%                 GrossPositionValue_S: '1036015.66'
%                   IndianStockHaircut: '0.00'
%                 IndianStockHaircut_C: '0.00'
%                 IndianStockHaircut_S: '0.00'
%                        InitMarginReq: '259003.92'
%                      InitMarginReq_C: '0.00'
%                      InitMarginReq_S: '259003.92'
%                           Leverage_S: '0.98'
%              LookAheadAvailableFunds: '796346.36'
%            LookAheadAvailableFunds_C: '0.00'
%            LookAheadAvailableFunds_S: '796346.36'
%             LookAheadExcessLiquidity: '796346.36'
%           LookAheadExcessLiquidity_C: '0.00'
%           LookAheadExcessLiquidity_S: '796346.36'
%               LookAheadInitMarginReq: '259003.92'
%             LookAheadInitMarginReq_C: '0.00'
%             LookAheadInitMarginReq_S: '259003.92'
%              LookAheadMaintMarginReq: '259003.92'
%            LookAheadMaintMarginReq_C: '0.00'
%            LookAheadMaintMarginReq_S: '259003.92'
%                  LookAheadNextChange: '0'
%                       MaintMarginReq: '259003.92'
%                     MaintMarginReq_C: '0.00'
%                     MaintMarginReq_S: '259003.92'
%                 MoneyMarketFundValue: '0'
%                      MutualFundValue: '0'
%                          NetDividend: '0'
%                       NetLiquidation: '1055350.27'
%                     NetLiquidation_C: '0.00'
%                     NetLiquidation_S: '1055350.27'
%             NetLiquidationByCurrency: '1055350'
%                    OptionMarketValue: '0'
%                        PASharesValue: '0.00'
%                      PASharesValue_C: '0.00'
%                      PASharesValue_S: '0.00'
%                 PostExpirationExcess: '0.00'
%               PostExpirationExcess_C: '0.00'
%               PostExpirationExcess_S: '0.00'
%                 PostExpirationMargin: '0.00'
%               PostExpirationMargin_C: '0.00'
%               PostExpirationMargin_S: '0.00'
%       PreviousDayEquityWithLoanValue: '1048942.54'
%     PreviousDayEquityWithLoanValue_S: '1048942.54'
%                          RealizedPnL: '0'
%                           RegTEquity: '1055350.27'
%                         RegTEquity_S: '1055350.27'
%                           RegTMargin: '518007.83'
%                         RegTMargin_S: '518007.83'
%                                  SMA: '538646.26'
%                                SMA_S: '538646.26'
%                       SegmentTitle_C: 'US Commodities'
%                       SegmentTitle_S: 'US Securities'
%                     StockMarketValue: '1036016'
%                           TBillValue: '0'
%                           TBondValue: '0'
%                     TotalCashBalance: '19335'
%                       TotalCashValue: '19334.61'
%                     TotalCashValue_C: '0.00'
%                     TotalCashValue_S: '19334.61'
%                        TradingType_S: 'STKNOPT'
%                        UnrealizedPnL: '-26664'
%                         WarrantValue: '0'
%                      WhatIfPMEnabled: 'true'
%
%
%   ib.accounts('DU335877',@ibExampleEventHandler)
%
%   displays
%
%   Columns 1 through 7
% 
%     [1x1 COM.TWS_TwsCtrl_1]    [7]    'AccountCode'    '335877'    ''    '335877'    [1x1 struct]
% 
%   Column 8
% 
%     'updateAccountValue'
%
%   .
%   .
%   .
%
%   See also IBTWS, CLOSE, CREATEORDER, HISTORY, TIMESERIES.

%   Copyright 2016 The MathWorks, Inc. 
   
% Default account string is empty
if nargin < 2
  acctstr = '';
end

% Define event handlers for error and tick events
eventNames = {'accountDownloadEnd','updateAccountValue'};
if nargin < 3
  for i = 1:length(eventNames)
    c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInAccountDataEventHandler(varargin{:},c)})
  end
elseif ~isempty(callback)
  for i = 1:length(eventNames)
    c.Handle.registerevent({eventNames{i},callback})
  end
end

% IB market data request API call  
c.DataRequest = true;
c.Handle.reqAccountUpdates(true,acctstr)

% If user defined event handler, do not loop on DataRequest property
if nargin > 2
  if nargout == 1
    d = [];
  end
  return
end

while c.DataRequest
  drawnow
end

% Return data to method workspace and convert dates to date numbers
d = evalin('base','ibBuiltInAccountData');

% Clear temporary variables
evalin('base','clear ibBuiltInAccountData');

c.Handle.reqAccountUpdates(false,acctstr)

function ibBuiltInAccountDataEventHandler(varargin)
%IBBUILTINACCOUNTDATAEVENTHANDLER Interactive Brokers' Trader Workstation built in account data event handler.

persistent ibBuiltInAccountData

% Trap event type
switch varargin{end-1}
  
  case {'accountDownloadEnd'}
    
    % Return data to base workspace
    assignin('base','ibBuiltInAccountData',ibBuiltInAccountData);
    clear ibBuiltInAccountData
    evtListeners = varargin{1}.eventlisteners;
    i = strcmp(evtListeners(:,1),'updateAccountValue');
    varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
    i = strcmp(evtListeners(:,1),'accountDownloadEnd');
    varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
    varargin{end}.DataRequest = false; 
    
  case {'updateAccountValue'}
    
    sFieldName = varargin{3};
    sFieldName(sFieldName == '-' | sFieldName == '+') = '_';
    ibBuiltInAccountData.(sFieldName) = varargin{4};
    
    % Return data to base workspace
    assignin('base','ibBuiltInAccountData',ibBuiltInAccountData);
    
end