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

조회 수: 2(최근 30일)
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?
  댓글 수: 2
Artem Lenskiy
Artem Lenskiy 2022년 3월 7일
Jan thanks for the prompt reply, I've updated the question.

댓글을 달려면 로그인하십시오.

채택된 답변

Brendan Hannigan
Brendan Hannigan 2022년 3월 10일
편집: Brendan Hannigan 2022년 3월 31일
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!
  댓글 수: 2
Artem Lenskiy
Artem Lenskiy 2022년 3월 10일
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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by