backtestStrategy: rebalance function keeps track of past & current open position

2 views (last 30 days)
Hi All,
backtestStrategy provides a great workflow for defining a strategy, which includes the rebalancing function.
As part of the EngineData object, WeightsHistory can be used to derive open positions, entry price and open pnl. Nonetheless ... the same computation takes place also within the backtestEngine's runBacktest function, which provides access to a Positions table for each strategy.
Hoping not having to re-invent the wheel, is there a way to pass the Positions table into the rebalancing function, for a given strategy? This would be a good fit for an additional field into the engineData structure. Thx

Accepted Answer

Andy
Andy on 27 Apr 2024
best way to do that currently is to use the engineDataList and within the rebalancing function, compute:
positions = engineData.PortfolioValue .* engineData.WeightsHistory;
then look for the relevant ticker's history.

More Answers (1)

Akshat Dalal
Akshat Dalal on 10 Apr 2024
Hi Andy,
I believe you could use the 'UserData' field in the rebalance function to pass the Position's Table as an input argument. When you're dealing with portfolio objects or any custom backtesting framework in MATLAB that supports the 'UserData' field, you can leverage this property to include additional information that the standard properties do not cover, such as a Position's Table or any other strategy-specific data you wish to carry through the rebalancing process.
To learn more about 'backtestStrategy' and the 'rebalance' function, you can refer the following documentation: https://www.mathworks.com/help/finance/backtest-investment-strategies.html
Hope this helps!
  1 Comment
Andy
Andy on 20 Apr 2024
Edited: Andy on 20 Apr 2024
Hi @Akshat Dalal, thanks for looking into this. Nonetheless ... can you elaborate a bit more, your suggestion relies on UserData having access to Positions' table, which is not the case.
According to https://uk.mathworks.com/help/finance/backteststrategy.html#engine_data_list_argument the below applies, please, highlight any inauracies in the below:
1) Both UserData and EngineDataList are optional backtestStrategy add-on's:
a) EngineDataList is an optional argument that would change the rebalancing function's signature from:
new_weights = rebalanceFcn(current_weights, etc.)
% to:
new_weights = rebalanceFcn(engineData, etc.)
b) UserData is a struct that can optionally be specified as:
bStrategy.UserData = struct(dummyVar = 0);
% and rebalanceFcn signature changes to:
[new_weights, userData] = rebalanceFcn(engineData, tableTT, userData)
And that's where I take you suggest userData gets augmented with elements from the Positions table. Nonetheless, within the rebalancing function, there is no access and no data related to Positions. EngineData only has:
Weights/History,
PortfolioValue/History,
FeesHistory,
TransactionCostHistory,
CashAsset,
DebtAsset
and nothing else.
So, there is no Positions data available within the rebalanceFcn, Positions property comes later, as part of the backtestEngine object. Obviously, the way EngineDataList was "designed" to keep track of PortfolioValue/History, etc., EngineDataList could also be enhanced to keep track of the Positions table ... hence this post.
How do you suggest the Positions table gets visible within the rebalanceFcn, without "creating" it and keeping track of it yourself (i.e., re-inventing the wheel)?

Sign in to comment.

Categories

Find more on Portfolio Optimization and Asset Allocation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!