Skip to Main Content
Back to website
Indicator Disclaimer | Report this indicator

The Consecutive Candles indicator counts how many bars in a row price has closed up or down and plots that streak as a histogram in a lower pane.

Green bars grow taller the longer an up streak continues, while red bars deepen as a down streak extends. The count resets to one as soon as the streak reverses. A dotted zero line serves as the baseline reference. The indicator includes two modes so you can define what counts as an up or down bar.

  • vs. Last Close mode counts consecutive closes above or below the prior bar's close, measuring pure close-to-close momentum.
  • Candle Color mode counts consecutive bullish or bearish candles based on whether each bar closes above or below its own open, focusing on individual bar structure rather than carry-over momentum.
  • Streak reset logic flips immediately on a reversal, so you always know exactly how extended the current run is.
describe_indicator('Consecutive Candles', 'lower');

// Input parameter to toggle between close momentum and candle color modes
const myMode = input.select('Mode', 'vs. Last close', ['vs. Last close', 'Candle Color']);

// This indicator counts consecutive closes above or below the previous close
// Green bars grow when price closes higher than the previous close
// Red bars grow when price closes lower than the previous close
// The count resets when the trend reverses
const consecutiveBars = for_every(close, open, (currentClose, currentOpen, prevValue, index) => {
  if (index === 0) {
    return 0;
  }
  
  let isHigherClose;
  let isLowerClose;
  
  if (myMode === 'vs. Last close') {
    // Compare current close to previous close
    const prevClose = close[index - 1];
    isHigherClose = currentClose > prevClose;
    isLowerClose = currentClose < prevClose;
  } else {
    // Compare current close to current open (candle color)
    isHigherClose = currentClose > currentOpen;
    isLowerClose = currentClose < currentOpen;
  }
  
  if (isHigherClose) {
    // If previous value was positive, add 1. If negative, reset to 1
    return (prevValue > 0) ? prevValue + 1 : 1;
  }
  else if (isLowerClose) {
    // If previous value was negative, subtract 1. If positive, reset to -1
    return (prevValue < 0) ? prevValue - 1 : -1;
  }
  else {
    // Close equals previous close (or open in color mode), maintain current streak
    return prevValue;
  }
});

// Create colors: green for positive values, red for negative
const barColors = for_every(consecutiveBars, value => value >= 0 ? '#469e48' : '#f34539');

paint(consecutiveBars, { 
  style: 'column', 
  color: barColors, 
  name: 'Consecutive Closes' 
});

// Paint a zero line for reference
paint(horizontal_line(0), { 
  style: 'dotted', 
  color: 'gray', 
  thickness: 1, 
  name: 'Zero Line' 
});