Overview
Condition blocks create the logic and rules that determine when your algorithm takes action. These blocks compare values, detect crossovers, combine multiple conditions, and implement complex decision-making logic. Every trading decision flows through condition blocks.
Comparison Operators
Condition: A < B
Returns TRUE if A is less than B
📋 Example: Buy on Dip
source1 = Data: Ticker (AAPL, 1m)
sma1 = Indicator: SMA (source1, length=20)
lt1 = Condition: A < B (source1_price, sma1)
buy1 = Action: Buy (symbol=AAPL, cash=$10,000, when=lt1)
Condition: A ≤ B
Returns TRUE if A is less than or equal to B
📋 Example: Stop Loss
pos_value1 = State: Position Value (AAPL)
lte1 = Condition: A ≤ B (pos_value1, 9500)
sell1 = Action: Sell (symbol=AAPL, sell_all_from=buy1, when=lte1)
Condition: A > B
Returns TRUE if A is greater than B
📋 Example: Trend Entry
source1 = Data: Ticker (TSLA, 1m)
sma1 = Indicator: SMA (source1, length=50)
gt1 = Condition: A > B (source1_price, sma1)
buy1 = Action: Buy (symbol=TSLA, cash=$10,000, when=gt1)
Condition: A ≥ B
Returns TRUE if A is greater than or equal to B
📋 Example: Overbought Exit
source1 = Data: Ticker (QQQ, 1m)
rsi1 = Indicator: RSI (source1, length=14)
gte1 = Condition: A ≥ B (rsi1, 70)
sell1 = Action: Sell (sell_all_from=buy1, when=gte1)
Logical Operators
Condition: AND
LOGICReturns TRUE only if ALL connected conditions are TRUE. Use for stricter entry criteria.
📋 Example: Multi-Condition Entry
source1 = Data: Ticker (SPY, 1m)
sma1 = Indicator: SMA (source1, length=50)
rsi1 = Indicator: RSI (source1, length=14)
macd1 = Indicator: MACD Line (source1, 12/26/9)
macd_signal1 = Indicator: MACD Signal (source1, 12/26/9)
gt1 = Condition: A > B (source1_price, sma1)
gt2 = Condition: A > B (rsi1, 50)
gt3 = Condition: A > B (macd1, macd_signal1)
and1 = Condition: AND (gt1, gt2)
and2 = Condition: AND (and1, gt3)
buy1 = Action: Buy (cash=$10,000, when=and2)
Condition: OR
LOGICReturns TRUE if ANY connected condition is TRUE. Use for multiple entry opportunities.
📋 Example: Multiple Exit Conditions
source1 = Data: Ticker (SPY, 1m)
rsi1 = Indicator: RSI (source1, length=14)
sma1 = Indicator: SMA (source1, length=50)
gt1 = Condition: A > B (rsi1, 70)
lt1 = Condition: A < B (rsi1, 30)
lt2 = Condition: A < B (source1_price, sma1)
or1 = Condition: OR (gt1, lt1)
or2 = Condition: OR (or1, lt2)
sell1 = Action: Sell (sell_all_from=buy1, when=or2)
Special Conditions
Condition: Cross
TIMINGDetects when one value crosses above or below another value. Essential for crossover strategies.
Cross Above
TRUE when A crosses from below to above B
Previous: A < B
Current: A > B
Result: TRUE ✅
Cross Below
TRUE when A crosses from above to below B
Previous: A > B
Current: A < B
Result: TRUE ✅
📋 Example: Golden Cross Strategy
source1 = Data: Ticker (SPY, 1d)
sma1 = Indicator: SMA (source1, length=50)
sma2 = Indicator: SMA (source1, length=200)
cross1 = Condition: Cross (sma1, sma2)
gt1 = Condition: A > B (sma1, sma2)
lt1 = Condition: A < B (sma1, sma2)
and1 = Condition: AND (cross1, gt1)
and2 = Condition: AND (cross1, lt1)
buy1 = Action: Buy (cash=$10,000, when=and1)
sell1 = Action: Sell (sell_all_from=buy1, when=and2)
Condition: Cross Over (A above B)
TIMINGTriggers only when A crosses from below to above B (up-cross). Use when you want bullish-only signals.
📋 Example: Bullish Crossover Entry
source1 = Data: Ticker (AAPL, 1m)
ema1 = Indicator: EMA (source1, length=9)
ema2 = Indicator: EMA (source1, length=21)
crossover1 = Condition: Cross Over (ema1, ema2)
buy1 = Action: Buy (symbol=AAPL, cash=$10,000, when=crossover1)
Condition: Sticky True
STATEOnce true, stays true until a Loop Restart. Useful for latching a signal or enforcing one-time actions.
📋 Example: Latch an Entry Signal
source1 = Data: Ticker (SPY, 1m)
rsi1 = Indicator: RSI (source1, length=14)
lt1 = Condition: A < B (rsi1, 30)
latch1 = Condition: Sticky True (lt1)
pos_qty1 = State: Position Qty (SPY)
lte1 = Condition: A ≤ B (pos_qty1, 0)
and1 = Condition: AND (latch1, lte1)
buy1 = Action: Buy (cash=$10,000, when=and1)
Condition: Up Streak ≥ N
MOMENTUMReturns TRUE if price has closed higher for N consecutive periods. Detects sustained upward momentum.
📋 Example: Momentum Breakout
source1 = Data: Ticker (SPY, 1d)
streak_up1 = Condition: Up Streak ≥ N (n=3, ref=source1_price)
streak_down1 = Condition: Down Streak ≥ N (n=2, ref=source1_price)
buy1 = Action: Buy (cash=$5,000, when=streak_up1)
sell1 = Action: Sell (sell_all_from=buy1, when=streak_down1)
Condition: Down Streak ≥ N
MOMENTUMReturns TRUE if price has closed lower for N consecutive periods. Detects sustained downward momentum.
📋 Example: Mean Reversion Entry
source1 = Data: Ticker (SPY, 1d)
rsi1 = Indicator: RSI (source1, length=14)
streak_down1 = Condition: Down Streak ≥ N (n=3, ref=source1_price)
lt1 = Condition: A < B (rsi1, 30)
and1 = Condition: AND (streak_down1, lt1)
gt1 = Condition: A > B (rsi1, 60)
buy1 = Action: Buy (cash=$5,000, when=and1)
sell1 = Action: Sell (sell_all_from=buy1, when=gt1)
Quick Reference
| Condition | Returns TRUE when | Best For |
|---|---|---|
| A < B | A is less than B | Price below support, oversold, dips |
| A ≤ B | A is less than or equal to B | Stop losses, max position limits |
| A > B | A is greater than B | Breakouts, resistance breaks, strong momentum |
| A ≥ B | A is greater than or equal to B | Profit targets, minimum thresholds |
| AND | All conditions are TRUE | Multi-factor confirmation, strict entries |
| OR | Any condition is TRUE | Multiple entry signals, flexible exits |
| Cross | A crosses above/below B | MA crossovers, indicator signals |
| Cross Over (A above B) | A crosses from below to above B | Bullish-only entry timing |
| Sticky True | Once true, stays true until Loop Restart | One-time triggers, latching signals |
| Up Streak ≥ N | N consecutive up closes | Momentum confirmation, trend following |
| Down Streak ≥ N | N consecutive down closes | Mean reversion, oversold bounces |
Common Condition Patterns
1. Triple Confirmation Entry
source1 = Data: Ticker (SPY, 1m)
sma1 = Indicator: SMA (source1, length=50)
rsi1 = Indicator: RSI (source1, length=14)
macd1 = Indicator: MACD Line (source1, 12/26/9)
macd_signal1 = Indicator: MACD Signal (source1, 12/26/9)
gt1 = Condition: A > B (source1_price, sma1)
gt2 = Condition: A > B (rsi1, 50)
gt3 = Condition: A > B (macd1, macd_signal1)
and1 = Condition: AND (gt1, gt2)
and2 = Condition: AND (and1, gt3)
buy1 = Action: Buy (cash=$10,000, when=and2)
High-quality signals with low false positives
2. Flexible Exit Conditions
source1 = Data: Ticker (SPY, 1m)
rsi1 = Indicator: RSI (source1, length=14)
sma1 = Indicator: SMA (source1, length=50)
gt1 = Condition: A > B (rsi1, 70)
lt1 = Condition: A < B (rsi1, 30)
lt2 = Condition: A < B (source1_price, sma1)
or1 = Condition: OR (gt1, lt1)
or2 = Condition: OR (or1, lt2)
sell1 = Action: Sell (sell_all_from=buy1, when=or2)
Multiple exit strategies for risk management
3. Trend Filter with Crossover
source1 = Data: Ticker (SPY, 1m)
sma1 = Indicator: SMA (source1, length=200)
ema1 = Indicator: EMA (source1, length=9)
ema2 = Indicator: EMA (source1, length=21)
gt1 = Condition: A > B (source1_price, sma1)
crossover1 = Condition: Cross Over (ema1, ema2)
and1 = Condition: AND (gt1, crossover1)
buy1 = Action: Buy (cash=$10,000, when=and1)
Combine trend filter with timing signals
4. Mean Reversion with Momentum
source1 = Data: Ticker (SPY, 1m)
rsi1 = Indicator: RSI (source1, length=14)
ema1 = Indicator: EMA (source1, length=9)
streak_down1 = Condition: Down Streak ≥ N (n=3, ref=source1_price)
lt1 = Condition: A < B (rsi1, 30)
gt1 = Condition: A > B (source1_price, ema1)
and1 = Condition: AND (streak_down1, lt1)
and2 = Condition: AND (and1, gt1)
buy1 = Action: Buy (cash=$10,000, when=and2)
Wait for oversold bounce confirmation
Best Practices
✅ Do:
- Use AND for stricter, higher-quality entry signals
- Use OR for multiple valid exit conditions
- Combine trend, momentum, and multiple indicators for confirmation
- Test different condition combinations in backtests
- Use Sticky True to latch signals until Loop Restart
- Use Cross for timing, comparisons for continuous checks
❌ Don't:
- Over-complicate with too many AND conditions (no signals)
- Use only OR conditions (too many false signals)
- Confuse Cross (one-time event) with > (continuous)
- Ignore edge cases (division by zero, missing data)
- Trade every signal - quality over quantity