MichaelJMath
6/20/2018 - 4:57 PM

Custom Momentum Factor

Custom Momentum Factor

def construct_custom_momo_factor(momo_period_days, lag_days, volatility_period_days):
  """Wrapper function to generate Custom Momo factor with varying parameters"""
  class CustomMomoFactor(CustomFactor):
      """This example will create a custom 
      volatility-adjusted momentum factor"""
      # Set constants - These will not necessarily be 
      # in every custom factor
      LAG = lag_days
      MOMO_PERIOD = momo_period_days
      VOLATILITY_LOOKBACK = volatility_period_days

      # Default inputs and window_length
      inputs = [USEquityPricing.close]
      window_length = VOLATILITY_LOOKBACK + LAG + 1

      def compute(self, today, asset_ids, out, close):
          daily_returns = close[1:] / close[:-1] - 1
          std = np.nanstd(daily_returns, ddof=1, axis=0) * np.sqrt(self.MOMO_PERIOD)
          ret_end_pos = -self.LAG - 1
          ret_start_pos = ret_end_pos - self.MOMO_PERIOD
          out[:] = (close[ret_end_pos] / close[ret_start_pos] - 1) / std
          
  return CustomMomoFactor
class Momo_15_2(CustomFactor):
    """This example will create a custom 
    volatility-adjusted momentum factor"""
    # Set constants - These will not necessarily be 
    # in every custom factor
    LAG_MONTHS = 2
    LAG = 21 * LAG_MONTHS
    MOMO_PERIOD = 252
    VOLATILITY_LOOKBACK = 252 * 3
    
    # Default inputs and window_length
    inputs = [USEquityPricing.close]
    window_length = VOLATILITY_LOOKBACK + LAG + 1
  
    def compute(self, today, asset_ids, out, close):
        daily_returns = close[1:] / close[:-1] - 1
        std = np.nanstd(daily_returns, ddof=1, axis=0) * np.sqrt(self.MOMO_PERIOD)
        ret_end_pos = -self.LAG - 1
        ret_start_pos = ret_end_pos - self.MOMO_PERIOD
        out[:] = (close[ret_end_pos] / close[ret_start_pos] - 1) / std