How can I optimize my quantitative trading strategy, e.g. MACD signal?


In the previous post, we saw how to backtest a quantitative trading strategy. The result was, that we suspected that we should be able to find better constants for the MACD trading signal strategy. Now, we will use Matlab and Theta Suite in order to create an efficient optimization. Furthermore, we create a simple visualization of the resulting performance.

Setting up Matlab for Theta Suite access

After creating a model for the MACD signal trading strategy, we set up Matlab for accessing the model.

%% Initialize
% set paths required for Theta Suite connection

% set workspace with the project required

% from project "Pricing" load "bench.thetml"
thc = theta_compiler('bench.thetaml');

Set parameters and run ThetaML compiler

Then, we can set up the parameters const_l_12 to [1:40] and const_l_26 to [11:110], which is just one line of matlab meshgrid():
%% Run

% create compiler object
conf = thc.getConf();

% create grid of perameters of interest
[X,Y] = meshgrid(1:40, 11:110);

% set parameters as vectors and mark as model point using "@mp"
conf.param.const_l_12.value = {};
conf.param.const_l_12.value{1} = '@mp';
conf.param.const_l_12.value{2} = X(:);

conf.param.const_l_26.value = {};
conf.param.const_l_26.value{1} = '@mp';
conf.param.const_l_26.value{2} = Y(:);

% prepare path-wise compuation, i.e. model points in the vector
conf.generator.output_file.value = '';
conf.eval.MC.n.value = length(Y(:));
conf.VAModelpoints.PathWise.value = true;

% overwrite data if required (is also loaded from bench.thetaml) = hist_close; = yearfrac(hist_date(1), hist_date(1:end));

% set and run configuration using the theta_compiler object
res =;

Render graphics in a post-process

Running this script performs 4000 different performance estimations within about 60 seconds. After that, we can create nice graphics as result:

%% Plot

title('Performance of MACD trading strategy on IBM')
zlabel('Cumulative return 2000-2011')

[M, I ] = max(res.output.Return.value(:,1,end));
title('Performance of best MACD trading strategy on IBM')
ylabel('Cumulative return from 2000')

Evaluate results

Running the plots shows the following:

and the best paths is

That means, the optimized MACD strategy was performing great during 2000 to 2003. After that time period, this strategy does not perform at all. Taking transaction costs into account, the performance is even much worse.


Using Matlab and Theta Suite, an efficient optimization procedure is performed. We can improve the parameters of the MACD trading strategy significantly. However, the optimized strategy only works within a short period of the historical data. This leaves you with the task to investigate other strategies: Good luck!

1 Comment

Comments RSS
  1. Pawe

    Great article. The performance is always dependable on trading regimes. As for the late figure the first 1/3 of performance is very nice and stably upwards. The good exercise would be to view the dynamics of the indicator and recognize the initial condition.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s