Constraints

[constraints] restricts the search space of an optimization. Whale-E runs one backtest for every hyperparameter combination, and this block discards, up front, those that do not satisfy a condition, before they are ever tested.

Consider a crossover of two moving averages, fast and slow, explored over the same ranges: two sources (close and ohlc4), three types (sma, ema, and wma), and a length from 10 to 50, which gives 41 values. Each average therefore represents 2 × 3 × 41 = 246 configurations, and the crossover of the two tests 246 × 246 = 60,516.

Many of these combinations are pointless, because a fast average longer than the slow one matches no real strategy. The constraint fast.length < slow.length keeps only the length pairs where the fast one is shorter, that is 41 × 40 / 2 = 820 pairs instead of 41 × 41. The optimization then drops to 2 × 3 × 2 × 3 × 820 = 29,520 backtests. The full derivation is available on the Hyperparameter Combinations page.

Block declaration

A strategy contains at most one [constraints] block. It exposes a single key, condition, holding the logical expression evaluated for each combination. When the condition is true, the combination is kept and tested; when it is false, the combination is discarded.

Examples

Two moving averages crossover

For a fast / slow crossover, require that the fast period stays below the slow period:

[constraints]
condition = "fast.length < slow.length"

Consistent source and type

When several sources and types are explored, you can also require that both averages share the same source and the same type:

[constraints]
condition = "fast.length < slow.length and fast.type = slow.type and fast.source = slow.source"

Optimized constants

The condition can also reference [[constant]] blocks by their id. To keep a take profit above the stop loss throughout the optimization:

[constraints]
condition = "stop_loss_pct < take_profit_pct"

Block parameters

ParameterDescription
condition
 Text (expression)
 Required
Logical expression evaluated for each hyperparameter combination. The combination is kept when the result is true (not 0) and discarded when it is false (0).

Referenceable variables

The condition references the hyperparameters explored during the optimization:

  • indicator block parameters, written as <id>.<parameter>, for example fast.length, fast.type, or fast.source;
  • [[constant]] blocks, written with their id alone, for example take_profit_pct.

The available operators are the comparisons <, >, <=, >=, = and the logical operators and and or.

Evaluation

  • The condition is evaluated for each combination before its backtest. Only the combinations that satisfy it are run.
  • A single [constraints] block is taken into account. To combine several rules, join them with and and or within the same condition.