Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Error

I am having an error running this algo. I used it 2 weeks ago and it was working just fine. Now it give len() of unsized object at line 44.

from zipline.utils.tradingcalendar import get_early_closes

def initialize(context):  
    context.stock = sid(2) # AAPL  
    set_commission(commission.PerTrade(cost=0.00))  
    set_slippage(slippage.FixedSlippage(spread=0.00))  
    set_benchmark(context.stock)

    schedule_function(check_and_buy,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_open(minutes=1))  
    schedule_function(sell_all,  
                      date_rule=date_rules.every_day(),  
                      time_rule=time_rules.market_close(minutes=5))  
    context.early_closes = get_early_closes(  
        context.stock.start_date, context.stock.end_date).date  
def check_and_buy(context, data):  
    if get_datetime().date() in context.early_closes:  
        log.info('Early close: no trading')  
    else:  
        if context.stock in data:  
            highs = history(bar_count=200, frequency='1d', field='high')  
            lows = history(bar_count=200, frequency='1d', field='low')  
            opens =  history(bar_count=5, frequency='1d', field='open_price')  
            close =  history(bar_count=5, frequency='1d', field='close_price')  
            for s in data:  
                 retns = (close[s][-2]-opens[s][-2])  
            ret = ((highs-lows)/lows)*100  
            retstd = ret.std()  
            retavg = ret.mean()  
            over = retavg+retstd  
            negover = -1*over  
            for s in data:  
                 prev_ret = ret[s][-2]  
            # Insert your calculations here, and then depending  
            # on the result, set "buy" to True or False  
        if (retns > 0 and prev_ret > over):  
            order_target_percent(context.stock, -1)  
            record(avg=retavg)  
        else:  
            if (retns <0 and prev_ret < negover):  
              order_target_percent(context.stock, 1)  

def sell_all(context, data):  
    if context.stock in data:  
        if context.portfolio.positions[context.stock].amount != 0:  
            order_target_percent(context.stock, 0)  
def handle_data(context, data):  
    pass  
5 responses

Can you explain the algorithm I can write another alternative code

The algorithm is basically a test to Overreaction hypothesis. It calculates returns as ((high-low)/low)*100
It then takes the value of returns for past 200 days and calculates its average and standard deviation.
If the previous day returns are more than average returns + standard deviation (that means the price has overreacted) it will buy or sell in the opposite direction of overreaction.
This algo was working 2 weeks ago . Now it gives an error.

Hello Yatharth
I will try to write a separate algorithm.
Also my skype id : nishant20233 and if possible come on audio conferencing

I have attached a backtest of the modified algorithm

Thanks Nishant for solving it. I found another method as well to solve this problem by using over.values[0])