Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to count days of stock above sma in a period

How to count days of stock above sma in a period?

2 responses

let say i want to count days that stock price above sma200 within a period 50 days
Is the below correct?

class AboveDays(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 251  
    def compute(self, today, asset_ids, out, close):  
        range_mean = pd.Series(close).rolling(200).mean()  
        diff = close[-50:] - range_mean  
        out[:] = np.apply_along_axis(lambda x: np.nansum(x>0), 0, diff)  

anyone could help?