Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Serious bug with monthly rebalance?

I think there is some serious bug associated with algorithms with monthly rebalance, or may be rebalance() in general, or maybe with algorithm that only rebalance once a year.

When you run the algorithm year by year, it would generate much smaller gain vs. if you run it over 10-15 years. There seems to be additional cash credited to the total balance every month for unknown reasons (and that cash is not dividend from stocks).

For example: in the algorithm listed here, if you run it from 2003 to 2015, the gain in year 2013 alone is about 75%, but if you run it just from 12/31/2012 to 12/31/2013, it is 57%. Similarly, the gain in year 2009 is 142% if you run in 12 years, but only 107% if you run it for that single year.

Also, when I run it from 2003-01-04 to 2009-12-31, it is 515% gain. When I run it from 2009-12-31 to 06-24-2015, it is 93% gain. So the total gain is (5.15 + 1) * (.93 + 1) - 1 = 1087%. But when you run from 2003-01-04 to 06-24-2015, the gain is 1398%.

Imagine if you trade live with this backtest result, it could be disastrous.

So I think this bug should be verified and should be fixed as soon as possible.

7 responses

if you run year by year you have to set the starting capital properly or it won't compound

Thanks for the comments, but why wouldn't it compound?

Yes, I think there is some sort of bug which gets worse in longer backtests. I have been seeing anomalous cash flows too:

https://www.quantopian.com/posts/debugging-anomalous-dividends
https://www.quantopian.com/posts/anomalous-appearance-of-cash-during-backtest

I recall someone else mentioning it was worse in longer backtests, which leads me to believe that it's a bug with their split-adjustment of dividends or something, but I haven't heard anything concrete. I emailed Dan about this a few days ago with my algo and some analytics in a research notebook which highlighted the error.

Thanks for the confirmation. I am pretty sure the cash is not dividend since I carefully compared the total balance between two scenarios:

Scenario A:
Don't start buying into any stock until January, 2013. But run the test starting from 01/04/2012. So it will keep 100% cash for a year.

Scenario B:
Don't start buying into any stock until January, 2013. But run the test starting from 2013 directly. (No cash balance for a year).

In these two scenarios, you will find that Scenario A has extra cash flows (about 3% - 4% a year) than Scenario B. I also observed dividend payment in B, so the cash flow in A is not dividend. It is not interest on cash either, because if it is, you would see some gains in year 2012 as well.

That's a huge issue and i've seen a similar anomaly on some of my backtests where cash is randomly ejected. FED bailout perhaps? lol

I've spent a lot of time on this algo, and I haven't found any bugs in the backtest yet. There are no cash additions or subtractions that I can't explain.

Mingran's original post includes this note:

Also, when I run it from 2003-01-04 to 2009-12-31, it is 515% gain. When I run it from 2009-12-31 to 06-24-2015, it is 93% gain. So the total gain is (5.15 + 1) * (.93 + 1) - 1 = 1087%. But when you run from 2003-01-04 to 06-24-2015, the gain is 1398%.

I think I understand why this is true. In my testing, I have found that the algorithm behaves differently depending on what day the algorithm starts. This difference takes two different forms.

  1. The algorithm rebalances annually, So, if you start on December 29, your algo rebalances every January; on Jan 29, then you rebalance every February. You get different results depending on which month you start.
  2. Even if you run multiple backtests that start on the same "cycle," it still isn't consistent. For instance, I kicked off one backtest that starts on Jan-03-2003, and a second backtest on Jan-29-2010, which is the February cycle, closest to Mingran's original post. Then, I check the holdings of the two backtests on Feb-16-2010. On that date, the longer backtest has 23 positions. The shorter backtest only has 20 (the same are in both tests, but the longer test has 3 more).

I didn't dive deep enough into the algo code to understand why the number of positions varies. But the positional differences very much can explain the differences in returns across multiple backtest runs. When the dates change, the behavior of the algorithm changes.

FYI, I did my study by running the following lines in the research environment. The backtest_id's are my own, and won't work for you, but you get the idea.

# 'full' refers to the backtest starting in 2003, 'second_six' refers to the one that starts in 2010 (bad naming convention, sorry)  
full = get_backtest('558dc24a175d18124f1f7252')  
second_six = get_backtest('558dd517f47f39123f134eef')  
january_2010_positions_full = full.positions.loc['2010-02-16']  
january_2010_positions_second = second_six.positions.loc['2010-02-16']  
january_2010_positions_full['sid'].isin(january_2010_positions_second['sid'])  
find_the_extras = january_2010_positions_full[~january_2010_positions_full['sid'].isin(january_2010_positions_second['sid'])]  
Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Dan, if you only enter positions starting Feb, 2010 (filter year to be 2010 only), then run two experiments, one start backtest from 2009-01-01, the other start from 2010-01-31, you might see the bug I talked about more clearly.