Class Rules

java.lang.Object
com.quantfinlib.dsl.Rules

public final class Rules extends Object
Factory of common Rules over indicator arrays. All rules are NaN-safe: a rule is never satisfied while its inputs are in warm-up.

The design decision worth knowing: a Rule is a predicate over a BAR INDEX into precomputed indicator arrays, not over live values. That keeps strategy definitions declarative ("RSI crossed under 30 AND price above the 200-day") and — more importantly — makes look-ahead bias structurally harder: the arrays are computed once by causal indicator code, and a rule can only combine values at i and i-1, never peek at i+1. Cross rules require the previous bar to be on the other side (with <=/>=), so a series that OPENS above the level does not count as a cross — the classic off-by-one that fires a "breakout" signal on bar 0 of every backtest. NaN warm-up bars satisfy nothing, and combining rules with Rule.and(com.quantfinlib.dsl.Rule)/Rule.or(com.quantfinlib.dsl.Rule)/Rule.not() preserves that. Assembled into strategies by StrategyBuilder.

  • Method Details

    • crossAbove

      public static Rule crossAbove(double[] a, double[] b)
      a crossed above b on this bar.
    • crossBelow

      public static Rule crossBelow(double[] a, double[] b)
      a crossed below b on this bar.
    • crossAboveValue

      public static Rule crossAboveValue(double[] a, double level)
      a crossed above a constant level on this bar.
    • crossBelowValue

      public static Rule crossBelowValue(double[] a, double level)
      a crossed below a constant level on this bar.
    • above

      public static Rule above(double[] a, double[] b)
    • below

      public static Rule below(double[] a, double[] b)
    • aboveValue

      public static Rule aboveValue(double[] a, double level)
    • belowValue

      public static Rule belowValue(double[] a, double level)
    • rising

      public static Rule rising(double[] a, int bars)
      a has risen on each of the last bars bars.
    • falling

      public static Rule falling(double[] a, int bars)
      a has fallen on each of the last bars bars.