Back to Community
Syntax Help on Schedule Function

Hey, I am having trouble with my code, I simply don't understand what I am doing wrong with the syntax.
here is the code

def initialize(context):  
    context.stock =sid(39940)  
    schedule_function(  
    myfunc,  
    date_rules.every_day(),)  

    schedule_function(  
    myotherfunc,  
    date_rules.every_day(),  
    time_rules.market_close(minutes=2))  
total_minutes = 6*60 + 30

for i in range(total_minutes):  
    # Every 30 minutes run schedule  
        if i % 30 == 0:  
            schedule_function(  
            myfunc  
            date_rule = date_rules.every_day(),  
            time_rule=time_rules.market_open(minutes=i),)  
total_minutes = 6*60 + 30

for i in range(total_minutes):  
    # Every 30 minutes run schedule  
        if i % 30 == 0:  
            schedule_function(  
            handle_data  
            date_rule=date_rules.every_day(),  
            time_rule=time_rules.market_open(minutes=i),)

def myfunc_(context, data):  
    open_price= data[context.stock].open_price  
    order(context.stock, 100, style=StopOrder(open_price))

def handle_data(context, data):  
    data[context.stock].open_price  
    curr_bar = data[context.stock].price  
    open_price= data[context.stock].open_price  
    if curr_bar > open_price :  

        order(context.stock, 100, style=StopOrder(open_price))  
def myfunc(context, data):  
    data[context.stock].open_price  
    curr_bar = data[context.stock].price  
    open_price= data[context.stock].open_price  

    if curr_bar <= open_price :


        order(context.stock, -100, style=LimitOrder(open_price))  

def myotherfunc(context,data):  
    order(context.stock, -100)  
4 responses

just as a hint: it would be way more useful you would apply a backtest and provide some more information about the error you are getting.

don't use "," after the last argument in a function, for instance "date_rules.every_day(),)" will certainly throw an error

This is not correct. It is perfectly legal in Python to have a comma after the last item in any list:

>>> def foo(*args, **kwargs):  
...     print args  
...     print kwargs  
...  
>>> foo('a', 'b',)  
('a', 'b')  
{}  
>>> foo('a', 'b', c='d',)  
('a', 'b')  
{'c': 'd'}  
>>> 

Getting back to the original question, whitespace and indentation are syntactically significant in Python, and in your initialize() function, everything after the first "total_minutes = 6*60 + 30" is indented four spaces less than it should be. If you accurately replicated your indentation in the sample code above, then that's your problem, or at least a problem. If you didn't accurately replicate your indentation, then you need to accurately replicate your indentation in the code sample you ask us to look at for us to be able to effectively help you find syntax issues. ;-)

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.

@Jonathan: thanks, learnt something new today :-).... (although your link refers to a discussion about lists , this http://stackoverflow.com/questions/12087742/python-trailing-comma-in-function-call-yes-no discusses using a comma after the last function argument, which afaik is often not allowed in other programming languages)

I think you forgot two "," in the function argument list and there was an issue with the fact that the minimum offset has to be 1... this should work... well at least it compiles ;-)

Clone Algorithm
6
Loading...
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
def initialize(context):  
    context.stock =sid(39940)  
    schedule_function(  
    myfunc,  
    date_rules.every_day(),)  

    schedule_function(  
    myotherfunc,  
    date_rules.every_day(),  
    time_rules.market_close(minutes=2))  
    total_minutes = 6*60 + 30
    

    for i in range(total_minutes):  
    # Every 30 minutes run schedule  
        if i % 30 == 1:  
            schedule_function(  
            myfunc,  
            date_rule = date_rules.every_day(),  
            time_rule=time_rules.market_open(minutes=i),)  
    
    total_minutes = 6*60 + 30

    for i in range(total_minutes):  
    # Every 30 minutes run schedule  
        if i % 30 == 1:  
            schedule_function(  
            handle_data,  
            date_rule=date_rules.every_day(),  
            time_rule=time_rules.market_open(minutes=i),)

def myfunc_(context, data):  
    open_price= data[context.stock].open_price  
    order(context.stock, 100, style=StopOrder(open_price))

def handle_data(context, data):  
    data[context.stock].open_price  
    curr_bar = data[context.stock].price  
    open_price= data[context.stock].open_price  
    if curr_bar > open_price :  
        order(context.stock, 100, style=StopOrder(open_price))  

def myfunc(context, data):  
    data[context.stock].open_price  
    curr_bar = data[context.stock].price  
    open_price= data[context.stock].open_price  
    if curr_bar <= open_price :
        order(context.stock, -100, style=LimitOrder(open_price))  

def myotherfunc(context,data):  
    order(context.stock, -100)
There was a runtime error.