Open funnym0nk3y opened 3 years ago
That sounds really nice! Does this also calculate predicted transaction costs and allow to trade between transaction costs and error?
Does this also calculate predicted transaction costs and allow to trade between transaction costs and error?
Usually the transaction costs can be modelled as a more or less simple formula, right? This could be set for each investment vehicle. The rebalancer would then calculate the cost for the rebalancing. I don't know which algorithm the rebalancer is using for minimization. I just recently learnd in university about the simplex algorithm which could give a solution. Let me descirbe it, if it is too obvious, if the current rebalancer is more advanced or if there are any problems with that idea, please comment. I'd like to learn!
But mathematically speaking, we could formulate the rebalancing problem as a minimization of costs. How those costs are defined would characterize the rebalancer. Say we choose the relative deviation from the set value to be the cost. Then adding a cost value derived from the actual cost could be added as another cost factor with a weight.
But those are just my thoughts. I've never used java so I'm a little lost with the current implementation of the rebalancer.
Also there are several optimizer implementations like OptaPlanner and IPOPT.
@funnym0nk3y I don’t quite see how to solve the Rebalancing with transaction cost minimization problem can be solved with the Simplex algorithm (or any other linear optimization problem solver)
First of all, there is the problem that most brokers have non-linear transaction cost (i.e. all brokers where you pay a flat fee).
Second, a linear optimization problem solver can only find the cost-minimal solution among all possible solution, i.e. it would only be usefull for the situation where the rebalancing problem is ambigious. (If there is only one rebalancing solution, it would always output this. There is no way to model a tradeoff between rebalancing precision and transaction costs. If there is no exact rebalancing solution, the algorithm would fail.)
Finally, for an ambigous rebalancing situation the result is not interesting. The optimal solution is always to exclude those shares that have an ambigous result with the highest transaction costs. For all brokers I now, the transaction costs are the same for each share (buying 100$ worth of shares of company A has the same cost as buying 100$ worth of shares of company B), so this usually simplifies to exclude any of the ambigous securities from the rebalancing. This can be done manually with the current rebalancer as well, and I think this is better than using an automatic approach (like with the simplex algorithm) that would in the end just select a random set of securities for the rebalancing.
TLDR: Any interesting variant of „minimizing transaction cost” can not be modelled as linear optimazation problem and thus not solve by the Simplex algorithm or anything like that.
By the way, the current implementation uses the singular value decomposition (SVD) to solve the rebalancing problem. When there is no exact solution, it minimizes the transaction volume under the side condition that you buy as much as you sell in total.
@RomanLangrehr Yeah, it is true that a linear optimizer can't solve this problem adequatly. The simplex algorithm was just my introduction to optimization in general, I did not made that clear in my previous comment.
Regarding your first point: When considering transaction cost there are two models I can think of that could be called "flat fee". First of all, there are flatrate brokers, where you can trade as much as you want for a fixed fee. I would not consider those as transaction costs but as subscription. The transaction cost would be zero in such a case. On the other hand paying a fee no matter how much or what is traded simply is a constant which could be used as a constraint. This would lead to my second thought about modeling transaction costs: How to deal with not continously differentiable cost functions? For that I haven't found a solution yet, but it seems that smoothing is the way to go there.
Regarding your second point: I politely disagree with your statement: That totally depends on how you model your gain function. I will elaborate this later in the model I thought up.
Regarding your third point: That is totally true if one security has weight 100% in only one group. But with the current rebalancing feature one can assign a security partially to multiple groups. That makes cost optimization not trivial as there could be two solutions like sell some A and buy some B or just buy some C.
What I have come up so far (please excuse the poor math display): We want to minimize the delta of each category. So the gain/cost function is the squared sum of the deltas. But we don't want to restructure the complete portfolio for the last 100th of a percent as this could lead to unreasonably high costs. Therefor the gain function is the summ of all squared deltas plus the transaction costs multiplied by a weight. This is our tradoff tuning parameter for transaction costs vs accuarcy.
Each group delta is calculated as the (normalized) sum of each security minus the rebalancing delta (our variable we are optimizing) multiplied by its group weight minus the wanted group sum.
The transaction costs are simply the sum of each rebalancing delta multiplied by the costs each account has for that transaction.
Our constraints are (this is, where we could implement more functionality like just adding amount x to the portfolio, or just taking money out): The sum of securities after rebalancing should be more or less equal to what is available. Furthermore one could set the constraint, that each transaction delta should be positive or negative.
There are mistakes in that model for sure, bnut I haven't found them yet. All in all that should yield a quadratic (depending on the transaction cost model) optimization problem. That could be solved with a general purpose nonlinear solver. I read somewhere that SLSQP could fit.
@funnym0nk3y First some clarifications: With flat fee I meant that the transaction cost is a fixed amount (independent of the transaction volume) and for my second and third point I assumed a different model: Namely that the transaction costs are linear and the rebalancing constraints are the constraints of the optimization problem
Your model sounds interessting and I don’t see a significant problem here. Just some comments:
We want to minimize the delta of each category. So the gain/cost function is the squared sum of the deltas.
Do you mean each category or each lowest-level category? Because if you would minimize the delta of each category, this would have the strange effect that when you have a “deeply nested” category, it will cause higher costs when it is not balanced exactly (because the error will also count towards each upper level category). The rebalancer I implemented in #2268 uses one constrained per lowest-level category, except when this category is empty (or contains no securities included in the rebalancing.)
Furthermore one could set the constraint, that each transaction delta should be positive or negative.
I don’t see what you mean by this. This would allow only sell or only buy transaction and then an rebalancing is definitly not possible.
I am not familiar with SLSQP (or non-linear optimization problems in general), but I presume this approach will be quite complicated and will require a lot of prototyping to get usable solutions, because SLSQP will only find approximative local minima. Personally, I don’t think it is worth the effort.
@RomanLangrehr Well, I haven't thought about how to deal with nested groups yet. But I think errors in subgroups can cancel each other out. But you are right, that if we have two groups, each with a single nested subgroup which has another nested subgroup, up to an abitrary depth, the one with deepest nesting will always win. But that could be handled as a special case where the parent groups are not taken into the equation.
Yes, I exactly mean that. The rebalancer could have a mode, where all the rebalancing is done by just selling or buying. I imagine a usecase where I have amount x to invest and my asset allocation is a little off. I can make use of the fact that I have to pay transaction fees anyway and bring back the allocation to the ideal state "for free". The same goes for taking money out of the investment portfolio. I tell the rebalancer my sum into or out of the investment and it tells me how I get the most rebalancing effect out of that.
Whether it's worth the effort or not depends I think. If 0,2% of return could be gained, that would be more than any cash account I know of. furthermore I don't think that SLSQP should be implemented by hand but instead a library should be used. And there are a lot! Maybe after my exams are over in two months I can cobble together a MVE in python.
@buchen Would it be possible/fit into the bigger picture of PortfolioPerformace if each security/deposits account has some way of modeling transaction costs? A rebalancer could use that information to make optimized recommendations across multiple deposits accounts. My current understanding is that this would not make the rebalancer much more complicated.
Is your feature request related to a problem? Please describe. When a portfolio consists of multiple ETFs each with a distinct allocation per country/region the current rebalancing just calculates the delta based on its weight. For example: A MSCI ACWI has a smaller percentge invested in say europe than what I like. Adding to the MSCI Europe would lift the european allocation to its desired value. Now the rebalancing shows that for the world quota I need to sell like 10% of the ACWI ETF and to match the europe quota I need to buy 50% ACWI whereas it would be much more sensible to just buy MSCI Europe. I hope that example is clear enough
Describe the solution you'd like I think a more advanced rebalancing strategy should be implemented and just give one delta number for each stock.
Describe alternatives you've considered I considered a more granular approach with more stocks/ETFs but that adds to the cost.