Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Strange Bug - for loop iterates on list of 4 items, 5 times

When this code is executed with these dates, on 11/24/2004 the for loop at line 111 executes on the context.portfolio.positions dictionary. The dictionary contains 4 SIDS, however the for loop iterates 5 times repeating UNH. Anybody know how this could happen?

3 responses

Hi Chris,

This is a real mind bender. I had to step through our code in a debugger to figure out what was happening here. During a single invocation of handle_data, the positions collection should be static. With respect to the keys and the values, it is. However, when you place an order inside a loop that is iterating over positions, behind the scenes the sort order of the keys changes. As a result, the length of the list remains the same, but the order gets shifted and the iterator restarts at 0. It will take some more time to fix properly, but you can avoid the problem for now by creating a static list of the keys from positions and iterating over those. That's what I did in the attached backtest.

I've filed a bug for the dev team to fix the underlying issue.

thanks,
fawce

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.

The underlying bug here is fixed - shipped last night.

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.

John/Dan, Thanks for the quick turnaround and a great system here.