I just learned a little Python for the first time last week. Be gentle.
KeyError: 'price_change'
There was a runtime error on line 34.
Not quite sure why this error is happening...can anybody help a newbie out?
I just learned a little Python for the first time last week. Be gentle.
KeyError: 'price_change'
There was a runtime error on line 34.
Not quite sure why this error is happening...can anybody help a newbie out?
Hi Michael,
I took a look and the KeyError is resulting because iterating over a pandas DataFrame (which is what context.my_universe
is in this example), iterates over the column headers. Since you used the update_universe()
function, your tradable universe is stored in the data
variable so you can just iterate over it (it's a dictionary). Check out this short tutorial video on the data
variable and your trading universe.
I've attached a version of your backtest with the fix. Let me know if you have any questions.
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.
Thank you so much, Jamie! I see what you did there and that makes sense, but your solution is buying the stocks in "data" rather than the stocks ranked by annual return which are in "context.my_universe". I do understand the issue with the dictionary vs. Pandas dataframe now though...just now quite sure how to get the ranked stocks into data parameter : (
Hi Michael,
Here's a version that goes through your sorted list. For reference, check out the documentation on pandas dataframes, it's worth a read! I should add that the rank won't matter if you assign the same weight to every stock, but I'll leave that to you to adjust to your preference!
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.