How can I speed up runBacktest that is part of Financial Toolbox?
4 次查看(过去 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:
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 个评论
Jan
2022-3-6
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.
采纳的回答
Brendan
2022-3-10
编辑:Brendan
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,...
'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 个评论
Brendan
2022-3-10
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!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Transaction Cost Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!