Main Content

Listen for X_TRADER Price Market Depth Updates

This example shows how to connect to X_TRADER® and turn on event handling for level-two market data (for example, bid and ask orders in the market for an instrument) and then create a figure window to display the depth data.

Connect to X_TRADER

X = xtrdr;

Create an Event Notifier

Create an event notifier and enable depth updates. The event notifier is the X_TRADER mechanism lets you define MATLAB® functions to use as callbacks for specific events.

createNotifier(X)
X.InstrNotify(1).EnableDepthUpdates = 1;

Create an Instrument

createInstrument(X,'Exchange','CME','Product','2F','ProdType','Future',...
               'Contract','Aug13','Alias','PriceInstrumentDepthUpdate')

Attach an Instrument to a Notifier

Assign one or more notifiers to an instrument. A notifier can have one or more instruments attached to it.

X.InstrNotify(1).AttachInstrument(X.Instrument(1))

Define Events

Assign callbacks for validating or invalidating an instrument, and updating the example order book window.

registerevent(X.InstrNotify(1),{'OnNotifyFound',...
                                  @ttinstrumentfound})
registerevent(X.InstrNotify(1),{'OnNotifyNotFound',...
                                  @ttinstrumentnotfound})
registerevent(X.InstrNotify(1),{'OnNotifyDepthData',...
                                  @ttinstrumentdepthupdate})

Set Up the Figure Window

Set up the figure window to display depth data.

f = figure('Numbertitle','off','Tag','TTPriceUpdateDepthFigure',...
       'Name',['Order Book - ' X.Instrument(1).Alias])
pos = f.Position;
f.Position = [pos(1) pos(2) 360 315];
f.Resize = 'off';

Create Controls

Create controls for the last price data.

bspc = 5;
bwid = 80;
bhgt = 20;

uicontrol('Style','text','String','Exchange',...
          'Position',[bspc 4*bspc+3*bhgt bwid bhgt])
uicontrol('Style','text','String','Product',...
          'Position',[2*bspc+bwid 4*bspc+3*bhgt bwid bhgt])
uicontrol('Style','text','String','Type',...
          'Position',[3*bspc+2*bwid 4*bspc+3*bhgt bwid bhgt])
uicontrol('Style','text','String','Contract',...
          'Position',[4*bspc+3*bwid 4*bspc+3*bhgt bwid bhgt])
ui.Exchange = uicontrol('Style','text','Tag','',...
          'Position',[bspc 3*bspc+2*bhgt bwid bhgt]);
ui.Product = uicontrol('Style','text','Tag','',...
          'Position',[2*bspc+bwid 3*bspc+2*bhgt bwid bhgt]);
ui.Type = uicontrol('Style','text','Tag','',...
          'Position',[3*bspc+2*bwid 3*bspc+2*bhgt bwid bhgt]);
ui.Contract = uicontrol('Style','text','Tag','',...
          'Position',[4*bspc+3*bwid 3*bspc+2*bhgt bwid bhgt]);
uicontrol('Style','text','String','Last Price',...
          'Position',[bspc 2*bspc+bhgt bwid bhgt])
uicontrol('Style','text','String','Last Qty',...
          'Position',[2*bspc+bwid 2*bspc+bhgt bwid bhgt])
uicontrol('Style','text','String','Change',...
          'Position',[3*bspc+2*bwid 2*bspc+bhgt bwid bhgt])
ui.Last = uicontrol('Style','text','Tag','',...
          'Position',[bspc bspc bwid bhgt]);
ui.Quantity = uicontrol('Style','text','Tag','',...
          'Position',[2*bspc+bwid bspc bwid bhgt]);
ui.Change = uicontrol('Style','text','Tag','',...
          'Position',[3*bspc+2*bwid bspc bwid bhgt]);

Create a Table

Create a table containing order information.

data = {' '};
data = data(ones(10,4));
uibook = uitable('Data',data,'ColumnName',...
                {'Bid','Bid Size','Ask','Ask Size'},...
                 'Position',[5 105 350 205]);

Store Data

setappdata(0,'TTOrderBookHandle',uibook)
setappdata(0,'TTOrderBookUIData',ui)

Listen for Event Data

Listen for event data with depth updates enabled.

X.Instrument(1).Open(1)

Order Book - Price Instrument Depth Update figure displays event data such as bid and ask prices, exchange, product, type, and last price.

The last command instructs X_TRADER to start monitoring the attached instruments using the specified event settings.

Close the Connection

close(X)

See Also

| | | |

Related Examples

More About