Introduction

The Momentum traders take on a long or short position in the stock in the hopes that the momentum will continue in the same direction. The momentum strategy has a higher degree of volatility than most other strategies. In the tutorial, we'll introduce the trading volume factor to enhance the portfolio return and control the risk of momentum strategy.

Method

The investment universe consists of all stocks on NYSE and NASDAQ. The momentum is usually defined by the stock return in the last N months. The RateOfChange indicator is constructed to represent the return. The lookback period is one year.

  1. class SymbolData:
  2. def __init__(self, symbol, lookback):
  3. self.symbol = symbol
  4. self.ROC = RateOfChange(lookback)
  5. self.volume = None

The ROC indicator and volume are updated everyday in the CoarseSelectionFunction. Securities which do not have fundamental data are eliminated.

  1. def coarse_selection_function(self, coarse):
  2. for i in coarse:
  3. if i.symbol not in self.data_dict:
  4. self.data_dict[i.symbol] = SymbolData(i.symbol, self.lookback)
  5. self.data_dict[i.symbol].ROC.update(i.end_time, i.adjusted_price)
  6. self.data_dict[i.symbol].volume = i.volume
  7. if self.monthly_rebalance:
  8. # drop stocks which have no fundamental data
  9. filtered_coarse = [x for x in coarse if (x.has_fundamental_data)]
  10. return [i.symbol for i in filtered_coarse]
  11. else:
  12. return []

Trading volume (turnover) serves as a useful indicator of the level of investor interest in stocks. The number of sellers tends to exceed the number of buyers when the stock falls into disfavor, which will lead to a falling share price. When a stock is popular, the number of buyers exceeds the number of sellers, the price tends to rise. As a result, a firm’s turnover may be a measure of investors' interest in the firm's stock so it could help to identify the future trend of stocks. Here the turnover is calculated as the ratio of the number of shares traded each day to the number of shares outstanding.

In the FineSelectionFunction, after the indicator is ready for all symbols in the self.data_dict dictionary, turnover is calculated with the Volume and EarningReports.basic_average_shares.

  1. def fine_selection_function(self, fine):
  2. if self.monthly_rebalance:
  3. data_ready = {symbol: symbolData for (symbol, symbolData) in self.data_dict.items() if symbolData.ROC.is_ready}
  4. if len(data_ready) < 100:
  5. self.filtered_fine = []
  6. else:
  7. sorted_fine = [i for i in fine if i.earning_reports.basic_average_shares.three_months != 0 and i.symbol in data_ready]
  8. sorted_fine_symbols = [i.symbol for i in sorted_fine]
  9. filtered_data = {symbol: symbolData for (symbol, symbolData) in data_ready.items() if symbol in sorted_fine_symbols}
  10. for i in sorted_fine:
  11. if i.symbol in filtered_data and filtered_data[i.symbol].volume != 0:
  12. filtered_data[i.symbol].turnover = i.earning_reports.basic_average_shares.three_months / filtered_data[i.symbol].volume

Stocks are sorted into deciles every month based on previous 12-month returns. Each momentum decile is then divided into terciles based on turnover. The long and short portfolios are constructed with the highest turnover from the top momentum decile and the highest volume from the bottom momentum decile, respectively.

  1. for i in sortedFine:
  2. if i.symbol in filteredData and filteredData[i.symbol].volume != 0:
  3. filteredData[i.symbol].turnover = i.earning_reports.basic_average_shares.three_months / filteredData[i.symbol].volume
  4. sortedByROC = sorted(filteredData.values(), key = lambda x: x.ROC.current.value, reverse = True)
  5. topROC = sortedByROC[:int(len(sortedByROC)*0.2)]
  6. bottomROC = sortedByROC[-int(len(sortedByROC)*0.2):]
  7. HighTurnoverTopROC = sorted(topROC, key = lambda x: x.turnover, reverse = True)
  8. HighTurnoverBottomROC = sorted(bottomROC, key = lambda x: x.turnover, reverse = True)
  9. self.long = [i.symbol for i in HighTurnoverTopROC[:int(len(HighTurnoverTopROC)*0.01)]]
  10. self.short = [i.symbol for i in HighTurnoverBottomROC[:int(len(HighTurnoverBottomROC)*0.01)]]
  11. self.filtered_fine = self.long + self.short
  12. self.portfolios.append(self.filtered_fine)

A long-short portfolio is held for three months, and then it is rebalanced. Therefore, the investor buys 1/3 of its portfolio for three consecutive months and rebalances 1/3 of its portfolio each month. We save each portfolio in a deque list self.portfolios and liquidate the portfolio three-months ago when rebalancing.

  1. def on_data(self, data):
  2. if self.monthly_rebalance and self.filtered_fine:
  3. self.filtered_fine = None
  4. self.monthly_rebalance = False
  5. # 1/3 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 3 months
  10. short_weight = 1/len(self.short)
  11. for i in self.short:
  12. self.set_holdings(i, -1/3*short_weight)
  13. long_weight = 1/len(self.long)
  14. for i in self.long:
  15. self.set_holdings(i, 1/3*long_weight)
+ Expand


Reference

  1. Quantpedia Premium - Combining Momentum Effect with Volume

Author

Jing Wu

August 2018