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

    function p = portfolio(c,acctstr,callback)
% PORTFOLIO Obtain current portfolio data from Interactive Brokers.
%   P = PORTFOLIO(C,ACCTSTR,CALLBACK) requests Interactive Brokers current 
%   portfolio data given the connection handle, C, and the 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,
%
%   p = ib.portfolio('DU335877')
%  
%   returns
%
%   p = 
% 
%              Type: {5x1 cell}
%            Source: {5x1 cell}
%           EventID: {5x1 cell}
%          contract: {5x1 cell}
%          position: {5x1 cell}
%       marketPrice: {5x1 cell}
%       marketValue: {5x1 cell}
%       averageCost: {5x1 cell}
%     unrealizedPNL: {5x1 cell}
%       realizedPNL: {5x1 cell}
%       accountName: {5x1 cell}
%
%   ib.portfolio('DU335877',@ibExampleEventHandler)
%
%   displays
%   Columns 1 through 5
% 
%     [1x1 COM.TWS_TwsCtrl_1]    [103]    [1x1 Interface.Tws_ActiveX_Control_module.IContract]    [90]    [7.9950]
% 
%   Columns 6 through 12
% 
%     [719.5500]    [8.0823]    [-7.8600]    [0]    'DU149963'    [1x1 struct]    'updatePortfolioEx'
%
%   See also IBTWS, CLOSE, CREATEORDER, GETDATA, HISTORY, TIMESERIES,
%   MARKETDEPTH, EXECUTIONS.

%   Copyright 2015 The MathWorks, Inc.

% Default account string is empty
if nargin < 2
  acctstr = '';
end

% Define and register event handlers
eventNames = {'updatePortfolioEx','accountDownloadEnd'};
if nargin < 3
  for i = 1:length(eventNames)
    c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInGetPortfolioEventHandler(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
    p = [];
  end
  return
end

while c.DataRequest
  drawnow
end

% Return data to method workspace
try
  p = evalin('base','ibBuiltInPortfolioData');
catch
  p = evalin('base','ibBuiltInErrMsg');
end

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

c.Handle.reqAccountUpdates(false,acctstr)

function ibBuiltInGetPortfolioEventHandler(varargin)

persistent ibBuiltInPortfolioData iPortfolio

% Trap event type
switch varargin{end-1}
  case {'updatePortfolioEx'}
    
    if isempty(iPortfolio)
      iPortfolio = 1;
    end
    
    portfolioFields = fieldnames(varargin{11});
    for i = 1:length(portfolioFields)
      switch portfolioFields{i}
        case 'contract'
          ibBuiltInPortfolioData.(portfolioFields{i}){iPortfolio,1} = get(varargin{11}.(portfolioFields{i}));
        otherwise
          ibBuiltInPortfolioData.(portfolioFields{i}){iPortfolio,1} = varargin{11}.(portfolioFields{i});
      end
    end
    
    iPortfolio = iPortfolio + 1;
    
    assignin('base','ibBuiltInPortfolioData',ibBuiltInPortfolioData);
    
  case {'accountDownloadEnd'}
    
    assignin('base','ibBuiltInPortfolioData',ibBuiltInPortfolioData);
    
    % Clear persistent variables and event listeners
    clear ibBuiltInPortfolioData iPortfolio
    evtListeners = varargin{1}.eventlisteners;
    i = strcmp(evtListeners(:,1),'updatePortfolioEx');
    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; %#ok
end