Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Writing and Running Custom Event Handler Functions with Interactive Brokers

Write a Custom Event Handler Function

You can process events related to any Interactive Brokers® data updates by writing a custom event handler function to use with Trading Toolbox™. For example, you can request data about all open orders or retrieve account information. Follow these tasks to write a custom event handler.

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

  2. Decide how the custom event handler function 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, see Create Functions in Files (MATLAB). For a code example of an Interactive Brokers event handler function, see ibExampleEventHandler.m.

Run a 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 as a character vector, string, or function handle. For details about function handles, see Create Function Handle (MATLAB).

For example, suppose you want to retrieve real-time data from Interactive Brokers using realtime with the custom event handler function named eventhandler. You can use either of these syntaxes to run eventhandler. This code assumes a IB Trader WorkstationSM connection ib, IB Trader Workstation IContract object ibContract, and Interactive Brokers fields f.

Use a character vector or string.

tickerid = realtime(ib,ibContract,f,'eventhandler');

Or, use a function handle.

tickerid = realtime(ib,ibContract,f,@eventhandler);

Workflow for Custom Event Handler Function

This workflow summarizes the tasks to work with a custom event handler function using Interactive Brokers.

  1. Write a custom event handler function and save it to a file.

  2. Create a connection to the IB Trader Workstation using ibtws.

  3. Run an existing function to receive data updates. Use the custom event handler function as an input argument.

    Caution:

    To run default event handler and sample event handler functions, you must run one event handler function at a time. After you run one event handler, close the IB Trader Workstation connection. Then, create another connection to run a different event handler with the same existing function. Otherwise, MATLAB® assigns multiple existing functions to events and errors occur.

  4. Close the connection to the IB Trader Workstation using close.

See Also

| |

Related Topics

Was this topic helpful?