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.
Choose the events that you want to process, monitor, or evaluate.
Decide how the custom event handler function 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 an Interactive Brokers event
handler function, see ibExampleEventHandler.m
.
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.
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);
This workflow summarizes the tasks to work with a custom event handler function using Interactive Brokers.
Write a custom event handler function and save it to a file.
Create a connection to the IB Trader Workstation using ibtws
.
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.
Close the connection to the IB Trader Workstation using close
.