santoshlite / Empyrial

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

rebalance has a bug #71

Closed rgleavenworth closed 2 years ago

rgleavenworth commented 2 years ago

When you set up quarterly rebalance with only one ticker, the strategy and benchmark show different values. This is a bug. They should be completely equal.

The codebase below reproduces the issue. The EOY returns and the timeseries plot of Cumulative returns vs benchmark show that the strategy and benchmark are divergent.

from empyrial import empyrial, Engine portfolio = Engine(
start_date= "2021-01-01", portfolio= ["BTC-USD", "GOOG"], weights = [1, 0.], #equal weighting is set by default benchmark = ["BTC-USD"], #SPY is set by default rebalance = 'quarterly' ) empyrial(portfolio)

santoshlite commented 2 years ago

Hey, thanks for raising that issue.

I checked the order book and it doesn't seem like the problem comes from rebalancing. Seems strange, I'll investigate it, thanks for reporting it!

Anyway, rebalancing one stock has no real purpose

rgleavenworth commented 2 years ago

I'm wondering if there is a logical bug somewhere in rebalance. The portfolio as listed above should be identical in performance with or without reblancing enabled. The intent to run the test was to ensure that rebalance doesn't introduce a different result. Clearly, it does so there is a bug in the code somewhere. I know that rebalancing a portfolio with one stock isn't useful but the goal was to do a regression check.

rgleavenworth commented 2 years ago

I've solved the bug and submitted a pull request.