www.gusucode.com > trading工具箱matlab源码程序 > trading/tradingdemos/emsxOrderBlotter.m

    function [t,subs] = emsxOrderBlotter(b)
%EMSXORDERBLOTTER Bloomberg EMSX example order blotter.
%   [T,SUBS] = EMSXORDERBLOTTER(B) displays a trader's order information.  B is
%   the Bloomberg EMSX connection object. T is the timer object associated
%   with the event handler and SUBS is the Bloomberg order subscription
%   object.
%
%   For example,
%
%   [t,subs] = emsxOrderBlotter(b) 
%
%   opens an order blotter showing the current order information for a
%   trader.  As orders are created and managed, the blotter is updated.
%
%   reqStruct.EMSX_AMOUNT = int32(330);
%   reqStruct.EMSX_ORDER_TYPE = 'MKT';
%   reqStruct.EMSX_BROKER = 'BB';
%   reqStruct.EMSX_TIF = 'DAY';
%   reqStruct.EMSX_HAND_INSTRUCTION = 'ANY';
%   reqStruct.EMSX_SIDE = 'BUY';
%   reqStruct.EMSX_TICKER = 'XYZ';
%   b.createOrderAndRoute(reqStruct,'useDefaultEventHandler',false)
%
%   will update the order blotter with information on the created and
%   routed order using the event hander function processEventToBlotter.
%
%   See also emsx.

