santoshlite / Empyrial

An Open Source Portfolio Backtesting Engine for Everyone | 面向所有人的开源投资组合回测引擎
https://empyrial.gitbook.io/empyrial/
MIT License
914 stars 124 forks source link

Error when rebalancing with only one stock #65

Closed atobiese closed 2 years ago

atobiese commented 2 years ago

Hi, I have tried to reproduce test results and simulated one single stock over time by forcing the weight distribution as shown below: tickers = ["stock1", "stock2"] weightsnew = [1.0, 0.0] no optimizer is used, so just using the quantstats calculations of ratios and returns. In the next example, we do the same but with a yearly rebalancer. The thing here is that the results should be exactly the same. There seems to be a slight error in the returns calculations over time, which turns out to be bigger with more rebalancing.

I will have some more look at it, and update if I find the bug. Btw great work!

santoshlite commented 2 years ago

Hey,

Sorry, for the late reply? There is no purpose in rebalancing only one stock. The weight will stay the same. Hope it helps!

atobiese commented 2 years ago

Hi, of course, but the point I was trying to make is that there is a math error. The end value is NOT the same here. This means that when using more than one stock), the rebalancing will create large errors. The test for this is simply rebalancing with two stocks, where the second weight is zero. The result should be exactly the same, since a perfentage of zero is zero. So since this result is different than the single stock, the math is not correct.

santoshlite commented 2 years ago

Sorry!! My fault, I misinterpreted the issue. If you find the bug, pin me! I'll also look at the code to see where is the problem coming from 😟 Thanks for reporting this issue!

atobiese commented 2 years ago

I haven't looked at the code for a while, but it could be the precision of the calculations in pypfopt, resulting in rounding erros outside after several balancers. Errors grow fast as I remember. May need higher precision. Could be a summation error etc. I'll have a look if I get time. Regards

santoshlite commented 2 years ago

This https://github.com/ssantoshp/Empyrial/pull/72 pull request solved it You can install Empyrial 2.0.1 and it should work!