Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
how to use schedule_function in Quantopian Research?

Has anyone gotten schedule_function to work in Quantopian Research?

I am porting my local backtesting scripts to QResearch, however
am getting the below error message during import of date_rules.

from zipline.utils.events import date_rules, time_rules  

Error message: "InputRejected: Importing date_rules from zipline.utils.events raised an ImportError. No modules or attributes with a similar name were found."

Do I need to import date_rules/time_rules from somewhere else, or are they just not yet supported?
Thanks.

3 responses

Hi Ted,

Try importing it from zipline.api

Check out this code snippet:

"""
This cell is going to create the basic framework of the algorithm  
"""

import zipline  
import pytz  
from datetime import datetime  
import matplotlib.pyplot as pyplot  
from collections import defaultdict

from zipline import TradingAlgorithm  
from zipline.api import order_target_percent, record, symbol, history, add_history, schedule_function, date_rules, time_rules  
import numpy as np

#: NOTICE HOW THIS IS OUTSIDE INITIALIZE, BECAUSE IT IS, WE CAN REDFINE IT EVERYTIME WE REDINE INITIALIZE  
aapl_weights = .50  
spy_weights = .50

def initialize(context):  
    context.aapl = 24  
    context.spy = 8554  
    context.aapl_weights = aapl_weights  
    context.spy_weights = spy_weights  
    context.first_time = True  
    schedule_function(func=order_stocks,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open(minutes=30))  
def order_stocks(context, data):  
    order_target_percent(context.aapl, 1)

def handle_data(context, data):  
    #: Only order on the first bar  
    return  
    if context.first_time:  
        order_target_percent(context.aapl, context.aapl_weights)  
        order_target_percent(context.spy, context.spy_weights)  
        context.first_time = False  

Thanks
Seong

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.

Perfect! Thanks Seong. Thanks also for posting the tutorials. They were very helpful!

Hi Seong, Karen,

You might want to just import it automatically, and generally, anything else that works in the browser backtester that would also work when backtesting using the research platform. Or is the idea that you want users to explicitly import this stuff, so that they know what's going on, and don't create problems / conflicts in their notebooks?

Grant