Back to Community
ETF rebalance monthly based on momentum

Hello
I am trying to replicate this:

I have done the first step: Select the best performing ETF in a basket based on the returns of the last 60 days and then buy them (for now i haven't done the weight part yet) and rebalance monthly
Do you think that this code is efficent? i have tryied to avoid loops by using only pandas festures.
Thank you very much for your help

Clone Algorithm
52
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5638efef583e99110cc5982c
There was a runtime error.
21 responses

Thanks for sharing this, I am looking for similar sorts of systems.

Glad to help, there was an error in the previous post because the weight for each stock was not correclty calculated,
In next posts I am giong to weight each stock differently using the minimum variance portfolio.

Clone Algorithm
52
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5639e64d69511610fdea65b0
There was a runtime error.

i'm having a hard time implementing the markowitz weights to obtain minimum variance, i am using this can someone please help me to figure out why these weights gives such a different resoult?
Thanks!

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 563b39d6942d3710f8df581e
There was a runtime error.

Because it is a totally, wholly different system! You can't go from equal weighted to vol weighted and expect the same results. I am currently working on both myself and will post when I am a little further down the line.

And anyway what are you optimising for? Simply the lowest vol along the EF curve by the looks of it. Equal weighting is far from this. You need to include constraints and then targets. EG for a 15% vol what is the best return available......for a 5% vol what is the best return available and so forth. I don't know if it help but here is a link to a very good paper:

https://quantstrattrader.wordpress.com/2015/06/05/momentum-markowitz-and-solving-rank-deficient-covariance-matrices-the-constrained-critical-line-algorithm/

There is a Critical line implementation here but it seems horrible over complex to me:
https://www.quantopian.com/posts/critical-line-algorithm-for-portfolio-optimization

Ilya Kipnis' work in R looks a lot more straightforward but I need to download R and work through it.

Thank you Anthony.
Actually the goal of my algo is:
1) select n best performance stocks
2) construct a portfolio looking for the lowest ptf volatility along the Efficent Frontier curve.

It seems to me that this example does exactly the point number 2, there is also a notebook here
Now although i have understood what a minimum variance portfolio is, i have a hard time to understand the mechanism of the cvxopt library used to calculate weights.. so actually the way weights are calculated is a not very clear to me, but when i've looked at the example i tought that it was like a 'one size fits all' method to obtain the minimum variance.
The problem is that i was expecting just a volatility reduction and not such a totally different result.

Momentum is built into the Markowitz CLA solution anyway in the covariance matrix. You don't need/want to code for it separately. You just choose the portfolio further up to the right which will contain a greater weighting to higher momentum / vol stocks anyway.

Thus it becomes a case of simply telling the algo to find you the portfolio on the EF curve which gives you the desired vol/ return.

At least , this is my interpretation!

Unconstrained mean variance optimization is going to give you terrible results. You should first of all consider not using any estimates of mean returns, if you are doing so; try setting those all to zero. Second, you should do a Monte Carlo bootstrapping, maybe 100 iterations and covariance samples, that should give you more stable and reasonable results. I posted a notebook with some bootstrap code a few weeks ago.

Thank you Simon! , it must be this one: Bootstrapping volatility-standardized asset weights
very useful thanks

That is a very contrary view to that contained in the paper I referenced where the back test results were very good indeed. Mean return estimates are merely a forecast that the next month's returns will equal the returns of the lookback period.

So they should not be set to zero. The criticism is usually about using the very long terms returns as an estimate. In Ilya Kipnis' very attractive scheme these returns are recalculated each month based on a three to 12 month look back period.

Anyway, that is the scheme I am working on and that is the only way you will be able to incorporate momentum into the mean variance model.

I am not sure how clear I have been but in any event read the paper on Ilya's website. It is very clealry written. Markowitz suks if you use long term returns/estimates but rocks if you use the 3 to 12 month return as your estimate for the next period which is helpful in predicting momentum.

Well, I hope we are all successful! My experience with unconstrained minimum variance with mean return estimates has been that it leads to extreme, degenerate portfolios.

Doesn't have to. Use constraints. No shorts. No position except your bond ETF to take an allocation of more than 10% or whatever of the portfolio. That sort of stuff.

Sure, but after enough of those constraints, you may as well just equal weight. The bootstrapping works well to chop down the noise of the optimizer, worth a look.

Ilya's results suggest this is far from the case. But until I have coded it, I really could not say. To me, it seems very well worth looking at. But each to his own. When I get round to it, I'll post the algo.

Take 8 uncorrelated ETFs
Select best 4 assets based on 6 months momentum
weigth them on volatility over last 60 days
rebalance weekly

well, it does not make money at all, but volatility is low

Clone Algorithm
21
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56afc2dea2e78a129bf8721c
There was a runtime error.

Take two negatively correlated ETF: XLP and TLT.
Fixed proportion : 0.55-0.45.
Re balance monthly.
Plain vanilla fixed ratio stock-bond portfolio making money and less volatile.

Clone Algorithm
97
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 56aff5703891da11734ee2bc
There was a runtime error.

Thanks Vladimir, i'll give a go to that kind of allocation systems for a while, actually i'd like something that include OIL (or commodities in general) and GOLD since they could have a lot of upside potential for the future