cvxgrp / cvxportfolio

Portfolio optimization and back-testing.
https://www.cvxportfolio.com
GNU General Public License v3.0
969 stars 253 forks source link

The result of backtesting 30 years is different from 5 years? #159

Open jacktang opened 4 months ago

jacktang commented 4 months ago

Hello, I play the etf demo and change the backtest_many code as below (for 5 years time window) since my laptop is not very high performance.

ress = sim.backtest_many(
    [make_policy(*key) for key in keys], parallel=True, start_time=pd.Timestamp.today() - pd.Timedelta(f'{365.24*5}d'))

And the plots

image

And the original one for around 30 years time window

image

The portfolio value trend is very similar, but the largest 7 assets are quite different. Especially for cash. Can you explain why it changes so much? Thanks!

enzbus commented 3 months ago

That's a good question, I hope it's not because Yahoo Finance data has changed in the meantime. Can you run the whole thing to try (it's monthly re-sampled, so it shouldn't be too bad). In general every back-test is path dependent so it will change based on the starting conditions, but this does seem like a large difference.

jacktang commented 3 months ago

@enzbus I run the demo with 30 years time window, and the result is almost the same with the example page, below is the largest_growth_figure

image

enzbus commented 3 months ago

Yeah, if read the code carefully you'll see that example actually runs many back-test sweeping over hyper-parameters, so each one will have different allocations.

jacktang commented 3 months ago

It is interesting :) I'd like to read the code deeply. BTW, is there any verbose option for debugging or output more detail logging?

enzbus commented 3 months ago

Yes, adding this two lines at the start should suffice:

import logging
logging.basicConfig(level='INFO')