Startup

Add ib-matlab.jar to Matlab's Current Directory (e.g., C:\ib-matlab), add it to the path, import IB, create the callback and connection, and connect.

Command Window
>> cd('C:\ib-matlab\');
>> javaaddpath ib-matlab.jar;
>> import com.ib.client.*;

>> callback = IBCallback();
>> ib = EClientSocket(callback);

>> ib.eConnect('127.0.0.1', 7496, 0);

Errors can be printed out with callback.printErrors = true; or callback = IBCallback(true).

You can have one IBCallback object per EClientSocket object, or use the same IBCallback object for multiple EClientSocket objects.

Callbacks

Matlab functions are called when events occur. To establish a callback, create a callback function in Matlab with the specified parameters and set the property of the event you wish to be notified for to the name of the function.

See the simple example below and see IB's documentation for callback details.

If you do not specify a callback for an event, the event will be ignored.

CommandCallback function parameters
Connection and server
callback.currentTime = 'callback_function_name'time
callback.error = 'callback_function_name'int id, int errorCode, String errorMsg
callback.connectionClosed = 'callback_function_name'
Market data
callback.tickPrice = 'callback_function_name'tickerId, field, price, canAutoExecute
callback.tickSize = 'callback_function_name'tickerId, field, size
callback.tickOptionComputation = 'callback_function_name'tickerId, field, impliedVol, delta, modelPrice, pvDividend
callback.tickGeneric = 'callback_function_name'tickerId, tickType, value
callback.tickString = 'callback_function_name'tickerId, tickType, value
callback.tickEFP = 'callback_function_name'tickerId, tickType, basisPoints, formattedBasisPoints, impliedFuture, holdDays, futureExpiry, dividendImpact, dividendsToExpiry
callback.tickSnapshotEnd = 'callback_function_name'reqId
Orders
callback.orderStatus = 'callback_function_name'orderId, status, filled, remaining, avgFillPrice, permId, parentId, lastFillPrice, clientId, whyHeld
callback.openOrder = 'callback_function_name'orderId, contract, order, orderState
callback.nextValidId = 'callback_function_name'orderId
callback.openOrderEnd = 'callback_function_name'
Account and portfolio
callback.updateAccountValue = 'callback_function_name'key, value, currency, accountName
callback.updatePortfolio = 'callback_function_name'contract, position, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL, accountName
callback.updateAccountTime = 'callback_function_name'timeStamp
callback.accountDownloadEnd = 'callback_function_name'accountName
Contract details
callback.contractDetails = 'callback_function_name'reqId, contractDetails
callback.contractDetailsEnd = 'callback_function_name'reqId
callback.bondContractDetails = 'callback_function_name'reqId, contractDetails
Executions
callback.execDetails = 'callback_function_name'contract, execution
callback.execDetailsEnd = 'callback_function_name'reqId
Market depth
callback.updateMktDepth = 'callback_function_name'tickerId, position, operation, side, price, size
callback.updateMktDepthL2 = 'callback_function_name'tickerId, position, marketMaker, operation, side, price, size
News bulletins
callback.updateNewsBulletin = 'callback_function_name'msgId, msgType, message, origExchange
Financial advisors
callback.managedAccounts = 'callback_function_name'accountsList
callback.receiveFA = 'callback_function_name'faDataType, xml
Historical data
callback.historicalData = 'callback_function_name'reqId, date, open, high, low, close, volume, count, WAP, hasGaps
Market scanners
callback.scannerParameters = 'callback_function_name'xml
callback.scannerData = 'callback_function_name'reqId, rank, contractDetails, distance, benchmark, projection, legsStr
callback.scannerDataEnd = 'callback_function_name'reqId
Real time bars
callback.realtimeBar = 'callback_function_name'reqId, time, open, high, low, close, volume, wap, count
Fundamental data
callback.fundamentalData = 'callback_function_name'reqId, data
Other
callback.deltaNeutralValidation = 'callback_function_name'reqId, underComp

Examples

Simple example

To receive the current time when it is requested, add callback.currentTime = 'my_current_time_callback', where callback is your IBCallback object, and implement the function my_current_time_callback with the correct arguments:

Editor - C:\ib-matlab\my_current_time_callback.m
function my_current_time_callback( time )
	disp('You called reqCurrentTime(), then ib called me.')
	disp(time)
end
Command Window
>> ib.reqCurrentTime();
% Nothing happens, since no callback specified.

>> callback.currentTime = 'my_current_time_callback';
>> ib.reqCurrentTime();
You called reqCurrentTime(), then ib called me.
1.2698e+009

Order example

Editor - C:\ib-matlab\my_open_order_callback.m
function my_open_order_callback( orderId, contract, order, orderState )
	disp('You called reqOpenOrders(), then ib called me.')
	disp(contract.m_symbol)
	disp(order.m_totalQuantity)
end
Command Window
>> % Create object
>> callback = IBCallback();
>> ib = EClientSocket(callback);
>> ib.eConnect('127.0.0.1', 7496, 0);

>> % Create contract
>> aa = Contract();
>> aa.m_symbol = 'AA';
>> aa.m_exchange = 'SMART';
>> aa.m_secType = 'STK';
>> aa.m_currency = 'USD';

>> % Create order
>> aa_order = Order();
>> aa_order.m_action = 'BUY';
>> aa_order.m_orderType = 'LMT';
>> aa_order.m_lmtPrice = 14.1;
>> aa_order.m_totalQuantity = 100;

>> % Place order
>> ib.placeOrder(1, aa, aa_order);

>> % Request order information sent to callbacks
>> callback.openOrder = 'my_open_order_callback';
>> ib.reqOpenOrders();
You called reqOpenOrders(), then ib called me.
AA
100

Complete example

Editor - C:\ib-matlab\ib_startup.m
cd('C:ib-matlab\');
javaaddpath ib-matlab.jar;
import com.ib.client.*;

callback = IBCallback();
ib = EClientSocket(callback);
ib.eConnect('127.0.0.1', 7496, 0);
Editor - C:\ib-matlab\my_current_time_callback.m
function my_current_time_callback( time )
	disp('You called reqCurrentTime(), then ib called me.')
	disp(time)
end
Editor - C:\ib-matlab\my_open_order_callback.m
function my_open_order_callback( orderId, contract, order, orderState )
	disp('You called reqOpenOrders(), then ib called me.')
	disp(contract.m_symbol)
	disp(order.m_totalQuantity)
end
Command Window
>> ib_startup
>> callback.currentTime = 'my_current_time_callback';

>> ib.reqCurrentTime();
You called reqCurrentTime(), then ib called me.
1.2698e+009

>> % create Contract aa and Order aa_order
>> % ...
>> ib.placeOrder(1, aa, aa_order);
>> callback.openOrder = 'my_open_order_callback';
>> ib.reqOpenOrders();
You called reqOpenOrders(), then ib called me.
AA
100

Conversions

Strings and character arrays

If you receive a String as an argument to a callback, you can convert it to a Matlab character array with the_chars = char(the_string). (Matlab character arrays are automatically converted to Java Strings when sent to a EClientSocket object.)

IB data model objects and structs

To convert an IB data model object such as a Contract or an Order to a Matlab struct, use a_contract_struct = struct(an_ib_contract_object) (using Contracts as an example). You can also manually create a struct with only the parameters you need, as in the Contract and Order examples below. Converting back to objects to send to IB must be done manually, as demonstrated below.

Contract

Editor - C:\ib-matlab\contract_to_struct.m
function contract_struct contract_to_struct( ib_contract )
    contract_struct = struct('symbol', ib_contract.m_symbol, 'exchange', ib_contract.m_exchange, 'security_type', ib_contract.m_secType, 'currency', ib_contract.m_currency); % etc, for used parameters
end
Editor - C:\ib-matlab\contract_from_struct.m
function ib_contract contract_from_struct( contract_struct )
    ib_contract = Contract();
    ib_contract.m_symbol = contract_struct.symbol;
    ib_contract.m_exchange = contract_struct.exchange;
    ib_contract.m_securityType = contract_struct.security_type;
    ib_contract.m_currency = contract_struct.currency;
    % etc, for used parameters
end

Order

Editor - C:\ib-matlab\order_to_struct.m
function order_struct order_to_struct( ib_order )
    order_struct = struct('action', ib_order.m_action, 'order_type', ib_order.m_orderType, 'limit_price', ib_order.m_lmtPrice, 'total_quantity', ib_order.totalQuantity); % etc, for used parameters
end
Editor - C:\ib-matlab\order_from_struct.m
function ib_order order_from_struct( order_struct )
    ib_order = Order();
    ib_order.m_action = order_struct.action;
    ib_order.m_orderType = order_struct.order_type;
    ib_order.m_lmtPrice = order_struct.limit_price;
    ib_order.m_totalQuantity = order_struct.total_quantity;
    % etc, for used parameters
end