% IQML_MktDepth - sample Market-Depth usage function function IQML_MktDepth(symbol) % Initialize data numRows = 10; depthData = cell(numRows,8); lastUpdateTime = -1; GUI_refresh_period = 0.5 * 1/24/60/60; % =0.5 secs % Prepare the GUI hFig = figure('Name','IQML market-depth example', ... 'NumberTitle','off','CloseReq',@figClosedCallback,... 'Menubar','none', 'Toolbar','none', ... 'Resize','off', 'Pos',[100,200,700,260]); color = get(hFig,'Color'); headers = {'Ask valid','Ask time','Ask size','Ask price', ... 'Bid price','Bid size','Bid time','Bid valid'}; formats = {'logical','char','numeric','long', ... 'long','numeric','char','logical'}; hTable = uitable('Parent',hFig, 'Pos',[10,40,675,203], ... 'Data',depthData, 'ColumnName',headers, ... 'ColumnFormat',formats, 'ColumnWidth',{60,100,80,80,80,80,100,60}); hButton = uicontrol('Parent',hFig, 'Pos',[50,10,60,20], ... 'String','Start', 'Callback',@buttonCallback); hLabel1 = uicontrol('Parent',hFig, 'Pos',[120,10,100,17], ... 'Style','text', 'String','Last updated:', ... 'Horizontal','right', 'Background',color); hLabelTime = uicontrol('Parent',hFig, 'Pos',[225,10,100,17], ... 'Style','text', 'String','(not yet)', ... 'Horizontal','left', 'Background',color); % Send the market-depth request to IQFeed using IQML contractParams = {'symbol',symbol}; % '@ES#' callbacks = struct('Market_depth',@mktDepthCallbackFcn); IQML('marketdepth', contractParams{:}, 'processFunc',callbacks); % Figure close callback function - stop the market-depth streaming function figClosedCallback(hFig, ~) % Delete the figure window and stop any pending data streaming delete(hFig); IQML('marketdepth', contractParams{:}, 'numofevents',0); end % figClosedCallback % Start/stop button callback function function buttonCallback(hButton, ~) currentString = get(hButton,'String'); if strcmp(currentString,'Start') set(hButton,'String','Stop'); else set(hButton,'String','Start'); end end % buttonCallback % Callback functions to handle IQFeed Market Depth update events function mktDepthCallbackFcn(~, eventData) % Ensure that it's the correct MktDepth event allMsgParts = strsplit(eventData.MessageString,','); allMsgParts(strcmpi(allMsgParts,'T')) = {true}; allMsgParts(strcmpi(allMsgParts,'F')) = {false}; if strcmp(eventData.MessagePort,'Level2') && strcmp(allMsgParts{2},symbol) % These are the field names of the IQFeed messages inputParams = {'Intro','Symbol','MMID',... 'Bid','Ask','BidSize','AskSize',... 'BidTime','Date','ConditionCode','AskTime',... 'BidInfoValid','AskInfoValid','EndOfMsgGroup'}; % Get the updated data row % Note: Java indices start at 0, Matlab starts at 1 mmid = allMsgParts{strcmpi(inputParams,'MMID')}; row = sscanf(mmid,'%*c%*c%d'); % Get the size & price data fields from the event's data bidValid = allMsgParts{strcmpi(inputParams,'BidInfoValid')}; askValid = allMsgParts{strcmpi(inputParams,'AskInfoValid')}; bidTime = allMsgParts{strcmpi(inputParams,'BidTime')}; askTime = allMsgParts{strcmpi(inputParams,'AskTime')}; bidSize = allMsgParts{strcmpi(inputParams,'BidSize')}; askSize = allMsgParts{strcmpi(inputParams,'AskSize')}; bidPrice = allMsgParts{strcmpi(inputParams,'Bid')}; askPrice = allMsgParts{strcmpi(inputParams,'Ask')}; thisRowsData = {askValid,askTime,askSize,askPrice,... bidPrice,bidSize,bidTime,bidValid}; depthData(row,:) = thisRowsData; % Update the GUI if more than 0.5 secs have passed and % the button was not pressed if ~isvalid(hButton), return, end isStopped = strcmp(get(hButton,'String'),'Start'); if now - lastUpdateTime > GUI_refresh_period && ~isStopped set(hTable,'Data',depthData); set(hLabelTime,'String',datestr(now,'HH:MM:SS')); lastUpdateTime = now; end end end % mktDepthCallbackFcn end % IQML_MktDepth