Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to resample the daily 'USEquityPricing.close' to weekly?

Seems my question is very simple. But I really don't know how to do it. The purpose is I want to build a weekly indicator with CustomerFactor.

6 responses

Hi Thomas,

I have used this in Pipeline:

close_weekly = USEquityPricing.close.latest.downsample('week_start')  

Also in def(close):

close_weekly = lambda close: close.downsample('week_start')  

Quantopian has more information on Filter and Classifier.

Hope this helps.

Hi Karl,

Many thanks! Seems the Q has updated and expended their API Reference.

I will have a try.

Now, I haven't created a weekly CustomerFactor. I do somewhat different. It seems easier but I am not sure if this is correct.

What I did: I haven't applied the downsample onto the close, but on the output indicator value.

I can see your approach, Thomas and I can think of another way is to do lambda close: close.downsample('week_start') on USEquityPricing.close inside your MyTalibKAMA_Weekly(CustomFactor) and cross check both to see if the values are as expected for your purpose.

Hope this helps.

I've changed the MyTalibKAMA_Weekly as follow. There is any error but the outputs are not correct.

class MyTalibKAMA_Weekly(CustomFactor):

inputs = (USEquityPricing.close,)

inputs = (lambda close: close.downsample('week_start'),)  
params = {'kama_len' : 10,'day_offset':0,}  
window_length = 100 # 10 times of kama_len  
window_safe = True  
def compute(self, today, assets, out, close, kama_len, day_offset):  
    # TODO: figure out how this can be coded without a loop  
    kama = []  
    for col in close.T:  
        try:  
            kama_tmp = talib.KAMA(col, timeperiod=kama_len)  
            kama.append(kama_tmp[-1-day_offset])  
        except:  
            kama.append(np.nan)  
    out[:] = kama  

Hey! Glad to see somebody with the same isssue. Could you give me a helping hand with this custom factor? Seems like your approach doesn't work.

class GetMacDHistSlope(CustomFactor):  
    inputs = [USEP.close] #USEP = USEquityPricing  
    #inputs = [USEP.close.latest.downsample('week_start')]  
    inputs = [lambda close: close.downsample('week_start')]  
    window_length = 400  
    def compute(self, today, assets, out, close):  
        hists = []  
        for stock_close in close.T:  
            try:  
                #Compute only end of week equity prices!!  
                macd, signal, hist = talib.MACD(stock_close, fastperiod=12,  
                                                slowperiod=26, signalperiod=9)  
                if (hist[-3] > hist[-2]) and (hist[-2] > hist[-1]):  
                    trend = -1  
                elif (hist[-3] < hist[-2]) and (hist[-2] < hist[-1]):  
                    trend = 1  
                else:  
                    trend = 0  
                hists.append(trend)  
            except:  
                hists.append(np.nan)  
        out[:] = hists  

It gives me as a result

AttributeError: 'NoneType' object has no attribute 'format'