In Backtesting, where does the input argument 'current_weights' in a rebalancing function come from?

3 visualizzazioni (ultimi 30 giorni)
The following code is from the MATLAB documents for 'backtestStrategy' from here. Please note that I added few lines to the original code for loading data and executing backtesting (i.e., add a line with 'runBacktest'), so that this code becomes solely functional.
According to the same documetns, rebalancing functions have restricted input arguments, i.e.,, 'current_weights', 'assetPriceTimeTable', and 'signalDataTimeTable'. And the second and the third arguments are also input arguments to the function 'runBacktest'.
However, I do not understand where the rebalancing functions get the first argument 'current_weights' from. In the example code below, there is no clue about where values of this argument are generated.
Thank you very much for the help in advance.
% Read table of daily adjusted close prices for 2006 DJIA stocks
T = readtable('dowPortfolio.xlsx');
% Prune the table on only hold the dates and selected stocks
timeColumn = "Dates";
assetSymbols = ["BA", "CAT", "DIS", "GE", "IBM", "MCD", "MSFT"];
T = T(:,[timeColumn assetSymbols]);
% Convert to timetable
pricesTT = table2timetable(T,'RowTimes','Dates');
% Initialize the strategies with 30 weights, since the backtest
% data comes from a year of the 30 DJIA stocks.
numAssets = size(T,2)-1;
% Give the initial weights for both strategies equal weighting. Weights
% must sum to 1 to be fully invested.
initialWeights = ones(1,numAssets);
initialWeights = initialWeights / sum(initialWeights);
% Define the Transaction costs as [buyCosts sellCost] and specify the costs
% as decimal percentages.
tradingCosts = [0.0025 0.005];
% Both strategies rebalance every 4 weeks (20 days).
rebalFreq = 20;
% The equal weight strategy does not require any price history data.
ewLookback = 0;
% The "chase returns" strategy bases its decisions on the trailing
% 10-day asset returns. The lookback window is set to 11 since computing 10 days
% of returns requires the close price from day 0.
chaseLookback = 11;
% Create the equal weighted strategy. The rebalance function @equalWeights
% is defined in the Rebalance Functions section at the end of this example.
equalWeightStrategy = backtestStrategy("EqualWeight",@equalWeight,...
'RebalanceFrequency',rebalFreq,...
'TransactionCosts',tradingCosts,...
'LookbackWindow',ewLookback,...
'InitialWeights',initialWeights)
% Create the "chase returns" strategy. The rebalance function
% @chaseReturns is defined in the Rebalance Functions section at the end of this example.
chaseReturnsStrategy = backtestStrategy("ChaseReturns",@chaseReturns,...
'RebalanceFrequency',rebalFreq,...
'TransactionCosts',tradingCosts,...
'LookbackWindow',chaseLookback,...
'InitialWeights',initialWeights)
% Create an array of strategies for the backtestEngine.
strategies = [equalWeightStrategy chaseReturnsStrategy];
% Create backtesting engine to test both strategies.
backtester = backtestEngine(strategies);
bt = runBacktest(backtester, pricesTT);
equityCurve(bt);
%%%%%%%%%%%% REBALANCING FUNCTIONS %%%%%%%%%%%%%%%
function new_weights = equalWeight(current_weights,assetPrices) %#ok<INUSD>
% Invest equally across all assets.
num_assets = numel(current_weights);
new_weights = ones(1,num_assets) / num_assets;
end
function new_weights = chaseReturns(current_weights,assetPrices)
% Set number of stocks to invest in.
numStocks = 3;
% Compute rolling returns from lookback window.
rollingReturns = assetPrices{end,:} ./ assetPrices{1,:};
% Select the X best performing stocks over the lookback window
[~,idx] = sort(rollingReturns,'descend');
bestStocksIndex = idx(1:numStocks);
% Initialize new weights to all zeros.
new_weights = zeros(size(current_weights));
% Invest equally across the top performing stocks.
new_weights(bestStocksIndex) = 1;
new_weights = new_weights / sum(new_weights);
end

Risposte (1)

Brendan
Brendan il 16 Ago 2021
All of the arguments to the rebalance function are passed in by the backtest engine. That includes the current_weights, the moving window of asset price data, and the moving window of "signal" data (if one is provided by the user when calling the runBacktest function).
When the user-supplied strategy rebalance function is called by the engine, the engine object will pass in the current portfolio weights in the current_weights parameter.

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by