streak

Count consecutive bars where condition is true

streak(condition, period)

Parameters

Streak - streak(condition, period)

Counts consecutive bars where condition is true, looking back from the current bar.

  • condition (boolean expression): The condition to test (e.g., close > open)
  • period (int): Maximum lookback window to check for consecutive occurrences

Formula


Starting from current bar (bar 0), count backwards how many consecutive
bars satisfy the condition, up to a maximum of 'period' bars.

Returns integer from 0 to period:
  - 0 = condition false on current bar
  - 1 = true on current bar only
  - 2 = true on current and 1 bar ago
  - ...
  - period = true for all bars in window

Examples


// how many consecutive up days (close > open)?
streak(close > open, 5);

// consecutive days above 50-day MA
streak(close > sma(close, 50), 10);

// consecutive days with volume > average
streak(volume > sma(volume, 20), 7);

// find stocks with 3+ consecutive up days
streak(close > open, 5) >= 3;

// consecutive days where RSI > 50
streak(rsi(close, 14) > 50, 10);

// consecutive narrow range days (true_range < average)
streak(true_range() < sma(true_range(), 20), 5);

Returns

Integer: Count of consecutive true conditions (0 to period)