Main Content

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.

  1. Choose the events you want to process, monitor, or evaluate.

  2. Decide how the custom event handler processes these events.

  3. Determine the input and output arguments for the custom event handler function.

  4. Write the code for the custom event handler function.

For details, seeCreate Functions in Files。For a code example of a Bloomberg®event handler function, enteredit v3stockticker.mat 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, seeCreate Function Handle

For example, suppose you want to retrieve real-time data from Bloomberg usingrealtimewith the custom event handler function namedeventhandler。You can use either of the following syntaxes to runeventhandler。This code assumes a Bloomberg connectionc, security lists, Bloomberg data fieldsf, Bloomberg subscriptionsubs, and MATLAB®timert

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 usingtimer。指定自定义的事件处理程序函数名称s a function handle and pass this function handle as an input argument totimer。For details about function handles, seeCreate Function Handle。For example, suppose you want to create an order usingcreateOrderAndRoutewith the custom event handler function namedeventhandler。This code assumes a Bloomberg EMSX connectionc, Bloomberg EMSX orderorder, and timer objectt

  1. Runtimerto executeeventhandler。The name-value argumentTimerFcnspecifies the event handler function. The name-value argumentPeriodspecifies a 1-second delay between executions of the event handler function. When the name-value argumentExecutionModeis set tofixedRate, the event handler function executes immediately after it is added to the MATLAB execution queue.

    t = timer('TimerFcn',{@c.eventhandler},'Period',1,。..'ExecutionMode','fixedRate');
  2. Start the timer to initiate and executeeventhandlerimmediately.

    start(t)
  3. RuncreateOrderAndRouteusing the custom event handler by settinguseDefaultEventHandlertofalse

    createOrderAndRoute(c,order,'useDefaultEventHandler',false)
  4. Stop the timer to stop data updates.

    stop(t)

    If you want to resume data updates, runstart

  5. 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.

  1. 编写一个定制的事件处理器函数并保存它to a file.

  2. Create a connection to the data service provider.

  3. Subscribe to a specific security using an existing function or API syntax.

  4. Run an existing function to receive data updates and use the custom event handler function as an input argument.

  5. Stop data updates by usingstopor by closing the connection to the data service provider.

  6. Close the connection to the data service provider if the connection is still open.

For Bloomberg EMSX interfaces, follow this workflow.

  1. 编写一个定制的事件处理器函数并保存它to a file.

  2. Create a connection usingemsx

  3. Subscribe to Bloomberg EMSX fields usingordersandroutes。You can also write custom event handler functions to process subscription events.

  4. Run the custom event handler function usingtimer。Use a function handle to specify the custom event handler function name to runtimer

  5. Start the timer to execute the custom event handler function immediately usingstart

  6. Stop data updates usingstop

  7. Unsubscribe from Bloomberg EMSX fields by using the API syntax.

  8. Delete the timer usingdelete

  9. Close the connection usingclose

See Also

|||||||||

Related Topics

External Websites