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

4 visualizaciones (últimos 30 días)
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:
load('./price.mat')
%% 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?
  2 comentarios
Jan
Jan el 6 de Mzo. de 2022
Without seeing the code and based on the lean description of what you are doing, the best possible answer is to run it on faster hardware.
If you provide the code and some typical input data, a more explicit suggestion for improving the code is likely. Add the information you get by running the profiler, such that it gets clear, what the bottleneck of the code is.
Artem Lensky
Artem Lensky el 7 de Mzo. de 2022
Jan thanks for the prompt reply, I've updated the question.

Iniciar sesión para comentar.

Respuesta aceptada

Brendan
Brendan el 10 de Mzo. de 2022
Editada: Brendan el 31 de Mzo. de 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,...
'RebalanceFrequency',rebal_dates,...
'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!
  2 comentarios
Brendan
Brendan el 10 de Mzo. de 2022
oh I forgot to add... you can't run a backtest on multiple cores in parallel because the strategy rebalance decisions and investment positions are dependend on the previous days position and returns.
If you are running multiple strategies at once you could potentially run those on multiple cores, but this functionality is not built into the backtest engine by default. You would need to make a vector of backtests engines, each associated with a single strategy, and then you could run them all in parallel using parfor or some other parallel technology. Thanks!
Artem Lensky
Artem Lensky el 10 de Mzo. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Transaction Cost Analysis en Help Center y File Exchange.

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by