python 自定义动量因子

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 自定义动量因子相关的知识,希望对你有一定的参考价值。

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

以上是关于python 自定义动量因子的主要内容,如果未能解决你的问题,请参考以下文章

动量参数开合模型

他山之石 | 时间序列动量因子

CTA因子总体表现良好 时间序列动量和波动率因子表现最佳

python TTM基本字段自定义因子

四因子模型的介绍

策略回测报告——简单动量自适应策略