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

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5919fd6d1f05586184bba71b
There was a runtime error.
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?

Clone Algorithm
6
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 591b0438d0221166619f6b20
There was a runtime error.