count
Count total occurrences of condition in lookback window
count(condition, period)
Parameters
| Name | Type | Description |
|---|---|---|
condition |
boolean expression | The condition to test (e.g., close > open) |
period |
int | Lookback window to count occurrences |
Formula
```
Count all bars in the last 'period' bars where condition is true.
Returns integer from 0 to period:
- 0 = condition false for all bars in window
- 1 = true on exactly 1 bar
- 2 = true on exactly 2 bars
- ...
- period = true for all bars in window
```
Examples
// how many of the last 5 days were bullish?
count(close > open, 5);
// count days above 50-day MA in last 10 days
count(close > sma(close, 50), 10);
// count high-volume days in last 7 days
count(volume > sma(volume, 20), 7);
// find stocks with at least 3 up days in last 5
count(close > open, 5) >= 3;
// count days where RSI > 50 in last 10 days
count(rsi(close, 14) > 50, 10);
// count narrow range days in last 5 days
count(true_range() < sma(true_range(), 20), 5);
Returns
Integer: Count of true conditions (0 to period) ## Notes - Unlike streak(), count() does NOT require consecutive occurrences - Counts all true conditions within the lookback window - Useful for identifying frequency of patterns/conditions - Works with any boolean expression (comparisons, crosses, etc.) ## Related Functions - `streak(condition, period)` - Consecutive occurrences only - `rising(field, period)` - Specific case: consecutive increases - `falling(field, period)` - Specific case: consecutive decreases