streak() - Consecutive Condition Counter
The streak() function counts how many consecutive bars satisfy your condition, starting from today and looking backwards.
Examples:
Find stocks with 5+ consecutive up days:
streak(close > open, 10) >= 5;
7 or more consecutive days above the 50-day moving average:
streak(close > sma(close, 50), 10) >= 7;
Volume compression (decreasing volume streak):
streak(volume < volume[1], 5) >= 4;
Consecutive days with RSI above 50:
streak(rsi(close, 14) > 50, 10) >= 5;
The streak breaks immediately when the condition becomes false. A stock with 3 up days, then 1 down day, then 2 up days has a current streak of 2.
count() - Occurrence Counter
The count() function counts how many times a condition is true in the last N bars. Unlike streak(), the occurrences don't need to be consecutive.
Examples:
Stocks with at least 3 up days in the last 5:
count(close > open, 5) >= 3;
Stocks mostly above their 50-day MA (7+ of last 10 days):
count(close > sma(close, 50), 10) >= 7;
Frequent high RSI readings:
count(rsi(close, 14) > 60, 10) >= 5;
High volume days in the last week:
count(volume > sma(volume, 20), 5) >= 3;
Using streak() and count() Together
Find consistent performers that aren't necessarily on a hot streak:
count(close > open, 5) >= 3 and streak(close > open, 5) < 3;
Find stocks in a strong consecutive uptrend:
count(close > open, 10) >= 7 and streak(close > open, 10) >= 3;
Key Differences
streak() - Counts consecutive true conditions from today backwards. Resets to 0 when condition is false.
count() - Counts total true conditions in the window. Doesn't require consecutive bars.
Both accept boolean expressions as the first parameter, making them flexible for any condition you can write in StonQL.