APPENDIX
A Quick Survey of MATLAB

MATLAB is a general-purpose software package developed by Mathworks, Inc., which is used by many institutional quantitative researchers and traders as their platform for backtesting, particularly those who work in statistical arbitrage. In Chapter 3, I introduced this platform and compared its pros and cons with some other alternatives. Most of the strategy examples in this book are written in MATLAB. Many of those strategies are portfolio-trading strategies involving hundreds of stocks that are very difficult to backtest in Excel. Here, I will provide a quick survey of MATLAB for those traders who are unfamiliar with the language, so they can see if it is worthwhile for them to invest in acquiring and learning to use this platform for their own backtesting.

MATLAB is not only a programming language; it is also an integrated development platform that includes a very user-friendly program editor and debugger. It is an interpreted language, meaning that it’s similar to Visual Basic, but unlike a conventional programming language like C, it does not need to be compiled before it can be run. Yet it is much more flexible and powerful for backtesting than using Excel or Visual Basic because of the large number of built-in functions useful for mathematical computations, and because it is an array-processing language that is specially designed to make computations on arrays (i.e., vectors or matrices) simply and quickly. In particular, many loops that are necessary in C or Visual Basic can be replaced by just one line of code in MATLAB. It also includes extensive text-processing facilities such that it is useful as a powerful tool for parsing and analyzing texts (such as web pages). Furthermore, it has a comprehensive graphics library that enables easy plotting of many types of graphs, even animations. (Many of the figures and charts in this book are created using MATLAB.) Finally, MATLAB codes can be compiled into C or C++ executables that can run on computers without the MATLAB platform installed. In fact, there is third-party software that can convert MATLAB code into C source codes, too.

The basic syntax of MATLAB is very similar to Visual Basic or C. For example, we can initialize the elements of an array x like this:

x(1)=0.1;
x(2)=0.3;
% 3 elements of an array initialized.
% This is by default a row-vector
x(3)=0.2;

Note that we don't need to first “declare” this array, nor do we need to tell MATLAB its expected size beforehand. If you leave out the “;” sign, MATLAB will print out the result of the content of the variable being assigned a value. Any comments can be written after the “%” sign. If you wish, you can initialize a large number of elements en masse to a common value:

% assigning the value 0.8 to all elements of a 3-vector y.
This is a row-vector.y=0.8*ones(1, 3)

Now if you want to do a vector addition of the two vectors, you can do it the old-fashioned way (just as you would in C), that is, using a loop:

for i=1:3
z(i)=x(i)+y(i) % z is [0.9 1.1 1]
end

