Class Factors

java.lang.Object
com.quantfinlib.alpha.Factors

public final class Factors extends Object
The standard alpha factor library — nine signal generators covering the classic technical, factor-investing and defensive families. Each returns an AlphaFactor producing raw cross-sectional scores where higher = more attractive long (see the interface contract).

Sign conventions are chosen so every factor is usable long/short as-is:

  • Trend (MA crossover, MACD, momentum) score positively when the trend is up;
  • Mean reversion (RSI, Bollinger, mean reversion) score positively when the price is depressed — the contrarian orientation, since these signals bet on the snap-back;
  • Defensive/fundamental (value, quality, low volatility) score positively for cheap, profitable, calm names — the Fama-French/AQR orientation of each anomaly.

All computations read only bars <= index (the no-look-ahead contract) and cost O(window) per symbol per call — stateless by design so factors are trivially safe to evaluate at arbitrary dates, at the price of recomputing windows the streaming indicators would carry. EMAs are truncated at 4× their period, where the dropped tail weight is (1−2/(p+1))^{4p} < 0.04% — far below signal noise.

  • Method Summary

    Modifier and Type
    Method
    Description
    bollinger(int period, double stdDevs)
    Bollinger mean reversion: −(close − SMA) / (k·σ) — the negative band position, +1 at the lower band, −1 at the upper.
    lowVolatility(int lookback)
    Low-volatility anomaly: −σ(returns) over the lookback — calm names score high.
    macd(int fast, int slow, int signal)
    MACD histogram normalized by price: (macdLine − signalLine) / close.
    meanReversion(int lookback)
    Plain mean reversion: −(close / SMA − 1) — how far the price sits below its own average, as a fraction.
    momentum(int lookback, int skip)
    Cross-sectional momentum: close[i−skip] / close[i−lookback] − 1.
    movingAverageCrossover(int fast, int slow)
    Moving-average crossover: (SMA_fast − SMA_slow) / SMA_slow.
    Quality composite: profitability minus leverage — ROE − 0.1 × debt/equity.
    rsi(int period)
    Contrarian RSI: (50 − RSI) / 50, in [−1, +1].
    Value composite: the average of earnings yield (1/PE) and book yield (1/PB) — yields, not ratios, so "cheap" is high and negative-earnings names contribute a negative yield rather than a meaningless negative PE rank.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • movingAverageCrossover

      public static AlphaFactor movingAverageCrossover(int fast, int slow)
      Moving-average crossover: (SMA_fast − SMA_slow) / SMA_slow. Positive when the fast average rides above the slow — the % spread makes scores comparable across price levels.
    • macd

      public static AlphaFactor macd(int fast, int slow, int signal)
      MACD histogram normalized by price: (macdLine − signalLine) / close. Positive while bullish momentum is accelerating; the price normalization keeps a $10 and a $1000 stock on one scale.
    • momentum

      public static AlphaFactor momentum(int lookback, int skip)
      Cross-sectional momentum: close[i−skip] / close[i−lookback] − 1. The academic 12-1 form when called as momentum(252, 21) — skipping the last month sidesteps short-term reversal (Jegadeesh-Titman 1993).
    • rsi

      public static AlphaFactor rsi(int period)
      Contrarian RSI: (50 − RSI) / 50, in [−1, +1]. Oversold names (RSI 30 → +0.4) score positively.

      Definition note: this is Cutler's RSI (arithmetic average of gains/losses over the window), chosen because it is stateless and exactly recomputable at any bar. It is NOT the same number as indicators.Indicators#rsi, which uses Wilder smoothing — after a trend the two can disagree near the 30/70 thresholds. The factor name says so to keep reports unambiguous.

    • bollinger

      public static AlphaFactor bollinger(int period, double stdDevs)
      Bollinger mean reversion: −(close − SMA) / (k·σ) — the negative band position, +1 at the lower band, −1 at the upper.
    • meanReversion

      public static AlphaFactor meanReversion(int lookback)
      Plain mean reversion: −(close / SMA − 1) — how far the price sits below its own average, as a fraction.
    • value

      public static AlphaFactor value()
      Value composite: the average of earnings yield (1/PE) and book yield (1/PB) — yields, not ratios, so "cheap" is high and negative-earnings names contribute a negative yield rather than a meaningless negative PE rank. NaN without fundamentals.
    • quality

      public static AlphaFactor quality()
      Quality composite: profitability minus leverage — ROE − 0.1 × debt/equity. The 0.1 haircut puts one turn of leverage on the same scale as 10 points of ROE, the usual quality-minus-junk shape (profitable AND conservatively financed).
    • lowVolatility

      public static AlphaFactor lowVolatility(int lookback)
      Low-volatility anomaly: −σ(returns) over the lookback — calm names score high. Ranking only needs the negative sign, not annualization.