Main Content

Manage Bloomberg EMSX Order and Route

This example shows how to connect to Bloomberg® EMSX, set up an order and route subscription, create and route an order, and interact with the route.

For details about Bloomberg EMSX, see the EMSX API Programmer’s Guide using the WAPI <GO> option from the Bloomberg terminal.

Connect to Bloomberg EMSX

Connect to the Bloomberg EMSX test service. Display the current event queue contents using processEvent.

c = emsx('//blp/emapisvc_beta');
processEvent(c)
c = 
 
  emsx with properties:

      Session: [1x1 com.bloomberglp.blpapi.Session]
      Service: [1x1 com.bloomberglp.blpapi.impl.aQ]
    Ipaddress: 'localhost'
         Port: 8194

SessionConnectionUp = {
    server = localhost/127.0.0.1:8194
}

SessionStarted = {
}

ServiceOpened = {
    serviceName = //blp/emapisvc_beta
}

MATLAB® returns c as the connection to the Bloomberg EMSX test service with the following:

  • Bloomberg EMSX session object

  • Bloomberg EMSX service object

  • IP address of the machine running the Bloomberg EMSX test service

  • Port number of the machine running the Bloomberg EMSX test service

processEvent displays events associated with connecting to Bloomberg EMSX.

Set Up Order and Route Subscription

Subscribe to order events using the Bloomberg EMSX connection c associated with these Bloomberg EMSX fields.

fields = {'EMSX_TICKER','EMSX_AMOUNT','EMSX_FILL'};

[events,osubs] = orders(c,fields)
events = 

                       MSG_TYPE: {'E'}
                   MSG_SUB_TYPE: {'O'}
                   EVENT_STATUS: 4
                   ...

osubs =
 
com.bloomberglp.blpapi.SubscriptionList@4bc3dc78

events contains fields for the events associated with the existing Bloomberg EMSX orders. osubs contains the Bloomberg EMSX subscription list object.

Subscribe to route events for the Bloomberg EMSX fields EMSX_BROKER and EMSX_WORKING using the Bloomberg EMSX connection c. Return the status for existing routes.

fields = {'EMSX_BROKER','EMSX_WORKING'};

[events,rsubs] = routes(c,fields)
events = 

                       MSG_TYPE: {5x1 cell}
                   MSG_SUB_TYPE: {5x1 cell}
                   EVENT_STATUS: [5x1 int32]
                   ...

rsubs =

com.bloomberglp.blpapi.SubscriptionList@463b9287

events contains fields for the events currently in the event queue. rsubs contains the Bloomberg EMSX subscription list object.

Create and Route Order

Create the order request structure order to define the order parameters. This code creates a buy market order for 100 shares of IBM®. This code uses the broker BB with the time in force set to DAY and any hand instruction. Convert the number of shares to a 32-bit signed integer using int32.

order.EMSX_TICKER = 'IBM';
order.EMSX_AMOUNT = int32(100);
order.EMSX_ORDER_TYPE = 'MKT';
order.EMSX_BROKER = 'BB';
order.EMSX_TIF = 'DAY';
order.EMSX_HAND_INSTRUCTION = 'ANY';
order.EMSX_SIDE = 'BUY';

Create and route the order using the Bloomberg EMSX connection c and the order request structure order.

events = createOrderAndRoute(c,order)
events = 
    
    EMSX_SEQUENCE: 335877
    EMSX_ROUTE_ID: 1
          MESSAGE: 'Order created and routed' 

The default event handler processes the events associated with creating and routing the order. createOrderAndRoute returns events as a structure that contains these fields:

  • Bloomberg EMSX order number

  • Bloomberg EMSX route identifier

  • Bloomberg EMSX message

Modify Route

Define the modroute structure that contains these fields:

  • Bloomberg EMSX order sequence number EMSX_SEQUENCE

  • Bloomberg EMSX ticker symbol EMSX_TICKER

  • Bloomberg EMSX number of shares EMSX_AMOUNT

  • Bloomberg EMSX route identifier EMSX_ROUTE_ID

This code modifies the route to 50 shares of IBM for order sequence number 335877 and route identifier 1. Convert the numbers to 32-bit signed integers using int32.

modroute.EMSX_SEQUENCE = int32(335877)
modroute.EMSX_TICKER = 'IBM';
modroute.EMSX_AMOUNT = int32(50);
modroute.EMSX_ROUTE_ID = int32(1);

Modify the route using the Bloomberg EMSX connection c and modify route request modroute.

events = modifyRoute(c,modroute)
events = 
    
    EMSX_SEQUENCE: 0
    EMSX_ROUTE_ID: 0
          MESSAGE: 'Route modified'

The default event handler processes the events associated with modifying a route. modifyRoute returns events as a structure that contains these fields:

  • Bloomberg EMSX order number

  • Bloomberg EMSX route identifier

  • Bloomberg EMSX message

Delete Route

Define the structure routenum that contains the order sequence number EMSX_SEQUENCE for the routed order and route number EMSX_ROUTE_ID.

routenum.EMSX_SEQUENCE = 0;
routenum.EMSX_ROUTE_ID = 0;

Delete the route using the Bloomberg EMSX connection c and delete route number structure routenum.

events = deleteRoute(c,routenum)
events = 

     STATUS: '1'
    MESSAGE: 'Route cancellation request sent to broker'

The default event handler processes the events associated with deleting a route. deleteRoute returns events as a structure that contains these fields:

  • Bloomberg EMSX status

  • Bloomberg EMSX message

Stop Order and Route Subscription

Unsubscribe from order and route events using the Bloomberg EMSX subscription list objects osubs and rsubs. This code assumes that orders creates osubs and routes creates rsubs.

c.Session.unsubscribe(osubs)
c.Session.unsubscribe(rsubs)

Close Bloomberg EMSX Connection

close(c)

See Also

| | | | | |

Related Examples

More About

External Websites