But the power of MATLAB is that it can handle many array operations in parallel very succinctly, without using loops. (That's why it is called a vector-processing language.) So instead of the previous loop, you can just write

z=x+y % z is the same [0.9 1.1 1]

Even more powerful, you can easily select part of the different arrays and operate on them. What do you think would be the results of the following?

w=x([1 3])+z([2 1])

x([1 3]) selected the first and third elements of x, so x([1 3]) is just [0.1 0.2]. z([2 1]) selected the second and first elements of y, in that order, so z([2 1]) is [1.1 0.9]. So w is [1.2 1.1].

You can delete parts of an array just as easily:

x([1 3])=[] % this leaves x as [0.3]

To concatenate two arrays is also trivial. To concatenate by rows, use the “;” to separate the arrays:

u=[z([1 1]); w]
% u is now
% [0.9000 0.9000;
% 1.2000 1.1000]

To concatenate by columns, omit the “;”:

v=[z([1 1]) w]
% v is now
% [0.9000 0.9000 1.2000 1.1000]

Selection of a subarray can be done not only with arrays containing indices; it can be done with arrays containing logical values as well. For example, here is a logical array:

vlogical=v<1.1
% vlogical is [1 1 0 0], where the 0s and 1's
% indicate whether that element is less than 1.1 or
% note.
vlt=v(vlogical) % vlt is [0.9 0.9]

In fact, we can select the same subarray with the oft-used shorthand.

vlt=v(v<1.1) % vlt is the same [0.9 0.9]

If, for some reason, you are interested in the actual indices of the elements of v that have value less than 1.1, you can use the “find” function:

idx=find(v<1.1); % idx is [1 2]

Naturally, you can use this index array to select the same subarray as before:

vlt=v(idx); % vlt is the again same [0.9 0.9]

So far, the array examples are all one-dimensional. But of course, MATLAB can deal with multidimensional arrays as well. Here is a two-dimensional example:

x=[1 2 3; 4 5 6; 7 8 9];
% x is
% 1 2 3
% 4 5 6
% 7 8 9

You can select the entire row or column of a multidimensional array by using the “:” symbol. For example:

xr1=x(1,:) % xr1 is the first row of x, i.e. xr1 is [1 2 3]
xc2=x(:, 2)% xc2 is the second column of x, i.e. xc2 is
% 2
% 5
% 8

Naturally, you can delete an entire row from an array using the same method.

x(1,:) =[] % x is now just [4 5 6; 7 8 9]

The transpose of a matrix is indicated by a simple ‘′’. So the transpose of x is just x′, which is

4 7
5 8
6 9

Elements of arrays do not have to be numbers. They can be strings, or even arrays themselves. This kind of array is called cell array in MATLAB. In the following example, C is just such a cell array:

C={[1 2 3]; [`a' `b' `c' `d']}
% C is
% [1 2 3]
% `abcd'

One of the beauties of MATLAB is that practically all built-in functions can work on all elements of arrays concurrently. For example,

log(x) % this gives
% 1.3863 1.6094 1.7918
% 1.9459 2.0794 2.1972

There are a large number of such built-in functions. Some of the ones I have used are:

sum, cumsum, diag, max, min, mean, std, corrcoef,
repmat, reshape, squeeze, sort, sortrow, rand, size,
length, eigs, fix, round, floor, ceil, mod,
factorial, setdiff, union, intersect, ismember,
unique, any, all, eval, eye, ones, strmatch, regexp,
regexprep, plot, hist, bar, scatter, try, catch,
circshift, datestr, datenum, isempty, isfinite,
isnan, islogical, randperm

If the built-in functions of the basic MATLAB platform do not meet all your needs, you can always purchase additional toolboxes from MATLAB. I will discuss some of them as follows.

There is also a newer data structure in MATLAB called “tables” that I found very useful. (It is very similar to Python's Pandas Dataframes, in case any Python programmers are reading this.) Tables are arrays that come with column headings and possibly dates for rows, so you don't need to remember that, for example, cell (1562, 244) refers to the closing price of Tesla on October 23, 2020. Instead, you can retrieve that price by writing:

TT{’23-Oct-2020’, ‘TSLA’}

Tables and timetables make for some powerful simplifications in manipulating time series data that has two dimensions (called panel data by economists), such as the P&Ls of a portfolio of stocks. For example, you can use the synchronize function to quickly merge the P&L table and the capital allocation table, automatically aligning their dates. You can also use the retime function to convert daily P&Ls to monthly P&Ls with a single line of code.

Some of the toolboxes useful to quantitative traders are Optimization, Global Optimization (when you don't want to get stuck in a local minima/maxima and want to try simulated annealing or genetic algorithms), Statistics and Machine Learning (that's the most useful toolbox for me), Deep Learning, Signal Processing (technical indicators are but a small subset of signal processing functions), Financial (for backtesting!), Financial Instruments (good for options pricing, backing out implied volatility etc.), Econometrics (GARCH, ARIMA, VAR, and all the techniques you need for financial time series analysis), Datafeed (to retrieve data from IQFeed, Quandl, etc., easily), and Trading (to send orders to Interactive Brokers, TT, or even FIX). These toolboxes typically cost only about $50 each for a home user. If they still do not meet all your needs, there are also a number of free user-contributed toolboxes available for download from the internet. I have introduced one of them in this book: the Econometrics toolbox developed by James LeSage (www.spatial-econometrics.com). There are a number of others that I have used before such as the Bayes Net toolbox from Kevin Murphy (github.com/bayesnet/bnt). The easy availability of these user-contributed toolboxes and the large community of MATLAB users from whom you can ask for help greatly enhance the usefulness of MATLAB as a computational platform.

You can, of course, write your own functions in MATLAB, too. I have given a number of example functions in this book, all of which can be downloaded from my website, www.epchan.com/book. In fact, it is very helpful for you to develop your own library of utilities functions that you often use for constructing trading strategies. As this homegrown library grows, your productivity in developing new strategies will increase as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.100.120