# Candle Counter Indicator for ThinkorSwim

> Free candle counter for ThinkorSwim: counts green and red candles in a row, plots both as histograms and shows 4 labels with the max and average counts.

Page: https://tosindicators.com/indicators/candle-counter  
Video: https://www.youtube.com/watch?v=UriFiBvI3zc  
Section: Indicators  
Skill: Beginner-Friendly  
Access: free  
Updated: 2026-10-17

In this tutorial I build a candle counter indicator for ThinkorSwim that counts how many green candles, and how many red candles, print in a row. A histogram in a lower panel shows each count bar by bar, and 4 labels show the highest and the average count for each color across all the data on your chart.

I built it to profile the S&P 500: how many green days in a row it tends to have before a red day, and the reverse, so you have a better idea of where a trend might be exhausting. It's also a short, beginner-friendly thinkScript exercise in counter variables, resetting them, labels and histograms.

## Key Takeaways

- The candle counter is a free ThinkorSwim lower study that counts consecutive green candles (close above open) and consecutive red candles (close at or below open). Each count resets to 0 when a candle of the other color prints.

- It plots both counts as histograms, green for green candles and pink for red, and prints 4 labels: Max Green, Max Red, Avg Green and Avg Red.

- A doji, where the close equals the open, counts as a red candle, so it ends a green run.

- In the video, with 20 years of daily SPY data loaded, Max Green read 13, a run that started on March 25, and Max Red read 10.

- The labels use HighestAll and TotalSum, so they cover every bar loaded on the chart. Load more history and the max and average can change.

## What Is a Candle Counter?

A candle counter is an indicator that counts how many candles of the same color have printed in a row. On a daily chart, a green count of 5 means the market has closed above its open 5 days running.

The question behind ours is simple: how many green days in a row does the S&P 500 have before a red day, and how many red days before a green one? Knowing the longest run and the average count gives you a profile of the market, so a run that is getting long by that market's standards stands out.

The video is also a thinkScript lesson. You'll see how to create counter variables, how to reset them, how to plot a histogram, and how to keep two groups of data, the green candles and the red candles, separate from each other.

## What the Candle Counter Shows

The candle counter shows 2 histograms in a lower panel and 4 labels at the top of that panel. The histograms count, and the labels summarize those counts over the whole chart.

| Item | What it shows | Color |
| --- | --- | --- |

| Green histogram | How many green candles in a row up to this bar, 0 on a red candle | Green |

| Red histogram | How many red candles in a row up to this bar, 0 on a green candle | Pink |

| Max Green label | The highest green count on the chart | Green |

| Max Red label | The highest red count on the chart | Pink |

| Avg Green label | The average green count across all green candles, to 1 decimal | Green |

| Avg Red label | The average red count across all red candles, to 1 decimal | Pink |

In the video's example, 6 green candles in a row built the green histogram up to a bar of 6. The next red candle dropped it back to 0 and started the red count.

## How to Add the Candle Counter to ThinkorSwim

The candle counter is a regular ThinkorSwim study, created from the Studies menu.

- Click the **Studies** icon on your chart, then click **Create**.

- Name the study, for example candle counter, and delete the starter code in the editor.

- Paste in the code from the Code tab on this page, then click **OK** and **Apply**.

- Load enough history for the labels to mean something. I used 20 years of daily candles on SPY.

