A free candle counter for ThinkorSwim. It counts green and red candles in a row, plots each run as a histogram and labels the max and average counts.
Download IndicatorIn 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.
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.
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.
The candle counter is a regular ThinkorSwim study, created from the Studies menu.
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.
The candle counter code is 23 lines of thinkScript, built in 5 steps. The snippets below come from the downloaded file.
def green = close > open;
def red = close <= open;
A green candle closes above its open. A red candle closes below its open or right at it, so a doji counts as red.
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];
If the bar is green, the green counter takes its value from the previous bar, greenCounter[1], and adds 1. If the bar is red, it resets to 0.
The red counter is the mirror image. While I built it in the video, I put the green counter in a chart bubble over each candle: it counted 1, 2, 3 up to 6, dropped to 0 on the next red candle and started again at 1 on the next green one. Then I removed the bubble.
def maxGreenCount = HighestAll(greenCounter);
def maxRedCount = HighestAll(redCounter);
AddLabel(yes, "Max Green: " + maxGreenCount, color.green);
AddLabel(yes, "Max Red: " + maxRedCount, color.pink);
HighestAll finds the highest value of a variable across every bar on the chart. I made the red labels pink so they’re easier to see.
An average is a sum divided by the number of values. For the numbers 1, 2, 3, 4 and 5, you add them up to 15 and divide by 5 to get 3. The candle counter does the same with its counters, one color at a time:
def totalGreenCandleSum = TotalSum(greenCounter);
def totalGreenOccurrences = TotalSum(if greenCounter >= 1 then 1 else 0);
def avgGreenCounter = totalGreenCandleSum / totalGreenOccurrences;
TotalSum adds a value up across every bar on the chart.
The occurrences line counts 1 only where the green counter is 1 or more. If you divided by every bar, all the 0s from red candles would drag the average down. The red side repeats the same three lines with redCounter.
AddLabel(yes, "Avg Green: " + Round(avgGreenCounter, 1), color.green);
AddLabel(yes, "Avg Red: " + Round(avgRedCounter, 1), color.pink);
Round(..., 1) keeps each average to 1 decimal place.
plot greenHistogram = greenCounter;
plot redHistogram = redCounter;
greenHistogram.setPaintingStrategy(PaintingStrategy.Histogram);
redHistogram.setPaintingStrategy(PaintingStrategy.Histogram);
greenHistogram.setDefaultColor(Color.green);
redHistogram.setDefaultColor(Color.pink);
The two plots draw the counters. In the video they showed nothing until I added declare lower at the top.
Plotted as lines, they rise with each candle of the same color and fall back to 0. The histogram painting strategy is easier to read, and the default colors fix the green and pink no matter what else is on your chart.
The Avg Green label is the average reading of the green counter across every green candle on the chart. Each green candle adds its count to the sum: the first candle of a run adds 1, the second adds 2, and so on.
That makes the label different from the average length of a run. If a single run of 5 green candles were the only one on the chart, the counter would read 1, 2, 3, 4 and 5, and Avg Green would show 3. Read it as the count a typical green candle sits at, and Max Green as the longest run.
If you want the average length of a run instead, here is a suggested edit, not in the video or the download. Add these 2 lines below the existing average lines:
def greenRuns = TotalSum(if greenCounter == 1 then 1 else 0);
AddLabel(yes, "Avg Green Run: " + Round(totalGreenOccurrences / greenRuns, 1), color.green);
greenRuns counts the bars where a new run starts. Green candles divided by runs is the average run length. For the red side, copy both lines, change every green to red (redRuns, redCounter, totalRedOccurrences and the label text) and use color.pink.
The candle counter examples in the video ran on daily candles with 20 years of data loaded. I read out the max labels and found each run on the chart. The video doesn’t read out the Avg Green or Avg Red values.
| Symbol and chart | Reading | What I found |
|---|---|---|
| SPY, daily, 20 years | Max Green 13 | The tallest green spike. The run started on March 25 (the year isn’t stated), and I counted the 13 candles off one by one. |
| SPY, daily, 20 years | Max Red 10 | Found by scrolling back toward the start of the data: 10 red candles, then a green one. |
| SPY, daily | A spike of 7 | While the counters were still plotted as lines, one spike lined up with 7 green candles in a row. |
| QQQ, daily, 20 years | Max Red | A little bigger than SPY’s. No figure read out. |
To find the max run yourself, zoom out and look for the tallest bar of that color in the histogram, then zoom in on it. The label and the bar should show the same number.
The candle counter tells you the personality of a market: how long its runs of green and red candles have been, at their longest and on average. The current histogram bar tells you where today’s run stands against that history.
A long run is context, and it doesn’t time the turn. Use the count to anticipate where a trend may be stretched, decide your stop before you trade, and size the position so a stop-out costs a small, fixed share of your account.
The candle counter is short, and most problems come from three places.
declare lower is at the top of the code, so the study plots in its own panel.Our AddLabel tutorial explains how the labels work if you want to change their text or colors.
The candle counter’s pattern, adding 1 to the previous bar’s value while a condition holds and resetting to 0 when its opposite appears, works for other conditions too. Replace the green and red definitions with your own pair of opposite conditions and the counters, labels and histograms follow.
For other examples, our increasing volume scan checks for 3 bars in a row with Sum, and our Extreme TICK counter counts +1,000 and -1,000 NYSE TICK readings each day. To define more detailed candles, see how to turn candlestick patterns into code. More free studies are on our ThinkorSwim indicators page.
#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);Want the .ts file and the one-click import? Both come with a free account.
Here are some resources that you may find useful:
Join 40,000+ traders using institutional-grade tools for ThinkOrSwim.
Get the Bundle