The Gateway to Algorithmic and Automated Trading

The Virtue of Simplicity

Published in Automated Trader Magazine Issue 22 Q3 2011

As we keep pointing out, automated/algorithmic trading does not automatically imply ultra high frequency trading. Plenty of Automated Trader traders we talk to seem to do just fine deploying automated models that hold positions for periods ranging from seconds to days. At the same time, rather a lot of them appear to use MATLAB and more than a few use Interactive Brokers. All of which rather inevitably led the Wrecking Crew and Automated Trader’s Founder, Andy Webb, to take a look at IB-MATLAB...

First, an important message from the Automated Trader software review team. If you are looking for a high frequency trading interface to Interactive Brokers, please stop reading now; IB-MATLAB isn't intended for you. Why not flick to Peek Ahead at the end of the magazine and read this issue's morally improving message from the guys in the attic instead?

IB-MATLAB is intended to provide a quick and simple path to interfacing MATLAB with Interactive Brokers' IB API (as opposed to the FIX CTCI API that Interactive Brokers also offers). The intention is that IB-MATLAB will be primarily used to send orders and receive order fills - not for receiving real time streaming data from the IB API. For one thing, the IB API has a specified limit of 50 messages per second that would quickly be consumed by real time data from even a modest portfolio of highly active securities. For another, MATLAB is also not really intended for such a purpose and is likely to complain if its interfaces are swamped by a blizzard of market data.


While IB-MATLAB isn't intended for high-volume streaming of real time data, it happily supports one-off requests for current market priceås. The syntax is pretty simple and follows the same basic method as most other IB-MATLAB functions, which consist of pairing an input parameter with a user specified parameter value. The generic syntax is:

IB_trade('parameter 1', 'parameter value 1', 'parameter 2', 'parameter value 2' ...'parameter n', 'parameter value n')

For example, to get a quick update on Cisco, just type the following into the MATLAB console (or invoke it in a MATLAB function or script):

data = IB_trade('action','QUERY', 'symbol','CSCO')

This returns a MATLAB struct called 'data' that includes items such as bid, ask, open, high, low, close, volume, tick size and time stamp. As a standard MATLAB struct, this means that any of these items can be accessed in MATLAB by using 'dot referencing'. For example, entering:

data.high the MATLAB console returns the high of the session:

ans =

It is also possible to retrieve historical data for the current trading session. For example...

histdata = IB_trade('action', 'HISTORY_DATA', 'symbol','IBM', 'barSize','1 min', 'useRTH',1); pretty self explanatory ('useRTH' means 'use regular trading hours') and returns the following struct:

histdata =
dateTime: {1x390 cell}
open: [1x390 double]
high: [1x390 double]
low: [1x390 double]
close: [1x390 double]
volume: [1x390 double]
count: [1x390 double]
WAP: [1x390 double]
hasGaps: [1x390 logical]

As with the previous example, because a standard MATLAB struct is returned, individual data categories can be dot referenced. For instance, typing...

plot(histdata.close(1,1:390)) the console generates a chart of the closing values of each one minute bar as shown in Figure 1.

figure 1

Figure 1

The output to data requests can immediately be reused as inputs to an order. For example, the bid value from the struct that results from the following...

data = IB_trade('action','QUERY', 'symbol','CSCO') used ('') in an attempt to buy Cisco at the bid in the following order:

orderId = IB_trade('action','BUY', 'symbol',stk.stk{rndsec,1}, 'quantity',100, 'limitPrice',;

The 'orderId' variable is immediately re-output to the MATLAB console together with any resulting fill information:

The remainder of this article is only available to Paid Subscribers

Click here to purchase a subscription to Automated Trader

  • Copyright © Automated Trader Ltd 2018 - Strategies | Compliance | Technology

click here to return to the top of the page