Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Why can't I use context.portfolio in initialize()

I would like to use the initial capital base for the backtest or live execution as the max exposure in the algorithm (long or short) without having to change the starting cash in the algorithm and match that to my code.

def initialize(context):  
    context.MAX_NOTIONAL = context.portfolio.starting_cash  
    context.MIN_NOTIONAL = -context.portfolio.starting_cash  
    context.MAX_SHARES = 100  

But, this gives me a Build error:

19 Error Runtime exception: KeyError: 'portfolio'

5 responses

I would like to know the answer to this as well. Bizarrely it works inside 'handle_data' which is, of course, completely pointless.

P.

You can't reference the portfolio object inside of the initialize function. So you can just add code to handle_data that only executes on the first time the function is called that sets your notionals. Does that help?

def initialize(context):  
    context.security = sid(8554)  
    context.warmed = False  
def handle_data(context, data):  
    if not context.warmed:  
        context.MAX_NOTIONAL = context.portfolio.starting_cash  
        context.MIN_NOTIONAL = -context.portfolio.starting_cash  
        context.MAX_SHARES = 100  
        context.warmed = True  
    # more code  
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.

You can't currently reference the portfolio object inside the initialize function, but that's not very user-friendly. Thanks for the heads up, we will be looking into how to improve this.

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.

Gus and Jean,

Thanks for taking a look at this! I am glad that the community gets to shape the product in some ways which is excellent for a successful start-up.

I will be implementing the idea that Gus suggested until the context.portfolio object can be accessed inside the initialize()

My understanding is that "context" is an empty structure upon start, when initialize is called. Thus, you can set values to be passed to the handler, but you can't access values inside it. I agree there are certain parameters that you would hope would have already been set by the executor, but Gus does have a nice double-init method, so to speak. Good stuff!