Writing and Running Custom Event Handler Functions
Write Custom Event Handler Function
You can process events related to any data updates by writing a custom event handler function for use with Datafeed Toolbox™. For example, you can monitor prices before creating an order or plot interval data in a graph. Follow these basic steps to write a custom event handler.
- Choose the events you want to process, monitor, or evaluate. 
- Decide how the custom event handler processes these events. 
- Determine the input and output arguments for the custom event handler function. 
- Write the code for the custom event handler function. 
For details, see Create Functions in Files. For a code example
          of a Bloomberg® event handler function, enter edit v3stockticker.m at the
          command line.
Run Custom Event Handler Function
You can run the custom event handler function by passing the function name as an input argument into an existing function. Specify the custom event handler function name either as a character vector, string, or function handle. For details about function handles, see Create Function Handle.
For example, suppose you want to retrieve real-time data from Bloomberg using realtime with the custom event handler
          function named eventhandler. You can use either of the following
          syntaxes to run eventhandler. This code assumes a Bloomberg connection c, security list s,
            Bloomberg data fields f, Bloomberg subscription subs, and MATLAB® timer t.
Use a character vector or string.
[subs,t] = realtime(c,s,f,'eventhandler');
Alternatively, use a function handle.
[subs,t] = realtime(c,s,f,@eventhandler);
For Bloomberg EMSX interfaces, you can run the custom event handler function by using
            timer. Specify the custom event handler
          function name as a function handle and pass this function handle as an input argument to
            timer. For details about function handles, see Create Function Handle. For example, suppose you want to create an order
          using createOrderAndRoute with the custom event
          handler function named eventhandler. This code assumes a Bloomberg EMSX connection c, Bloomberg EMSX order order, and timer object
          t.
- Run - timerto execute- eventhandler. The name-value argument- TimerFcnspecifies the event handler function. The name-value argument- Periodspecifies a 1-second delay between executions of the event handler function. When the name-value argument- ExecutionModeis set to- fixedRate, the event handler function executes immediately after it is added to the MATLAB execution queue.- t = timer('TimerFcn',{@c.eventhandler},'Period',1, ... 'ExecutionMode','fixedRate'); 
- Start the timer to initiate and execute - eventhandlerimmediately.- start(t) 
- Run - createOrderAndRouteusing the custom event handler by setting- useDefaultEventHandlerto- false.- createOrderAndRoute(c,order,'useDefaultEventHandler',false)
- Stop the timer to stop data updates. - stop(t) - If you want to resume data updates, run - start.
- Delete the timer once you are done with processing data updates for the Bloomberg EMSX connection. - delete(t) 
Workflow for Custom Event Handler Function
This workflow summarizes the basic steps to work with a custom event handler function for any of the data service providers, except Bloomberg EMSX.
- Write a custom event handler function and save it to a file. 
- Create a connection to the data service provider. 
- Subscribe to a specific security using an existing function or API syntax. 
- Run an existing function to receive data updates and use the custom event handler function as an input argument. 
- Stop data updates by using - stopor by closing the connection to the data service provider.
- Close the connection to the data service provider if the connection is still open. 
For Bloomberg EMSX interfaces, follow this workflow.
- Write a custom event handler function and save it to a file. 
- Create a connection using - emsx.
- Subscribe to Bloomberg EMSX fields using - ordersand- routes. You can also write custom event handler functions to process subscription events.
- Run the custom event handler function using - timer. Use a function handle to specify the custom event handler function name to run- timer.
- Start the timer to execute the custom event handler function immediately using - start.
- Stop data updates using - stop.
- Unsubscribe from Bloomberg EMSX fields by using the API syntax. 
- Delete the timer using - delete.
- Close the connection using - close.
See Also
realtime | close | emsx | createOrderAndRoute | orders | routes | timer | start | stop | delete