www.gusucode.com > trading工具箱matlab源码程序 > trading/trading/@ibtws/realtime.m
function tickerID = realtime(c,s,f,callback) %REALTIME Interactive Brokers realtime data. % TICKERID = REALTIME(C,S,F,CALLBACK) asynchronously requests Interactive % Brokers realtime data given the connection handle, C, contract, S, and % field, F. TICKERID is the market request id used to track and cancel % the data request. The user can specify an event handler, CALLBACK. % If no event handler is specified, the data will be returned in the variable % ibBuiltInRealtimeData in the base workspace and be updated in realtime. % % For example, % % ibContract = ib.Handle.createContract; % ibContract.symbol = 'XYZ'; % ibContract.secType = 'STK'; % ibContract.exchange = 'SMART'; % ibContract.currency = 'USD'; % d = ib.realtime(ibContract,'233') % % To cancel the market data request % % ib.Handle.cancelMktData(d) % % See also IBTWS, CLOSE, CREATEORDER, HISTORY, TIMESERIES. % Copyright 2015 The MathWorks, Inc. % Determine number of contracts and create ticker ids numContracts = length(s); if numContracts == 1 && ~iscell(s) s = {s}; end tickerID = randperm(10000,numContracts); %Convert field list to cell array and then to comma delimited list if ischar(f) f = cellstr(f); end fldList = f{1}; for i = 2:length(f) fldList = [fldList ',' f{i}]; %#ok end % Define event handlers for tick events eventNames = {'tickSize','tickPrice','tickSnapshotEnd','tickOptionComputation','tickGeneric','tickString','tickEFP','marketDataType'}; if nargin < 4 for i = 1:length(eventNames) c.Handle.registerevent({eventNames{i},@(varargin)ibBuiltInRealtimeDataEventHandler(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; for i = 1:numContracts try c.Handle.reqMktDataEx(tickerID(i),s{i},fldList,0,c.Handle.createTagValueList) catch %Version pre 9.7 API call c.Handle.reqMktDataEx(tickerID(i),s{i},fldList,0) end end function ibBuiltInRealtimeDataEventHandler(varargin) %IBBUILTINREALTIMEDATAEVENTHANDLER Interactive Brokers' Trader Workstation built in realtime data event handler. persistent ibBuiltInRealtimeData % Trap event type switch varargin{end-1} case {'tickGeneric'} disp('3') case 'tickSize' ibBuiltInRealtimeData.id = varargin{6}.id; switch varargin{6}.tickType case 0 % BID SIZE ibBuiltInRealtimeData.BID_SIZE = varargin{6}.size; case 3 % ASK SIZE ibBuiltInRealtimeData.ASK_SIZE = varargin{6}.size; case 5 % LAST SIZE ibBuiltInRealtimeData.LAST_SIZE = varargin{6}.size; case 8 % VOLUME ibBuiltInRealtimeData.VOLUME = varargin{6}.size; end % Return data to base workspace assignin('base','ibBuiltInRealtimeData',ibBuiltInRealtimeData); case 'tickPrice' ibBuiltInRealtimeData.id = varargin{7}.id; switch varargin{7}.tickType case 1 % BID PRICE ibBuiltInRealtimeData.BID_PRICE = varargin{7}.price; case 2 % ASK PRICE ibBuiltInRealtimeData.ASK_PRICE = varargin{7}.price; case 4 % LAST PRICE ibBuiltInRealtimeData.LAST_PRICE = varargin{7}.price; end % Return data to base workspace assignin('base','ibBuiltInRealtimeData',ibBuiltInRealtimeData); end