Back to Community
What does def initialize(context) pass do?

This is probably such a simple question but I can't seem to find any resources that can help me understand! I'm still new to this so any guidance would be greatly appreciated!

4 responses

Hi Hannah,

Welcome to Quantopian! The initialize function is the only function that must be defined in an algorithm on Quantopian. Here are a few resources to help you learn more about initialize(context):
- Getting Started Tutorial (lesson 5)
- User Guide
- API Reference

At a high level, initialize(context) is where you run one-time logic like scheduling functions and attaching pipelines. If these are new concepts, I'd recommend checking out the Getting Started Tutorial to get a walkthrough of researching and developing an algorithm on Quantopian. Beyond that, we recently released new documentation that you might find helpful when exploring the platform.

I hope this helps!

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.

Hi Jamie. Thanks for the help. What specifically does context do in initialize(context)? Why not just have initialize()? I note the similarity to class inheritance e.g. Student(Person):
pass
but I don't know how this translates (or if at all relevant) when we're not talking about classes.
Thanks

@Bill Edwards. Good questions.

initialize is a Python function. The def keyword indicates what follows is a function name and any parameters one wants to pass to the function enclosed in parenthesis. It is a bit similar to a class definition but probably best to not think of it that way. There are a number of resources online which show how to define functions and pass parameters. Maybe check out this one.

One thing to realize is the code one writes for an algo isn't the entire program. In the background there is all the code to handle looping through every minute of every day of the backtest. It handles the mechanics of filling orders, updating the portfolio, and of course calling user defined code. This background program is called zipline. When zipline first starts it creates a TradingAlgorithm object to store all the info about the algo. A reference to this object is passed as a single parameter to the initialize function. By convention this is called "context". I wouldn't recommend it, but one can call it whatever one wishes (eg "my_algo"). It's generally not a good practice in Python to create global variables. For this reason, the TradingAlgorithm object reference is passed to all the user defined functions (eg initialize) . This object has attributes and methods so your code can access all the 'global' things like the portfolio, data, etc. It's all stored in the context object. Additionally, one can add attributes to this object. This is the primary way to create user defined 'global variables'.

So, all that said, context is just a reference to the TradingAlgorithm object. It's passed to the initialize function so users can access, and create, 'global' data to use in other user functions such as before_trading_start or scheduled functions.

Hope that helps.

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.

hi Jamie
did the tutorial changed since 2019 because in the link Getting Started Tutorial (lesson 5) is just have one lesson
sorry for my bad English but i think you understand me