function rtshowbook(varargin)
%RTSHOWBOOK Datafeed Toolbox Reuters realtime order book callback.
%   RTSHOWBOOK(X) demonstrates the Datafeed Toolbox Reuters
%   security realtime data import functionality as an order book.   

%   Copyright 1999-2009 The MathWorks, Inc.

%Get data structure
x = varargin{1};

%Get order book table handle
uibook = getappdata(0,'OrderBookHandle');
bookdata = getappdata(uibook,'OrderData');

%Process ticks
try
  %Check if tick has BID and ASK info
  x.BID;
catch %#ok
  try
    %Otherwise, display last price if found in tick
    fName = getappdata(0,'OrderBookTitle');
    set(get(uibook,'Parent'),'Name',[fName ' - ' num2str(x.TRDVOL_1) ' @ ' num2str(x.TRDPRC_1)])
    return
  catch %#ok
    %Otherwise, wait for next tick
    return
  end
end

%BID
bookdata{5,1} = bookdata{4,1};
bookdata{4,1} = bookdata{3,1};
bookdata{3,1} = bookdata{2,1};
bookdata{2,1} = bookdata{1,1};
bookdata{1,1} = x.BID;

%BIDSIZE
bookdata{5,3} = bookdata{4,3};
bookdata{4,3} = bookdata{3,3};
bookdata{3,3} = bookdata{2,3};
bookdata{2,3} = bookdata{1,3};
bookdata{1,3} = x.BIDSIZE;

%BID_MMID1
bookdata{5,2} = bookdata{4,2};
bookdata{4,2} = bookdata{3,2};
bookdata{3,2} = bookdata{2,2};
bookdata{2,2} = bookdata{1,2};
bookdata{1,2} = x.BID_MMID1;

%ASK
bookdata{5,4} = bookdata{4,4};
bookdata{4,4} = bookdata{3,4};
bookdata{3,4} = bookdata{2,4};
bookdata{2,4} = bookdata{1,4};
bookdata{1,4} = x.ASK;

%ASK_MMID1
bookdata{5,5} = bookdata{4,5};
bookdata{4,5} = bookdata{3,5};
bookdata{3,5} = bookdata{2,5};
bookdata{2,5} = bookdata{1,5};
bookdata{1,5} = x.ASK_MMID1;

%ASKSIZE
bookdata{5,6} = bookdata{4,6};
bookdata{4,6} = bookdata{3,6};
bookdata{3,6} = bookdata{2,6};
bookdata{2,6} = bookdata{1,6};
bookdata{1,6} = x.ASKSIZE;

%Hide data in GUI
set(uibook,'Data',bookdata)
setappdata(uibook,'OrderData',bookdata)

%Update book 
showoffers;

function showoffers()
%SHOWOFFERS Update OrderBook GUI.

%Get existing data and GUI handles
uibook = getappdata(0,'OrderBookHandle');
baraxeshandles = getappdata(0,'BarAxesHandles');
bookdata = getappdata(uibook,'OrderData');

%Update data and GUI
try
    
  %Sort data for display
  [a,i] = sort(cell2mat(bookdata(:,1)));
  [b,j] = sort(cell2mat(bookdata(:,4)));
  data = cell2mat(bookdata(i,[1,3,4,6]));
  data1 = [data(1,1)-.00;data(1,1)+.00;data(1,1)+.00;data(2,1)+.00;data(2,1)+.00;...
      data(3,1)+.00;data(3,1)+.00;data(4,1)+.00;data(4,1)+.00;data(5,1)+.00;data(5,1)+.00];
  data2 = [data(:,2) data(:,2)]';
  data2 = [data2(:);0];
  data(:,3) = sort(data(:,3));
  data3 = [data(1,3)-.00;data(1,3)-.00;data(1,3)+.00;data(1,3)+.00;data(2,3)+.00;data(2,3)+.00;...
      data(3,3)+.00;data(3,3)+.00;data(4,3)+.00;data(4,3)+.00;data(5,3)+.00];
  data4 = [data(:,4) data(:,4)]';
  data4 = [0;data4(:)];
  
  %Plot bid/ask values and sizes
  plot(baraxeshandles(1),data1,data2,'r-',data3,data4,'b-','linewidth',2);
  set(baraxeshandles(1),'xlim',[min(cell2mat(bookdata(i,1)))-.01 max(cell2mat(bookdata(i,4)))+.01])
  drawnow
  
  %Build cumulative bid/ask sizes
  buys = cell2mat(bookdata(i,3));
  buysizes = [sum(buys);sum(buys(2:end));sum(buys(3:end));sum(buys(4:end));buys(end)];
  asks = cell2mat(bookdata(i,6));
  asksizes = [asks(1);sum(asks(1:2));sum(asks(1:3));sum(asks(1:4));sum(asks)];
  area(baraxeshandles(2),cell2mat(bookdata(i,1)),buysizes,'facecolor','red','edgecolor','red','linewidth',2);
  hold(baraxeshandles(2),'on')
  
  %Plot bid/ask values versus cumulative bid/ask sizes
  plot([bookdata{i,1}],buysizes,'r-','linewidth',2)
  area(baraxeshandles(2),cell2mat(bookdata(j,4)),asksizes,'facecolor','blue','edgecolor','blue','linewidth',2); 
  plot([bookdata{j,4}],asksizes,'b-','linewidth',2)
  set(baraxeshandles(2),'xlim',[min(cell2mat(bookdata(i,1)))-.01 max(cell2mat(bookdata(i,4)))+.01])
  drawnow
  hold(baraxeshandles(2),'off')
catch %#ok
  %waiting until enough data to display
end