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

    function [d,tickerID] = marketdepth(c,s,depth,callback)
%MARKETDEPTH Interactive Brokers market depth data.
%   [D,TICKERID] = MARKETDEPTH(C,S,DEPTH,CALLBACK) asynchronously requests Interactive 
%   Brokers market depth data given the connection handle, C,  and contract, S. 
%   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,
% 
%   ibContract = ib.Handle.createContract;
%   ibContract.symbol = 'XYZ';
%   ibContract.secType = 'STK';
%   ibContract.exchange = 'SMART';
%   ibContract.currency = 'USD';
%   [d,t] = ib.marketdepth(ibContract,10)
%
%   or to specify a user defined event handler
%
%   [~,t] = ib.marketdepth(ibContract,10,@ibExampleEventHandler)
%
%   To cancel the market depth request
%
%   ib.Handle.cancelMktDepth(t)
% 
%   See also IBTWS, CLOSE, CREATEORDER, HISTORY, TIMESERIES.

%   Copyright 2015 The MathWorks, Inc. 
   
% Depth cannot be greater than 10
if depth > 10
  error(message('trading:ibtws:marketDepthLimitError'))
end

% Define event handlers for error and tick events
eventNames = {'updateMktDepth','updateMktDepthL2'};
tickerID = floor(rand*10000);
if nargin < 4
  for i = 1:length(eventNames)
    c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInMarketDepthEventHandler(varargin{:},c,tickerID,depth)})
  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.reqMktDepthEx(tickerID,s,depth)

% If user defined event handler, do not loop on DataRequest property
if nargin > 3
  d = tickerID;
  return
end

while c.DataRequest
  drawnow
end

% Return data to method workspace and convert dates to date numbers
try
  d = evalin('base','ibBuiltInMarketDepthData');
catch
  d = evalin('base','ibBuiltInErrMsg');
end

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


function ibBuiltInMarketDepthEventHandler(varargin)
%IBBUILTINMARKETDEPTHEVENTHANDLER Interactive Brokers' Trader Workstation built in market depth data event handler.

persistent ibBuiltInMarketDepthData buyPosition sellPosition

% Trap event type
switch varargin{end-3}
  
  case {'updateMktDepth','updateMktDepthL2'}
    
    if varargin{9}.side
      buyPosition = varargin{9}.position+1;
      ibBuiltInMarketDepthData.bid(buyPosition,1) = varargin{9}.price;
      ibBuiltInMarketDepthData.bid(buyPosition,2) = varargin{9}.size;
    else
      sellPosition = varargin{9}.position+1;
      ibBuiltInMarketDepthData.offer(sellPosition,1) = varargin{9}.price;
      ibBuiltInMarketDepthData.offer(sellPosition,2) = varargin{9}.size;
    end
    
    % varargin{end} is the depth
    if ~isempty(buyPosition) && ~isempty(sellPosition) && (buyPosition == varargin{end} && sellPosition == varargin{end})
      
      assignin('base','ibBuiltInMarketDepthData',ibBuiltInMarketDepthData)
    
      % Clear persistent variables and event listeners
      clear ibBuiltInMarketDepthData buyPosition sellPosition
      evtListeners = varargin{1}.eventlisteners;
      i = strcmp(evtListeners(:,1),'updateMktDepth');
      varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
      i = strcmp(evtListeners(:,1),'updateMktDepthL2');
      varargin{1}.unregisterevent({evtListeners{i,1} evtListeners{i,2}});
      % varargin{end-1} is id used to cancel request
      varargin{1}.cancelMktDepth(varargin{end-1});
      varargin{end-2}.DataRequest = false; %#ok
      return
      
    end
    
end