UB001UNIV's rule engine lets you compose entry and exit conditions using boolean logic — AND, OR, NOT — across any indicators. This article covers the syntax patterns, common idioms, and the design discipline that keeps custom rules legible and testable.
The Building Blocks
A rule is a boolean expression evaluated each bar. The atoms are comparisons:
- Indicator vs constant: RSI < 30, ADX > 25, ATR > 1.5.
- Indicator vs indicator: EMA(20) > EMA(50), MACD > signal.
- Indicator vs price/level: close > SMA(200), high > previous_high, close > pivot_R1.
- Cross events: crosses_above(MACD, signal), crosses_below(price, EMA(50)). True only on the bar of crossing.
Atoms combine via AND, OR, NOT to form rules. Rules can be nested.
The Three Rule Types
Most strategies need three rule definitions:
- Entry rule. When all conditions are met, open a position.
- Exit rule. When any condition triggers, close the position. Independent of entry.
- Filter rule. Optional global gate — entries only fire when this is true. Useful for regime conditioning.
Risk-management exits (stop loss, take profit, time stop) are configured separately and run alongside the explicit exit rule.
Entry and exit rules are independent — they don't have to be inverses of each other. A common asymmetric pattern: enter when RSI < 25 (rare extreme), exit when RSI > 50 (any neutralization). This captures more of each move than a symmetric "enter at 30, exit at 70" rule.
Common Idioms
Regime-Conditional Entry
The filter ensures the strategy only fires in low-trend regimes. Above the long-term MA keeps you in the directional bias. RSI is the timing.
Volume-Confirmed Breakout
Donchian breakout with volume confirmation. Chandelier exit (ATR-based trailing) preserves trend profits.
Multi-Indicator Mean Reversion
Three independent oversold confirmations + a ranging regime filter. Exits on either neutralization. Wider profit-take captures fuller reversion.
Cross-Asset / Index Filter
Trade the asset only when the broad market is in an uptrend. Exit if the broad market regime flips. Common for single-name strategies that historically fail in bear markets.
The crosses_above / crosses_below Idiom
Many crossover strategies fire on the moment of crossing, not on the persistent state of "above" or "below." Use cross events:
- EMA(20) > EMA(50) — true on every bar where 20 is above 50. Your strategy will keep firing entries every bar this is true.
- crosses_above(EMA(20), EMA(50)) — true only on the bar where EMA(20) crossed from below to above. Single-bar event. The signal you want for entries.
For exits, the same pattern: crosses_below(EMA(20), EMA(50)) exits on the moment of trend reversal, rather than every bar after.
Boolean Pitfalls
- Conflicting AND/OR semantics. "(RSI < 30) OR (RSI > 70 AND ADX > 25)" — without parentheses, AND binds tighter than OR, but in some users' mental models it's the opposite. Always use explicit parentheses.
- Negation traps. NOT (A AND B) ≠ (NOT A) AND (NOT B). De Morgan's laws apply: NOT (A AND B) = (NOT A) OR (NOT B). Test edge cases.
- Comparing apples to oranges. RSI is 0–100; ATR is in price units. "RSI > ATR" type comparisons are nonsense. Verify scales.
- Same-bar comparisons. "close > close[1]" is fine on closed bars but ambiguous mid-bar. UB001UNIV evaluates rules on bar close by default.
Keeping Rules Testable
- Limit clauses to 3–4 per rule. Each clause adds a degree of freedom. Beyond 5 clauses, rules become hard to validate and easy to overfit.
- Round constants. RSI < 30, not RSI < 28.5. Optimized constants are tells of curve-fit.
- Sweep at most one or two parameters. Sweep RSI threshold; don't sweep RSI period AND threshold AND ADX threshold AND volume cutoff in the same study.
- Document each clause. If you can't justify why each clause is in the rule, it doesn't belong.
The Bottom Line
Custom rules are the most powerful feature of UB001UNIV — and the source of most of its overfit failures. Three to four well-justified clauses with round-number thresholds, swept and walk-forwarded, produce strategies that survive. Five-plus clauses with optimized thresholds produce backtests that look beautiful and live results that look like noise.
Further Reading
Foundational papers
- Sullivan, R., Timmermann, A. & White, H. (1999). Data-Snooping, Technical Trading Rule Performance, and the Bootstrap. Journal of Finance, 54(5), 1647–1691.
- Brock, W., Lakonishok, J. & LeBaron, B. (1992). Simple Technical Trading Rules and the Stochastic Properties of Stock Returns. Journal of Finance, 47(5), 1731–1764.
Textbook references
- Kaufman, P. J. (2013). Trading Systems and Methods (5th ed.). Wiley.
- Chan, E. P. (2013). Algorithmic Trading: Winning Strategies and Their Rationale. Wiley.
Related QuanterLab articles
Try it in QuanterLab
In UB001UNIV write down each rule clause and its justification before running the backtest. If you cannot articulate why a clause should help in plain English, remove it. Justified clauses survive walk-forward; unjustified ones are noise.