Introduction

Beta is a statistical measure of a stock's volatility in relation to the market. Stock analysts use this measure to get a sense of stocks' risk profiles. It is also a key component of the capital asset pricing model (CAPM), A stock's price variability is essential to consider when assessing risk. It represents the co-movement instead of the volatility. Therefore, it is possible for a stock to have zero beta and higher volatility than the market.

In the real world, some investors are prohibited from using leverage and other investors’ leverage is limited by margin requirements. Therefore, their only way to achieve higher returns is to buy more risky stocks, which would cause the overvaluation of higher-beta stocks. This behavior suggests that high-beta (risky) stocks should deliver lower risk-adjusted returns than low-beta stocks. In this algorithm, we'll use the leverage to explore the inefficiency of the beta factor.

Method

The investment universe consists of all stocks in Nasdaq and NYSE. We use the Wilshire 5000 Total Market Index which covers all stocks actively traded in the United States.

  1. def initialize(self):
  2. self.add_universe(self.coarse_selection_function)
  3. # Add Wilshire 5000 Total Market Index data from Dropbox
  4. self.price5000 = self.add_data(Fred, Fred.wilshire.price5000, Resolution.DAILY).symbol

We set up a automatic ROC indicator and a 1-year rolling window to hold its daily return data for a year. We warm them up by historical data.

  1. # Setup a RollingWindow to hold market return
  2. self.market_return = RollingWindow[float](252)
  3. # Use a ROC indicator to convert market price index into return, and save it to the RollingWindow
  4. self.roc = self.ROC(self.price5000, 1)
  5. self.roc.updated += lambda sender, updated: self.market_return.add(updated.value)
  6. # Warm up
  7. hist = self.history(self.price5000, 253, Resolution.DAILY)
  8. for point in hist.itertuples():
  9. self.roc.update(point.index[1], point.value)

The formula for calculating beta is the covariance of the return of an asset with the return of the market divided by the variance of the return of the market over a certain period.

βi=cov(Ri,Rm)Var(Rm)

We choose the 1-year rolling window as the period in the beta calculation. We created the SymbolData class to update the rolling window of return and the calculation of beta.

  1. class SymbolData:
  2. def __init__(self, symbol):
  3. self.symbol = symbol
  4. self.last_price = 0
  5. self.returns = RollingWindow[float](252)
  6. self.roc = RateOfChange(1)
  7. self.roc.updated += lambda sender, updated: self.returns.add(updated.value)
  8. def update(self, time, price):
  9. if price != 0:
  10. self.last_price = price
  11. self.roc.update(time, price)
  12. def is_ready(self):
  13. return self.roc.is_ready and self.returns.is_ready
  14. def beta(self, market_ret):
  15. asset_return = np.array(list(self.returns), dtype=np.float32)
  16. market_return = np.array(list(market_ret), dtype=np.float32)
  17. return np.cov(asset_return, market_return)[0][1]/np.var(market_return)
+ Expand

In the CoarseSelectionFunction, we filter the stocks which price is lower than five as they are not active in the market. When the return rolling window is ready, stocks are then ranked in ascending order on the basis of their estimated beta. The algorithm goes long on five stocks at the bottom beta list and short on five stocks at the top beta list.

  1. def initialize(self):
  2. self.data = {}
  3. self.monthly_rebalance = False
  4. self.long = None
  5. self.short = None
  6. def coarse_selection_function(self, coarse):
  7. for c in coarse:
  8. if c.symbol not in self.data:
  9. self.data[c.symbol] = SymbolData(c.symbol)
  10. self.data[c.symbol].update(c.end_time, c.adjusted_price)
  11. if self.monthly_rebalance:
  12. filtered_data = {symbol: data for symbol, data in self.data.items() if data.last_price > 5 and data.is_ready()}
  13. if len(filtered_data) > 10:
  14. # sort the dictionary in ascending order by beta value
  15. sorted_beta = sorted(filtered_data, key = lambda x: filtered_data[x].beta(self.market_return))
  16. self.long = sorted_beta[:5]
  17. self.short = sorted_beta[-5:]
  18. return self.long + self.short
  19. else:
  20. self.monthly_rebalance = False
  21. return []
  22. else:
  23. return []
+ Expand

In each portfolio, securities are weighted by the ranked betas. Lower-beta stocks have larger weights in the low-beta portfolio and higher-beta stocks have larger weights in the high-beta portfolio. The portfolios are rebalanced every calendar month.

  1. def on_data(self, data):
  2. if not self.monthly_rebalance: return
  3. # Liquidate symbols not in the universe anymore
  4. for symbol in self.portfolio.keys:
  5. if self.portfolio[symbol].invested and symbol not in self.long + self.short:
  6. self.liquidate(symbol)
  7. if self.long is None or self.short is None: return
  8. long_scale_factor = 0.5/sum(range(1,len(self.long)+1))
  9. for rank, symbol in enumerate(self.long):
  10. self.set_holdings(symbol, (len(self.long)-rank+1)*long_scale_factor)
  11. short_scale_factor = 0.5/sum(range(1,len(self.long)+1))
  12. for rank, symbol in enumerate(self.short):
  13. self.set_holdings(symbol, -(rank+1)*short_scale_factor)
  14. self.monthly_rebalance = False
  15. self.long = None
  16. self.short = None
+ Expand


Reference

  1. Quantpedia - Beta Factor in Stocks

Author

Jing Wu

September 2018