Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Some Changes in the Quantopian API

Hello everyone,

We shipped some code today that changes the way the IDE works. Technically this affects every algorithm, but we did it in such a way that you will hopefully not be inconvenienced.

What Changed

  • The handle_data function has reversed the order of its arguments.
    All of the existing algorithms used handle_data(data, context).
    Going forward the order is handle_data(context, data). When you
    build your algorithms, you'll get a very clear error message if you
    have the old order. All of the examples and documentation have
    been updated (we hope!).
  • The portfolio object has moved from data to context. If you're in
    the habit of writing data.portfolio, you should change that to
    context.portfolio. Again, when you build your algorithm, you'll get a
    very clear error message if you have the old object.
  • The data.available property has been removed. Few people were
    using the available property, but for the few who try, you'll get a
    good error message. Use this form instead:
if sid(int) in data:  

Why We Made The Change

These changes are the result of a combination of user feedback and some internal architecture work we're doing. We're very aware that breaking changes are a pain, and we don't plan on doing this often (or ever?) in the future. But we're pretty convinced that these are the right thing to do, and we wanted to 'bite the bullet' and make the changes before we get bigger. We're in beta still, and part of beta software is sometimes realizing you made a mistake!

As always, we welcome your feedback. If you have any thoughts on the changes we're making, changes we should make in the future, or suggestions on how to manage the change process better, please let us know. Replies below or email to [email protected] are most welcome.

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.

5 responses

Hi Dan,
No problem. What happens to the posted algos? I can fix mine but can I do it without re-posting?

Hi Sanz - unfortunately, the posted/shared algos have the old datastructure. You have to re-post, basically.

We looked at ways to migrate the existing data and we didn't find a good way.

Again, I apologize for the inconvenience. This is not a habit for us! We hope it's a once-only thing.

Hello Dan,

A few more details/examples of how to use this would be helpful:

if sid(int) in data:  

In the discussion https://www.quantopian.com/posts/help-with-runtime-error, Fawce helped me to fix an error I was getting. How would I patch up the code below, now that data.available is obsolete? Generally, are there special coding considerations for thinly traded securities?

import datetime  
import pytz

def initialize(context):

    #Full Backtest outputs "There was a runtime error"  
    context.stocks = [sid(41290),sid(41425),sid(39479),sid(33972),sid(41159)]  
    #Full Backtest completes without errors  
    #context.stocks = [sid(8554),sid(19920),sid(22739)]  
    context.price = {}  
    context.volume = {}  
    context.max_notional = 1000000.1  
    context.min_notional = -1000000.0  
    utc = pytz.timezone('UTC')  
    context.d=datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)

def handle_data(context,data):  
    notional = 0.0  
    for stock in context.stocks:  
        if data.available(stock):  
            price = data[stock].price  
            volume = data[stock].volume  
            notional = notional + context.portfolio.positions[stock].amount * price  
            tradeday = data[stock].datetime  
            log.debug(stock)  
            log.debug(tradeday)  
            log.debug(volume)  
    if (context.d + datetime.timedelta(days=1)) < tradeday:  
        log.debug(str(notional) + ' - notional start ' + tradeday.strftime('%m/%d/%y'))  
        context.d = tradeday  

@Grant,

Thanks for bringing in a specific example.

You would just replace if data.available(stock) with if stock in data:. I did a quick build/backtest cycle on your code with the fix and verified that it works.
The in operator is a more "pythonic" way to check for data availability, so now our data api is consistent with the way that other python classes/packages. Conceptually and practically, there shouldn't be any difference.

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.

Thanks Fawce,

Yep, it works on both the mini and full backtests.

Grant