Introduction

Momentum is a well-known strategy that buys stocks with the best return over the past three to twelve months and sells stocks with the worst performances over the same time horizon. The reversal strategy buys the stocks with relatively low returns and sells stocks with high returns. In this algorithm, we will develop a long-short strategy combining the momentum/reversal effect with the realized volatility.

Method

The Universe Initial Filter

The investment universe consists of NYSE, AMEX and NASDAQ stocks with prices higher than $5 per share. In the FineSelectionFunction, we divide the universe into two equal halves by size of the company. Here size is defined as the share price times the number of shares outstanding.

  1. def coarse_selection_function(self, coarse):
  2. # update the price of stocks in universe everyday
  3. for i in coarse:
  4. if i.symbol not in self.data_dict:
  5. self.data_dict[i.symbol] = SymbolData(i.symbol, self.lookback)
  6. self.data_dict[i.symbol].update(i.adjusted_price)
  7. if self.monthly_rebalance:
  8. # drop stocks which have no fundamental data or have too low prices
  9. filtered_coarse = [x.symbol for x in coarse if (x.has_fundamental_data) and (float(x.price) > 5)]
  10. return filtered_coarse
  11. else:
  12. return []
  13. def fine_selection_function(self, fine):
  14. if self.monthly_rebalance:
  15. sorted_fine = sorted(fine, key = lambda x: x.earning_reports.basic_average_shares.value * self.data_dict[x.symbol].price, reverse=True)
  16. # select stocks with large size
  17. top_fine = sorted_fine[:int(0.5*len(sorted_fine))]
  18. self.filtered_fine = [x.symbol for x in top_fine]
  19. return self.filtered_fine
  20. else:
  21. return []
+ Expand

The Realized Return and Volatility

At the beginning of each month, realized returns and realized (annualized) volatilities are calculated for each stock. The realized volatility refers to the historical volatility. The formula of the realized volatility σ is

Ravg=∑ni=1Rin σ=√∑ni=1(Ri−Ravg)2n−1

To annualize the volatility, we multiply the 1-day volatility by the square root of the number of trading days in a year – in our case square root of 252.

A 6-month warm-up period is required to initialize the history price for stocks in the universe. We create the class SymbolData to save all required variables associated with a single stock. One week (5 trading days) prior to the beginning of each month is skipped to avoid biases due to microstructures.

  1. class SymbolData:
  2. '''Contains data specific to a symbol required by this model'''
  3. def __init__(self, symbol, lookback):
  4. self.symbol = symbol
  5. # self.history = RollingWindow[Decimal](lookback)
  6. self.history = deque(maxlen=lookback)
  7. self.price = None
  8. def update(self, value):
  9. # update yesterday's close price
  10. self.price = value
  11. # update the history price series
  12. self.history.append(float(value))
  13. # self.history.add(value)
  14. def is_ready(self):
  15. return len(self.history) == self.history.maxlen
  16. def volatility(self):
  17. # one week (5 trading days) prior to the beginning of each month is skipped
  18. prices = np.array(self.history)[:-5]
  19. returns = (prices[1:]-prices[:-1])/prices[:-1]
  20. # calculate the annualized realized volatility
  21. return np.std(returns)*np.sqrt(252)
  22. def return(self):
  23. # one week (5 trading days) prior to the beginning of each month is skipped
  24. prices = np.array(self.history)[:-5]
  25. # calculate the annualized realized return
  26. return (prices[-1]-prices[0])/prices[0]
+ Expand

After the warm-up period, the historical price series is ready. Stocks are sorted into quintiles based on their realized volatility. Stocks in the top 20% highest volatility are further sorted into quintiles by their six-month realized returns. The algorithm goes long on stocks from the highest performing quintile from the highest volatility group and short on stocks from the lowest performing quintile from the highest volatility group.

  1. def on_data(self, data):
  2. if self.monthly_rebalance and self.filtered_fine:
  3. filtered_data = {symbol: symbolData for (symbol, symbolData) in self.data_dict.items() if symbol in self.filtered_fine and symbolData.is_ready()}
  4. self.filtered_fine = None
  5. self.monthly_rebalance = False
  6. if len(filtered_data) < 100: return
  7. # sort the universe by volatility and select stocks in the top high volatility quintile
  8. sorted_by_vol = sorted(filtered_data.items(), key=lambda x: x[1].volatility(), reverse = True)[:int(0.2*len(filtered_data))]
  9. sorted_by_vol = dict(sorted_by_vol)
  10. # sort the stocks in top-quintile by realized return
  11. sorted_by_return = sorted(sorted_by_vol, key = lambda x: sorted_by_vol[x].return(), reverse = True)
  12. long = sorted_by_return[:int(0.2*len(sorted_by_return))]
  13. short = sorted_by_return[-int(0.2*len(sorted_by_return)):]

Portfolio Rebalance and Trade

The methodology of Jegadeesh and Titamn (1993) is used to rebalance the portfolio. Specifically, at the beginning of each month, stocks are sorted into quintiles based on their realized returns and equally weighted portfolios are formed to be held for the next six months. This sorting and portfolio formation procedure is performed each month. In any given month t, the strategy holds 6 portfolios that are selected in the current month as well as the previous 5 months. Therefore 1/6 of the portfolio is rebalanced every month. We save those 6 portfolios in a deque list self.portfolios and the list is updated every month. The portfolio of the current month is added while the portfolio selected from six months ago is removed from the list.

  1. def initialize(self):
  2. self.portfolios = deque(maxlen=6)
  3. def on_data(self, data):
  4. self.portfolios.append(short+long)
  5. # 1/6 of the portfolio is rebalanced every month
  6. if len(self.portfolios) == self.portfolios.maxlen:
  7. for i in list(self.portfolios)[0]:
  8. self.liquidate(i)
  9. # stocks are equally weighted and held for 6 months
  10. short_weight = 1/len(short)
  11. for i in short:
  12. self.set_holdings(i, -1/6*short_weight)
  13. long_weight = 1/len(long)
  14. for i in long:
  15. self.set_holdings(i, 1/6*long_weight)
+ Expand


Reference

  1. Quantpedia - Momentum and Reversal Combined with Volatility Effect in Stocks

Author

Jing Wu

August 2018