Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Sorry, me again!

Hi,

I have this code to work out the formula of a boll. slope:

import talib as ta  
import numpy as np  
import pandas as pd

# Setup our variables  
def initialize(context):  
    context.stocks = symbols('AAPL')

def handle_data(context, data):  
    for sid in data:  
        daily_prices = history(bar_count=504, frequency='1d', field='price')

        weekly_prices = daily_prices.resample('1W')  
        weekly_close = np.array(weekly_prices[sid].values.tolist())  
        weekly_bb_upper, weekly_smaLong, weekly_bb_lower = \  
            ta.BBANDS(weekly_close, timeperiod=30, nbdevup=2, nbdevdn=2, matype=0)

        i = -30  
        for b in weekly_bb_lower[i:]:  
            print i  
            ml, c = np.polyfit( range(3), weekly_bb_lower[i:i+3], 1 )  
            i += 4  
            print ml, c  

but when I run it I get this error:

24 Error Runtime exception: TypeError: expected x and y to have same length

and I don't understand why!

When I run the ml, c instruction in the debug it returns the values as expected.

Anyone have any ideas?

Thanks in advance for your help.

2 responses

Hi,

I've refined my code a bit but still getting the same error as before. Here's my code:

        i = 0  
        b = -3  
        bb_gradient = defaultdict(dict)  
        while ( b <= 0 ):  
            print i  
            print len(range(3))  
            print len(weekly_bb_lower[b-3:b])  
            bb_gradient[i][0], _ = np.polyfit( range(3), weekly_bb_lower[b-3:b], 1 )  
            i += 1  
            b += 1  

When I stop the loop in debug I get the correct lengths for len(range(3)) and len(weekly_bb_lower[b-3:b]) being displayed which match. So why am I getting the error on the polyfit function?

Any help appreciated.

Hey Andrew, I'm not 100% sure what you're trying to do, it looks like a rolling regression of some type. You can try using pandas ols, it does rolling regressions, or if you like numpy's polyfit, you might find np.poly1d handy, it returns the polynomial as a function. I included the basic use of each in the algo below.