Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Z-Score for Stock Returns

Hi All,

Could someone help me calculate the returns for individual stocks in a loop as well as finding the z-score for those stock returns?

Thanks,

Rohit

2 responses

Rohit ,

Try this :

    for stock in context.my_securities:  
        # Get returns for all stocks in security list  
        prices = data.history(stock, 'price', 21 , '1d')  
        returns = prices.pct_change()[-20:]  
        # Calculate z-score for returns  
        context.z_score = stats.zscore(returns)[-1]

it should work.

Thanks for the help, it did work. However, I'm now getting an unrelated error when I try to calculate my weights. Essentially, my longs and shorts list isn't being appended even if the conditions are met. Would you know how to fix this?