Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Solved - Help With Unexpected Output From CustomFactor
class EWMAWeekly(CustomFactor):  
    inputs=[USEquityPricing.close]  
    window_length=(13 + (2 * 13) - 1) * 5 # Initial 13 weeks for sma, then 25 more weeks to improve the accuracy of the current ema.  
    def compute(self, today, assets, out, data):  
        alpha = 2 / (13 + 1)  
        weekly_data = data[4::5] # len = 38, index from 0 - 37  
        ema = average(weekly_data[:13]) # Initial SMA  
        i = 0  
        while i < 25:  
            ema = (weekly_data[13+i] * alpha) + (ema * (1 - alpha))  
            i += 1

        out[:] = ema  

Hello All,
I'm making the custom factor above to get a 13wk EMA, however, when I run it through my pipeline the output is always the "initial sma" calculation commented above. It has to be something with the reassignment of "ema" in the while loop. It doesn't throw any errors. It basically runs like the code below.

class EWMAWeekly(CustomFactor):  
    inputs=[USEquityPricing.close]  
    window_length=(13 + (2 * 13) - 1) * 5  
    def compute(self, today, assets, out, data):  
        alpha = 2 / (13 + 1)  
        weekly_data = data[4::5] # len = 38, index from 0 - 37  
        out[:] = average(weekly_data[:13]) # Initial SMA  

Any help would be appreciated.

Thanks

EDIT:
First, I want to say that I looked at this for an hour before I posted a question. Second, I made a silly error. The problem is I was using integers to calculate alpha which was always zero. If you look at my ema calculation in the while loop and change alpha to zero you are always left with the original ema. The correct code is posted below for anyone interested.

class EWMAWeekly(CustomFactor):  
    inputs=[USEquityPricing.close]  
    window_length=(13 + (2 * 13) - 1) * 5 # Initial 13 weeks for sma, then 25 more weeks to improve the accuracy of the current ema.  
    def compute(self, today, assets, out, data):  
        alpha = 2.0 / (13.0 + 1.0)  
        weekly_data = data[4::5] # len = 38, index from 0 - 37  
        ema = average(weekly_data[:13]) # Initial SMA  
        i = 0  
        while i < 25:  
            ema = (weekly_data[13+i] * alpha) + (ema * (1 - alpha))  
            i += 1

        out[:] = ema