Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Stuck trying to backtest with minute-level resolution

Hi all,

First post here. I'm fairly new to python but not new to trading. I've coded something simple to get acquainted to Quantopian and I am hopelessly stuck. My code runs fine when I am backtesting with daily data. However, when I enable the schedule_function (currently wrapped in """s) and run it on the minute level, I get an attribute error:

'SIDData' object has no attribute 'price' There was a runtime error on line 39.

The worst part is I can't figure out why. Also, I should add that I did manage to get the backtest to run on the minute level yesterday. However, the trades were completely different from the daily backtest. More specifically, the strategy went long TLT in late-2002 and no other trades occurred afterwards. I thought perhaps it was my settings within the schedule_function so I played around with the time_rules...and now it's giving me error messages that I never got before.

As I've said, I am new to python, so any help would be greatly appreciated!

2 responses

Do not call your scheduled function "handle_data".

In fact NEVER USE handle_data. Period.

Always use the schedule_function (someother name)

    schedule_function(HandleTradingLogic,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_close(minutes=1))  
def handle_data(context, data):  
    pass

def HandleTradingLogic(context, data):  
    ...

handle_data will run whatever logic you put inside it EVERY PERIOD, minutely or daily.

MT nails down one of the issues - initialize() and handle_data() are method names reserved in the Quantopian API. The initialize() function is used to setup your variables and create your universe of stocks. When you use schedule_function to create a custom method, you should call it any other name.

Regarding the different behaivor between backtesting in daily and minute mode, check out this post: https://www.quantopian.com/posts/differences-between-minute-and-daily-backtests

TLDR: The mode determines how often price data is received in your algo. You should backtest in minute mode to closely simulate 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.