Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
AttributeError: 'NoneType' object has no attribute 'get_history'

Hi,
this part of code works in Zipline, not in Quantopian (AttributeError: 'NoneType' object has no attribute 'get_history')
context.parameterA = 3
pricesA = history(context.parameterA, '1d', 'price')

In Qunatopian I must write parameter directly into like pricesA = history(3, '1d', 'price')

Thanks for help
Peter

18 responses

Peter I was unable to recreate the exception you are experiencing, could you post a little more code? It may provide more context to where the error is originating from.

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.

For example here:

from datetime import datetime  
import pytz  
import talib

def initialize(context):  
    context.stock = [symbol('CSCO'), symbol('IBM'), symbol('MSFT')]  
    context.start = datetime(2015, 6, 1, 0, 0, 0, 0, pytz.utc)  
    context.end = datetime(2016, 1, 1, 0, 0, 0, 0, pytz.utc)  
    context.i = 0  
    context.parameterA = 3  
    add_history(context.parameterA, '1d', 'price')  

def handle_data(context, data):  
    context.i += 1  
    if context.i < context.parameterA:  
        return  
    pricesA = history(context.parameterA, '1d', 'price')  

Peter,

While necessary in stand alone Zipline, add_history is not needed in the IDE, we take care of that for you. Removing that line of code should return your code to working properly.

Thanks James for help, it works.
On the other side, it means I need to adjust code for using in Quantopian. Is there any workaround how to automatically use/skip this line based on env (Zipline/Quantopian)?

You can use the get_environment function: https://www.quantopian.com/help#api-get-environment

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.

Great function, problem it is not working in Zipline.
I got error: global name 'get_environment' is not defined and after importing

 from zipline.api import get_environment  

I am getting ImportError: cannot import name get_environment

hmm... I ran this in Zipline and the Quantopian IDE and it worked for me. Can you check that you're on the latest version (0.7) of Zipline? Or share more details of your setup?

I am using Anaconda Python (32-bit on 64-bit Windows 7) with latest Zipline and running the script from Spyder in IPython console...

Or should I use another Python distribution not to have this problem with Zipline on Windows machine?

Same problem here: Linux, ubuntu 14.10 in Spyder
But things are OK with a pip install ...

to Florent: you mean not to install using conda but pip?

on ubuntu 14.10 and 15.10, I mean installing with 'pip' is OK, but installing from the github repo using 'python setup.py develop' does not work, at least for me, although it was OK with previous versions.

I compile cython, numpy and scipy locally for optimization, then pip install zipline, yet I'd prefer to directly use the github repo (I haven't spent much time on it though as the zipline engine from pip is good enough).

Everything I do happens through Spyder and I can say it works very very well.
best,

Hi,
after using pip install zipline instead of conda install -c Quantopian zipline still getting the same ImportError: cannot import name get_environment
In both cases it installs version 0.7.x instead of latest 0.8.x

after a sudo pip install zipline, everything seems fine at v.0.8: Here is what I get from within spyder:

Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.

Imported NumPy 1.8.2, SciPy 0.13.3, Matplotlib 1.3.1

Type "scientific" for more details.

import zipline
zipline.__version__
'0.8.0rc1'

Hi,
after 2 months I would like to go back to the problem. I am still getting ImportError: cannot import name get_environment with Zipline 0.7.0 (np18py27_0) on 64-bit Windows with 32-bit Anaconda Python.
Thanks
Peter

Unfortunately the release version of zipline (0.7) is broken. Until we release 0.8 you can try to install the development version using:
pip install -U https://github.com/quantopian/zipline. It might be difficult on Windows though.

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.

to Thomas Wiecki: What does it mean ,,broken''? Is it the reason for get_environment error?

Suggested command pip install -U https://github.com/quantopian/zipline is not working on Windows (due to unpack and file format determination error).

Peter: Unfortunately it's quite far behind the zipline development version and the Quantopian API. So what I meant is that the API is stale and doesn't work as expected in various places. On top of that, I think there are some other bugs / incompatibilities, but that is only what I gathered from the mailing list.