Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern') // Something went wrong on our end. Sorry for the inconvenience.

Something went wrong on our end. Sorry for the inconvenience.
AttributeError: 'NoneType' object has no attribute 'tzinfo'
File test_algorithm_sycheck.py:36, in initialize
File test_algorithm_sycheck.py:39, in show_positions
File /zipline/algorithm.py:515, in get_datetime

10 responses

Hi Abraham,

When initialize(context) is invoked, there is no current datetime for get_datetime() to return - it runs before the test begins (this is also why any log statements generated in initialize show up as 1/1/1970). If you want to check the current (realtime) date, you will probably want to use "datetime.utcnow()" and then convert it to local time.

I'll file an issue internally about cleaning up the error message, it's definitely not obvious what is going on.

If you have any other specific questions about errors like these, feel free to use the Help -> Feedback option, or email [email protected] directly. Thanks for the report!

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 John,

Is there a reference manual from which I could learn this? https://www.quantopian.com/help doesn't seem to convey how to do this. I am happy to read manuals and examples before coding, but it feels like I'm flying blind.

Best,
Abe

Hi John,

Can you please provide actual code to do this?

Thanks,
Abe

Here's a thread that shows how to use the timezone function in pandas: https://www.quantopian.com/posts/diversified-portfolio-monthly-rebalance-for-live-trading

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 Alisa,

The above referenced url, unfortunately, does not have anything on time or timezone.

Best,
Abe

Hi Abe,

Below is example source code that I pulled (and slightly modified) from the algo referenced above. This is robust to rebalance the portfolio to 25% Apple stock between 10:30AM - 10:59AM. I could have said rebalance when the exchange time is equal to exactly 10:30AM, but this is brittle. What if the stock did not trade in that minute bar? Or what if something went wrong with my algo during that bar? This way, the algo will still order during a small window.

import datetime  
import pandas as pd


def initialize(context):  

    context.apple  = sid(24)  


def handle_data(context, data):  

    # Get the current exchange time in EST timezone:  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')

    # If it's 10:30am rebalance the account to 25% Apple stock  
    if exchange_time.hour == 10 and exchange_time.minute >=30:  
        order_target_percent(context.apple, 0.25)  

Hi Alisa,

That works in handle_data, but it does NOT work in initialize. There is some dependency that I can't get my head around, as I could really use some deeper documentation on context, data, initialize and handle_data. What makes "exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')" work in handle_data, but not in initialize?

Thanks,
Abe

Abraham,

I believe the issue is that get_datetime() uses the datetime stamps of the trade events. Since there are no trade events in initialize, the code only works in handle_data.

Grant

Grant,

I realize that, but that does not explain the internals or why it's so. I have many years of coding experience, but very little python experience, and even less Quantopian experience. I learn from manuals and from examples. I could really use some documentation, rather than hitting multiple failures. Time is too precious.

Abe

Hi Abe,

Best references available are:

  1. Quantopian API & Help Docs
  2. Quantopian FAQ
  3. Quantopian video tutorial
  4. Python general resources:
    Pandas documentation (highly recommended!)
    Codecademy
    Python tutorial

Of course you can also reach out to Quantopian support directly at [email protected] in addition to using the forums to get help from other members of the community. We know there's always room for better resources and documentation and we are working as quickly as we can to make the platform better and easier every day!

Best wishes, Jess

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.