%   Copyright 2012 The MathWorks, Inc.

  % Column names for blotter
  colnames = {'SEQUENCE','TICKER','SIDE','TYPE','WORKING','FILLED','TIF',...
    'BROKER','STATUS','HANDLING','AVGPRC','LMTPRC','TRADER','GTD','STOPPRC'};

  % Create gui components for displaying and updating order information
  uTable = uitable('ColumnName',colnames,'Position',[10 35 1127 300],'RowName',[],'Tag','OrderBlotter');
  updateButton = uicontrol('String','Update Orders','Position',[10 10 90 20],'callback',{@updateOrderBlotter,b},'Tag','UpdateOrderButton');
  pos = get(gcf,'Position');
  set(gcf,'Position',[pos(1) pos(2) 1147 340])
  set(updateButton,'Units','Normal');
  set(uTable,'Units','Normal')
  
  % Create order subscription
  [r,subs] = b.orders({'EMSX_SEQUENCE','EMSX_TICKER','EMSX_SIDE',...
                       'EMSX_TYPE','EMSX_WORKING','EMSX_FILLED','EMSX_TIF',...
                       'EMSX_BROKER','EMSX_STATUS','EMSX_HAND_INSTRUCTION',...
                       'EMSX_AVG_PRICE','EMSX_LIMIT_PRICE','EMSX_TRADER',...
                       'EMSX_GTD_DATE','EMSX_STOP_PRICE'});
  
  % Initialize and start event handler                   
  t = timer('TimerFcn',{@processEventToBlotter,b},'Period',1','ExecutionMode','fixedRate');
  start(t)
  
  % Create GUI components for handling closing dialog
  set(gcf,'NumberTitle','off','Name','EMSX Order Blotter','CloseRequestFcn',{@closeBlotter,b,t,subs})
  closeButton = uicontrol('String','Close','Position',[1047 10 90 20],'callback',{@closeBlotter,b,t,subs},'Tag','CloseButton');
  set(closeButton,'Units','Normal')
  
  % Update blotter with existing order information
  if ~isempty(r)
    orderNumbers = r.EMSX_SEQUENCE;
  else
    orderNumbers = [];
  end
  for i = 1:length(orderNumbers)
    b.getOrderInfo(orderNumbers(i),'useDefaultEventHandler',false)
  end

end


function updateOrderBlotter(~,~,b)
%UPDATEORDERBLOTTER Get information for existing orders and update blotter

  uTable = findobj('Tag','OrderBlotter');
  blotterData = get(uTable,'data');
  numOrders = size(blotterData,1);
  for i = 1:numOrders
    b.getOrderInfo(str2double(blotterData{i,1}),'useDefaultEventHandler',false)
  end
  
end

function closeBlotter(~,~,b,t,subs)
%CLOSEBLOTTER Close dialog handler. Stop event handler and cancel subscription.

  stop(t)
  try
    b.Session.unsubscribe(subs)
  catch %#ok
    % Problem with connection, close window anyway
  end
  closereq
  
end

function processEventToBlotter(~,~,b)
%PROCESSEVENTTOBLOTTER Sample Bloomberg EMSX event handler.
%   PROCESSEVENTTOBLOTTER(B) processes the EMSX event queue associated with 
%   Bloomberg connection handle, B.
%
%   T = timer('TimerFcn',{@b.processEventToBlotter},'Period',1,'ExecutionMode','fixedRate')
%   processes the EMSX event queue continually.
  
  % set iteration flag
  iter = true;
  
  %process events and display messages
  while iter
    evt = b.Session.nextEvent(1);
    evtType = char(evt.eventType);
    msgIter = evt.messageIterator;
    switch evtType
      case 'TIMEOUT'
        % timeout event received, exit event loop
        iter = false;
      otherwise
        % event received
        msg = msgIter.next;
        try
          % if error event, show message exit event loop
          ERROR_MESSAGE = char(msg.getElementAsString('ERROR_MESSAGE'));
          errordlg(ERROR_MESSAGE)
          return
        catch  %#ok
          % No error continue
        end
        try
          % update blotter with order information 
          uiBlotter = findobj('Tag','OrderBlotter');
          blotterData = get(uiBlotter,'data');
          if isempty(blotterData)
            blotterData = {};
          end
          % get order id or exit if information not given
          try
            EMSX_SEQUENCE = double(msg.getElementAsInt32('EMSX_SEQUENCE'));
          catch  %#ok
            try
              EMSX_SEQUENCE = -1*double(msg.getElementAsInt64('EMSX_TS_ORDNUM'));
            catch %#ok
              return
            end
          end
          if EMSX_SEQUENCE == 0
            return
          end
          % check for order in existing blotter data
          if isempty(blotterData)
            orderIndex = [];
          else
            orderIndex = find(strcmp(blotterData(:,1),num2str(EMSX_SEQUENCE)));
          end
          % get order information
          EMSX_TICKER = char(msg.getElementAsString('EMSX_TICKER'));
          EMSX_SIDE = char(msg.getElementAsString('EMSX_SIDE'));
          try
            EMSX_TYPE = char(msg.getElementAsString('EMSX_ORDER_TYPE'));
          catch %#ok
            EMSX_TYPE = [];
          end
          EMSX_WORKING = char(msg.getElementAsString('EMSX_WORKING'));
          EMSX_FILLED = num2str(double(msg.getElementAsInt32('EMSX_FILLED')));
          EMSX_TIF = char(msg.getElementAsString('EMSX_TIF'));
          EMSX_BROKER = char(msg.getElementAsString('EMSX_BROKER'));
          try
            EMSX_STATUS = char(msg.getElementAsString('EMSX_STATUS'));
            EMSX_HAND_INSTRUCTION = char(msg.getElementAsString('EMSX_HAND_INSTRUCTION'));
          catch %#ok
            EMSX_STATUS = [];
            EMSX_HAND_INSTRUCTION = [];
          end
          EMSX_AVG_PRICE = num2str(double(msg.getElementAsFloat64('EMSX_AVG_PRICE')));
          EMSX_LIMIT_PRICE = num2str(double(msg.getElementAsFloat64('EMSX_LIMIT_PRICE')));
          EMSX_TRADER = char(msg.getElementAsString('EMSX_TRADER'));
          try 
            EMSX_GTD_DATE = char(msg.getElementAsString('EMSX_GTD_DATE'));
            EMSX_STOP_PRICE = num2str(double(msg.getElementAsFloat64('EMSX_STOP_PRICE')));
          catch %#ok
            EMSX_GTD_DATE = [];
            EMSX_STOP_PRICE = '0';
          end
          % create record for new order or order not shown in blotter
          if isempty(orderIndex)
            blotterData = [blotterData;{num2str(EMSX_SEQUENCE) EMSX_TICKER ...
                           EMSX_SIDE EMSX_TYPE EMSX_WORKING EMSX_FILLED ...
                           EMSX_TIF EMSX_BROKER EMSX_STATUS EMSX_HAND_INSTRUCTION ...
                           EMSX_AVG_PRICE EMSX_LIMIT_PRICE EMSX_TRADER ...
                           EMSX_GTD_DATE EMSX_STOP_PRICE}]; %#ok
          else
            % update existing order record
            if isempty(blotterData{orderIndex,2}) && ~isempty(EMSX_TICKER)
              blotterData{orderIndex,2} = EMSX_TICKER;
            end
            if ~isempty(EMSX_SIDE)
              blotterData{orderIndex,3} = EMSX_SIDE;
            end
            if ~isempty(EMSX_TYPE)
              blotterData{orderIndex,4} = EMSX_TYPE;
            end
            blotterData{orderIndex,5} = EMSX_WORKING;
            blotterData{orderIndex,6} = EMSX_FILLED;
            blotterData{orderIndex,7} = EMSX_TIF;
            blotterData{orderIndex,8} = EMSX_BROKER;
            if ~isempty(EMSX_STATUS)
              blotterData{orderIndex,9} = EMSX_STATUS;
            end
            if ~isempty(EMSX_HAND_INSTRUCTION)
              blotterData{orderIndex,10} = EMSX_HAND_INSTRUCTION;
            end
            blotterData{orderIndex,11} = EMSX_AVG_PRICE;
            blotterData{orderIndex,12} = EMSX_LIMIT_PRICE;
            blotterData{orderIndex,13} = EMSX_TRADER;
            blotterData{orderIndex,14} = EMSX_GTD_DATE;
            blotterData{orderIndex,15} = EMSX_STOP_PRICE;
          end
          set(uiBlotter,'data',blotterData)
          drawnow
        catch %#ok
          disp(msg);
        end
    end
  end
end