Back to Community
Quick Troubleshooting Question

Trying to run an old algo. Getting a syntax error ...on this line:

schedule_function(end_of_week, date_rule=date_rules.week_end())

What's wrong with that?

Thanks,
Stephen

3 responses

That syntax is correct (assuming the function you want to call is 'end_of_week'). You may want to add a time_rule but it will work without it (and default to market_open).

Sometimes a Python error actually occurs in the previous line but it doesn't get recognized until the next. Could it be the previous line that's the problem? Maybe a missing closing parenthesis?

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.

Dan, I really appreciate your help. This is actually a really great little algorithm ...and I think it's simple. Here's the whole thing.


def initialize(context):
context.quarter_flag = False
context.asset1 = symbol('SPY')
context.asset2 = symbol('XLU')
context.Buy = 0
context.Current = 0
context.BuyAlert = False

set_slippage(slippage.FixedSlippage(spread=0.01))  
set_commission(commission.PerShare(cost=0.00, min_trade_cost=0.00))  
set_benchmark(symbol('SPY')  

schedule_function(end_of_week, date_rule=date_rules.week_end())  
schedule_function(rebalance, date_rules.week_start(), time_rules.market_close(minutes=30))

def rebalance(context, data):
if context.BuyAlert:
if context.Current != 0:
order_target_percent(context.Current, 0)
order_target_percent(context.Buy, 1)
context.Current = context.Buy
context.BuyAlert = False

def end_of_week(context, data):
if get_datetime().week % 1 == 0:
n = 110
context.quarter_flag = True
maxPercent = 0

    asset1_prices = data.history(context.asset1, "price", n, "1d")  
    asset1_PercentChange = (asset1_prices[-1] - asset1_prices[-110]) / asset1_prices[-1]  
    if asset1_PercentChange > maxPercent:  
        context.Buy = context.asset1  
        maxPercent = asset1_PercentChange  

    asset2_prices = data.history(context.asset2, "price", n, "1d")  
    asset2_PercentChange = (asset2_prices[-1] - asset2_prices[-110]) / asset2_prices[-1]  
    if asset2_PercentChange > maxPercent:  
        context.Buy = context.asset2  
        maxPercent = asset2_PercentChange  

    if context.Buy != context.Current:  
        context.BuyAlert = True

def before_trading_start(context, data):
if context.quarter_flag:
print(get_datetime())
context.quarter_flag = False

Clone Algorithm
10
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
# Equity BOND ROTATION STRATEGY

def initialize(context):
    context.quarter_flag = False
    context.asset1 = symbol('XLK') # SPY Long 2x
    context.asset2 = symbol('XLV') # SPY Short 2x
    context.Buy = 0
    context.Current = 0
    context.BuyAlert = False
    
    set_slippage(slippage.FixedSlippage(spread=0.01)) # average $0.01 bid/ask spread per share
    set_commission(commission.PerShare(cost=0.00, min_trade_cost=0.00)) # $0.005/share, $1 minimum 
    set_benchmark(symbol('SPY'))
    
    schedule_function(end_of_week, date_rule=date_rules.week_end())
    schedule_function(rebalance, date_rules.week_start(), time_rules.market_close(minutes=30))

def rebalance(context, data):
    if context.BuyAlert:
        if context.Current != 0:
            order_target_percent(context.Current,  0)
        order_target_percent(context.Buy,  1)
        context.Current = context.Buy
        context.BuyAlert = False

def end_of_week(context, data):
    if get_datetime().week % 1 == 0: 
        n = 110
        context.quarter_flag = True
        maxPercent = 0
           
        asset1_prices = data.history(context.asset1, "price", n, "1d")
        asset1_PercentChange = (asset1_prices[-1] - asset1_prices[-110]) / asset1_prices[-1]
        if asset1_PercentChange > maxPercent:
            context.Buy = context.asset1
            maxPercent = asset1_PercentChange
        
        asset2_prices = data.history(context.asset2, "price", n, "1d")
        asset2_PercentChange = (asset2_prices[-1] - asset2_prices[-110]) / asset2_prices[-1]
        if asset2_PercentChange > maxPercent:
            context.Buy = context.asset2
            maxPercent = asset2_PercentChange
        
        if context.Buy != context.Current:
            context.BuyAlert = True

def before_trading_start(context, data):
    if context.quarter_flag:
        print(get_datetime())
        context.quarter_flag = False
There was a runtime error.

Well ...now, suddenly it's working fine. Thanks for replying Dan.