The code starts with `declare lower`, so it opens in its own panel under the price chart. To import the downloaded file instead, follow our guide on [how to import a study into ThinkorSwim](https://tosindicators.com/thinkscript/how-to-import-thinkorswim).

## How the Code Works

The candle counter code is 23 lines of thinkScript, built in 5 steps. The snippets below come from the downloaded file.

### Step 1: Define Green and Red Candles

```
def green = close > open;
def red = close

## thinkScript code

```
#Written by TOS Indicators 2023
#Home of the Volatility Box
#Indicator: Candle Counter
#Full Tutorial Link: tosindicators.com/indicators/candle-counter

declare lower;

def green = close > open;
def red = close <= open;

def greenCounter = if green then greenCounter[1] + 1 else if red then 0 else greenCounter[1];

def redCounter = if red then redCounter[1] + 1 else if green then 0 else redCounter[1];

## Max Value

def maxGreenCount = HighestAll(greenCounter);
def maxRedCount = HighestAll(redCounter);

AddLabel(yes, "Max Green: " + maxGreenCount, color.green);
AddLabel(yes, "Max Red: " + maxRedCount, color.pink);

## Average Value

def totalGreenCandleSum = TotalSum(greenCounter);
def totalRedCandleSum = TotalSum(redCounter);

def totalGreenOccurrences = TotalSum(if greenCounter >= 1 then 1 else 0);
def totalRedOccurrences = TotalSum(if redCounter >= 1 then 1 else 0);

def avgGreenCounter = totalGreenCandleSum / totalGreenOccurrences;
def avgRedCounter = totalRedCandleSum / totalRedOccurrences;

AddLabel(yes, "Avg Green: " + Round(avgGreenCounter, 1), color.green);
AddLabel(yes, "Avg Red: " + Round(avgRedCounter, 1), color.pink);

## Histogram

plot greenHistogram = greenCounter;
plot redHistogram = redCounter;
greenHistogram.setPaintingStrategy(PaintingStrategy.Histogram);
redHistogram.setPaintingStrategy(PaintingStrategy.Histogram);
greenHistogram.setDefaultColor(Color.green);
redHistogram.setDefaultColor(Color.pink);
```

## Questions

### What is a candle counter indicator?

A candle counter counts how many candles of the same color print in a row. Ours is a free ThinkorSwim lower study that counts green candles and red candles separately, plots each count as a histogram, and labels the highest and the average count for each color across the chart.

### How do I add the candle counter to ThinkorSwim?

Click the Studies icon, click Create, name the study and delete the starter code. Paste in the code from the Code tab, click OK and Apply. You can also import the downloaded TI_CandleCounterSTUDY.ts file. The study opens in its own panel under the price chart because the code starts with declare lower.

### How does the candle counter reset?

The green counter adds 1 to its previous value on every green candle and resets to 0 on a red candle. The red counter does the opposite. So after 6 green candles the green histogram reads 6, and the next red candle drops it to 0 while the red histogram starts at 1.

### Does a doji count as a green or red candle?

A doji counts as red. The code defines a red candle as a close at or below the open, so a candle that closes exactly at its open is red. That means a doji ends a green run, resets the green counter to 0 and adds 1 to the red counter.

### What was the longest run of green days in the S&P 500 in the video?

With 20 years of daily SPY data loaded, the Max Green label read 13: a run of 13 green candles in a row that started on March 25. The video doesn't state the year. The longest red run on the same chart was 10 candles. Both numbers cover only the data loaded at the time.

### How is the Avg Green label calculated?

Avg Green adds up the green counter on every bar and divides by the number of bars where it is 1 or more, which leaves out the 0s from red candles. That gives the average count a green candle sits at. A single run of 5 green candles would read 3, while its length is 5.

### Why don't the candle counter histograms show on my chart?

Check that declare lower is at the top of the code. In the video the two plots showed nothing until I added it, because declare lower moves the study into its own panel under the price chart. Also check that both plots use the histogram painting strategy if you see lines instead of bars.

### Can I use the candle counter on other symbols and timeframes?

Yes. The code has no symbol or timeframe setting, so it counts the candles of whatever chart you load. In the video I switched from SPY to QQQ, where Max Red was a little bigger, and mentioned the Dow, the Russell, Apple and Microsoft. On an intraday chart it counts intraday candles the same way.

## Related

- [2 Ways to Use NYSE $TICK to Spot Trend Days](https://tosindicators.com/research/nyse-tick-spot-trend-days-thinkorswim)
- [How to Build a Fair Value Gap Indicator in ThinkorSwim](https://tosindicators.com/indicators/fair-value-gap)
- [How to Build a Heiken Ashi Trend Dots Indicator in ThinkorSwim](https://tosindicators.com/indicators/heiken-ashi)
- [How to Build a Commodities Tracker in ThinkorSwim](https://tosindicators.com/indicators/commodities-tracker)
- [Trend Squeeze Indicator for ThinkorSwim](https://tosindicators.com/indicators/trend-squeeze)
- [Volume Intensity Indicator for ThinkorSwim](https://tosindicators.com/indicators/volume-intensity)

