How can I speed up runBacktest that is part of Financial Toolbox?

2 views (last 30 days)
I am invoking runBacktest on tickdata and the function takes considerable time (several minutes on about 1 million ticks). Given that the strategy is computed separately and is trivial, it is quite slow.
Here is a sampe code:
%% Create a simple strategy
sma5 = movavg(price,'simple',50);
sma20 = movavg(price,'simple',200);
smaSignal_ = timetable(sma5.Time,...
double(sma5.("price") > sma20.("price")),...
'VariableNames', {'strategy'});
smaSignal = synchronize(price, smaSignal_);
%% Test the strategy
smaRebalanceFcn = @(weights, prices, signal)...
crossoverRebalanceFcn(weights, prices, signal);
smaStrategy = backtestStrategy('SMA',smaRebalanceFcn,...
'TransactionCosts', 0.05,...
'LookbackWindow', 2,...
'InitialWeights', []);
% Create the backtesting engine.
bt = backtestEngine(smaStrategy);
% Run the backtest.
bt = runBacktest(bt, smaSignal(:,1), smaSignal(:,2));
How can I speed it up? Perhpas I can run in on multiple cores? Could RAM be the bottleneck?

Sign in to comment.

Accepted Answer

Brendan Hannigan
Brendan Hannigan on 10 Mar 2022
Edited: Brendan Hannigan on 31 Mar 2022
Hi Artem,
There are a few optimizations you could do in the rebalance function itself, but the primary issue is just that you are invoking your rebalance function at every tick, so you are calling it something like 350,000 times. With each call to the rebalance function the backtest engine prepares the sliding window of asset prices and signal data timetables and invokes your function handle.
If you profile your code, you'll see the majority of time is just spent in preparing and indexing into these timetables.
One thing you could do is... because you have pre-computed your trading signal, you actually know ahead of time when you will need to rebalance (only when the value of the signal changes). So you could figure out those times and only rebalance when you know you need to. Here's what this would look like in your example:
% Only rebalance on dates when signal changes
diff_indices = find(diff(smaSignal.strategy) ~= 0) + 1;
rebal_dates = smaSignal.Time(diff_indices);
smaStrategy = backtestStrategy('SMA',smaRebalanceFcn,...
'TransactionCosts', 0.05,...
'LookbackWindow', 2,...
'InitialWeights', []);
Now, you will only rebalance your strategy when you need to. Obviously this only works because the strategy signal allows you to pre-compute the rebalance dates. That's not the case for many other strategies, so it won't work in all cases. Also, as the number of assets increases (and the number of signals), then the number of ticks where "nothing happens" becomes fewer and fewer.
hope this helps, cheers!
Artem Lenskiy
Artem Lenskiy on 10 Mar 2022
Thanks Brendan, your answers make perfect sense! To summurise, I will only call, whenever possible, rebalance function only at the times when trading signals are genereted, and will use parfor to run stratagies in parallel.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by