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

    function d = orders(c,clientonly,callback)
%ORDERS Interactive Brokers open order data.
%   D = ORDERS(C,CLIENTONLY,CALLBACK) returns Interactive Brokers open order data
%   given the connection handle, C.   CLIENTONLY specifies if only data from
%   orders placed from this client is returned or if order data from all
%   clients is returned.    For client data only, CLIENTONLY is input as
%   true.   For all data, CLIENTONLY is input as false.  The user can 
%   specify a custom event handler, CALLBACK, otherwise a default event
%   handler is used.
%
%   For example, 
%
%   d = ib.orders
%
%   returns
%
%   d = 
% 
%   1x2 struct array with fields:
% 
%     Type
%     EventID
%     orderId
%     contract
%     order
%     orderState
%
%   ib.orders(true,@ibExampleEventHandler)
%
%   displays
%   
%   Columns 1 through 4
% 
%     [1x1 COM.TWS_TwsCtrl_1]    [101]    [34324126]    [1x1 Interface.Tws_ActiveX_Control_module.IContract]
% 
%   Columns 5 through 6
% 
%     [1x1 Interface.Tws_ActiveX_Control_module.IOrder]    [1x1 Interface.Tws_ActiveX_Control_module.IOrderState]
% 
%   Columns 7 through 8
% 
%     [1x1 struct]    'openOrderEx'
% 
%     [1x1 COM.TWS_TwsCtrl_1]    [36]    [1x1 struct]    'openOrderEnd'
%
%   See also IBTWS, CLOSE, CREATEORDER, GETDATA,HISTORY, TIMESERIES.

%   Copyright 2015 The MathWorks, Inc. 
 
% Request only client orders by default
if nargin < 2 || isempty(clientonly)
  clientonly = true;
end

% Define event handlers for error and tick events
eventNames = {'openOrderEnd','openOrderEx'};
if nargin < 3
  for i = 1:length(eventNames)
    c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInOrderDataEventHandler(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;
if clientonly
  c.Handle.reqOpenOrders;
else
  c.Handle.reqAllOpenOrders;
end

% 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
try
  d = evalin('base','ibBuiltInOpenOrderData');
catch
  d = evalin('base','ibBuiltInErrMsg');
end

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

function ibBuiltInOrderDataEventHandler(varargin)
%IBBUILTINORDERDATAEVENTHANDLER Interactive Brokers' Trader Workstation built in order data event handler.

persistent ibBuiltInOpenOrderData orderCounter

if isempty(orderCounter)
  orderCounter = 1;
end

% Trap event type
switch varargin{end-1}
  
  case {'openOrderEnd','orderStatus'}
    
    ibBuiltInOpenOrderData(orderCounter).Type = varargin{3}.Type;
    ibBuiltInOpenOrderData(orderCounter).EventID = varargin{3}.EventID;
    
    % Return data to base workspace
    assignin('base','ibBuiltInOpenOrderData',ibBuiltInOpenOrderData);
    clear ibBuiltInOpenOrderData orderCounter
    evtListeners = varargin{1}.eventlisteners;
    i = strcmp(evtListeners(:,1),'openOrderEx');
    varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
    i = strcmp(evtListeners(:,1),'openOrderEnd');
    varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
    varargin{end}.DataRequest = false; %#ok
    
  case {'openOrderEx'}
    
    ibBuiltInOpenOrderData(orderCounter).Type = varargin{7}.Type;
    ibBuiltInOpenOrderData(orderCounter).EventID = varargin{7}.EventID;
    ibBuiltInOpenOrderData(orderCounter).orderId = varargin{7}.orderId;
    ibBuiltInOpenOrderData(orderCounter).contract = get(varargin{7}.contract);
    ibBuiltInOpenOrderData(orderCounter).order = get(varargin{7}.order);
    ibBuiltInOpenOrderData(orderCounter).orderState = get(varargin{7}.orderState);
    orderCounter = orderCounter + 1;
    
end