A free cumulative advance decline indicator for ThinkorSwim. It tracks 6 symbols you pick and plots CCI-confirmed arrows on crosses of its 50-bar average.
Download IndicatorIn this tutorial I build a cumulative advance decline line for a group of 6 stocks in ThinkorSwim, then turn it into a simple trading system. On each bar, every symbol that closed above its previous close adds 1, every symbol that closed below it subtracts 1, and the study keeps a running total.
The video answers a viewer’s question: how do you make the symbols inputs, so you can swap a stock from the settings window instead of editing the code? From there I add a 50-bar average and arrows confirmed by the CCI. It was Episode 2 of our How to thinkScript series, so it suits traders learning thinkScript who want a breadth line for their own stocks.
TotalSum keeps the running total.symbol_1 to symbol_6, so you change them in the study settings. The download defaults to FB, AMZN, AAPL, NFLX, GOOG and MSFT.advDecLineAvg, is the 50-bar average of the cumulative line. On the daily chart in the video, that covers the past 50 days.A cumulative advance decline line is a running total of advancing issues minus declining issues. The classic version counts every stock on an exchange such as the NYSE. Each day, the number of stocks that closed up minus the number that closed down is added to the previous total, so a rising line means more stocks have been going up than down.
Our study applies the same counting to a group of 6 symbols you choose. That gives you a breadth line for your own basket, such as the largest tech stocks or the leaders of one sector. The table shows how the two compare.
| Feature | Classic advance decline line | This study |
|---|---|---|
| What it counts | Every advancing and declining stock on an exchange | The 6 symbols in your inputs |
| What each bar adds | Advancing issues minus declining issues | +1 per symbol that closed up, -1 per symbol that closed down |
| Can you change the list? | No | Yes, from the study settings |
| Signals | A line only | Arrows on crosses of the 50-bar average, confirmed by the CCI |
Market internals measure the breadth of the whole market. If you want those readings instead of a custom basket, our Supply Demand Edge indicator compares price to ThinkorSwim’s advance/decline spread ($ADSPD) and the NYSE TICK. Our Cumulative TICK indicator keeps a running total of the TICK through the day.
The Cumulative Advance Decline Trading System plots 4 things in its own panel below the price chart. The declare lower; line at the top of the code puts it there, because a running count of +1s and -1s is on a different scale from price.
| Plot | What it shows | How it draws |
|---|---|---|
advDecLine |
The running total of the 6 symbols’ +1 and -1 scores | Line |
advDecLineAvg |
The 50-bar average of advDecLine |
Line |
bullishCrossOverSignal |
The line crossed above its average and the CCI is at +100 or higher | Up arrow, weight 3, on the average line |
bearishCrossOverSignal |
The line crossed below its average and the CCI is at -100 or lower | Down arrow, weight 3, on the average line |
The Cumulative Advance Decline study is a custom study, so you either import the download or paste the code into a new study.
If you haven’t added a custom study before, see our guide on how to import a study into ThinkorSwim.
The Cumulative Advance Decline study has 7 inputs: 6 symbols and a start date.
| Input | Default in the download | What it controls |
|---|---|---|
symbol_1 |
FB | The first symbol in the group |
symbol_2 |
AMZN | The second symbol |
symbol_3 |
AAPL | The third symbol |
symbol_4 |
NFLX | The fourth symbol |
symbol_5 |
GOOG | The fifth symbol |
symbol_6 |
MSFT | The sixth symbol |
startDateYyyyMmDd |
2020120 | The date the line starts, written as YYYYMMDD |
The symbol defaults are the 6 stocks from the viewer’s starting code. FB was Facebook’s ticker when I recorded the video. The company now trades as META, so enter META in symbol_1 if you want to keep it in the group.
The start date default in the download has 7 digits, 2020120. GetYYYYMMDD() returns 8-digit dates, and every one of them is a larger number, so every bar passes the check and the line covers your whole chart.
Type a full 8-digit date, such as 20201201 for December 1, 2020, and bars before that day plot 0. Step 3 below explains what the line shows from that day on.
The Cumulative Advance Decline code has 5 parts: the symbol inputs, a +1/-1 test for each symbol, the running total, the 50-bar average and the arrows. The viewer’s starting code already plotted the running total. Episode 1 of the series builds our anchored VWAP indicator, and you’ll find the rest of our thinkScript tutorials on one page.
The starting code had each ticker typed into the code itself, so changing Amazon to Microsoft meant editing every place it appeared. The input keyword fixes that. Anything defined with input shows up in the study settings window, where you can change it without opening the code.
input symbol_1 = "FB";
input symbol_2 = "AMZN";
input symbol_3 = "AAPL";
input symbol_4 = "NFLX";
input symbol_5 = "GOOG";
input symbol_6 = "MSFT";
Then I replaced each quoted ticker with its input and renamed the variables to match: symbol1, symbol2 and so on. To test it, I changed the first symbol from Facebook to Twilio in the settings, clicked OK, and the line redrew using Twilio.
Each symbol gets 4 lines. The first two read its close on the current bar and on the bar before. The last two are true/false tests: did it close higher, or did it close lower?
def symbol1 = close(symbol_1);
def symbol11 = close(symbol_1)[1];
def symbol1a = symbol1 > symbol11;
def symbol1b = symbol1 < symbol11;
ThinkorSwim treats true as 1 and false as 0. So symbol1a - symbol1b is +1 when the symbol closed up, -1 when it closed down, and 0 when it closed unchanged. The other 5 symbols repeat the same 4 lines with their own numbers.
TotalSum adds a value up across the bars on the chart. Here it adds the 6 scores, so each bar moves the line by anywhere from -6 to +6. I renamed the plot from the viewer’s variable name to advDecLine.
input startDateYyyyMmDd = 2020120;
def beyondStartDate = if GetYYYYMMDD() >= startDateYyyyMmDd then 1 else 0;
plot advDecLine = if beyondStartDate then TotalSum((symbol1a - symbol1b)+(symbol2a-symbol2b)+(symbol3a-symbol3b)+(symbol4a-symbol4b)+(symbol5a-symbol5b)+(symbol6a-symbol6b)) else totalsum(0);
Bars before the start date plot 0. The date check sits outside TotalSum, which adds on every bar either way. From the start date on, the line shows the total counted from your chart’s first bar. Common Problems below shows a fix.
A cumulative line on its own doesn’t show whether the current reading is high or low for the group. To see what the line normally is, I compare it to its average over the past 50 bars. On the daily chart I used, that’s 50 days.
plot advDecLineAvg = Average(advDecLine,50);
Average is a simple moving average. Clicking Apply adds this second line to the panel.
A cross through the average can mark a burst of momentum in the group, and the CCI confirms it. The bearish plot checks 3 things on one bar: the line is below its average, the line’s previous value was at or above the average, and the CCI is at -100 or lower.
plot bearishCrossOverSignal = if advDecLine < advDecLineAvg and advDecLine[1] >= advDecLineAvg and CCI() <= -100 then advDecLineAvg else double.nan;
plot bullishCrossOverSignal = if advDecLine > advDecLineAvg and advDecLine[1] <= advDecLineAvg and CCI() >= 100 then advDecLineAvg else double.nan;
When all 3 are true, the arrow sits on the average line.
Otherwise the plot returns double.nan and nothing draws. The bullish plot is a copy with every comparison reversed and the CCI at +100 or higher. Both use plot instead of def because they need to draw on the chart.
Clicking Apply right after adding the 2 plots changes nothing on the chart, because ThinkorSwim doesn’t know to draw them as arrows yet. These 4 lines set the painting strategy and give the arrows a line weight of 3 so they’re easy to see.
bearishCrossOverSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bullishCrossOverSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bearishCrossOverSignal.SetLineWeight(3);
bullishCrossOverSignal.SetLineWeight(3);
The CCI, or Commodity Channel Index, measures how far price has moved from its average, scaled by how far it usually strays. Readings above +100 or below -100 sit outside its usual range. In the video, I read +100 or higher as confidence in a bullish breakout and -100 or lower as confidence in a bearish one.
CCI() with empty parentheses runs the built-in study, with its default settings, on the symbol your chart shows. So the cross comes from the group, and the confirmation comes from your chart. In the video, I read the arrows as long or short timing for the tickers in the group, so load it on one of your 6 stocks.
The Cumulative Advance Decline example in the video uses the 6 default stocks on a daily chart. With the arrows on, I scrolled back through the group’s signals:
On that chart I called it a pretty reliable signal. That’s 3 arrows on one group of 6 stocks, so check the arrows on your own symbols and timeframe before you rely on them.
The Cumulative Advance Decline arrows are a read on the group’s momentum. In the video, I read them as the times to get long or short in the stocks that make up the group. The CCI half of each arrow comes from the chart you load the study on, so check that stock’s own chart before you act on an arrow.
An arrow doesn’t tell you where you’re wrong. Decide where your stop goes before you enter, and size the position so a stop-out costs a small, fixed share of your account.
The Cumulative Advance Decline study is a starting point. The average is one statistic applied to the line, and at the end of the video I suggest others you can add the same way: a standard deviation, the median, or linear regression techniques. None of them are in the download.
You can also change the 50 in Average(advDecLine,50) for a faster or slower average, or replace the 6 symbols with the stocks you trade.
Most Cumulative Advance Decline problems come from a symbol, the start date, or arrows that never print.
To count only from your start date, move the date check inside the sum, the way our anchored VWAP indicator does. Replace the plot advDecLine line with the line below. This edit is my suggestion and isn’t in the video or the download:
plot advDecLine = TotalSum(if beyondStartDate then (symbol1a - symbol1b)+(symbol2a-symbol2b)+(symbol3a-symbol3b)+(symbol4a-symbol4b)+(symbol5a-symbol5b)+(symbol6a-symbol6b) else 0);
If ThinkorSwim shows an error after an edit, our tutorial on how to fix bugs in thinkScript code covers how to track it down. You’ll find more free studies on our indicators page.
#Written by TOS Indicators 2020
#Home of the Volatility Box
#Indicator: Anchored Cumulative Advance Decline
#References TTM_Squeeze source code in TOS and Edge Signals Source Code
#Full Course Link: tosindicators.com/squeeze-course/
#Update 12/6/20 - Fixed line #50 which contained a typo. Thanks to Karl N. and Sidd N. for catching the error.
declare lower;
input symbol_1 = "FB";
input symbol_2 = "AMZN";
input symbol_3 = "AAPL";
input symbol_4 = "NFLX";
input symbol_5 = "GOOG";
input symbol_6 = "MSFT";
#FB
def symbol1 = close(symbol_1);
def symbol11 = close(symbol_1)[1];
def symbol1a = symbol1 > symbol11;
def symbol1b = symbol1 < symbol11;
#AMZN
def symbol2 = close(symbol_2);
def symbol21 = close(symbol_2)[1];
def symbol2a = symbol2 > symbol21;
def symbol2b = symbol2 < symbol21;
#AAPL
def symbol3 = close(symbol_3);
def symbol31 = close(symbol_3)[1];
def symbol3a = symbol3 > symbol31;
def symbol3b = symbol3 < symbol31;
#NFLX
def symbol4 = close(symbol_4);
def symbol41 = close(symbol_4)[1];
def symbol4a = symbol4 > symbol41;
def symbol4b = symbol4 < symbol41;
#GOOG
def symbol5 = close(symbol_5);
def symbol51 = close(symbol_5)[1];
def symbol5a = symbol5 > symbol51;
def symbol5b = symbol5 < symbol51;
#MSFT
def symbol6 = close(symbol_6);
def symbol61 = close(symbol_6)[1];
def symbol6a = symbol6 > symbol61;
def symbol6b = symbol6 < symbol61;
input startDateYyyyMmDd = 2020120;
def beyondStartDate = if GetYYYYMMDD() >= startDateYyyyMmDd then 1 else 0;
plot advDecLine = if beyondStartDate then TotalSum((symbol1a - symbol1b)+(symbol2a-symbol2b)+(symbol3a-symbol3b)+(symbol4a-symbol4b)+(symbol5a-symbol5b)+(symbol6a-symbol6b)) else totalsum(0);
plot advDecLineAvg = Average(advDecLine,50);
plot bearishCrossOverSignal = if advDecLine < advDecLineAvg and advDecLine[1] >= advDecLineAvg and CCI() <= -100 then advDecLineAvg else double.nan;
plot bullishCrossOverSignal = if advDecLine > advDecLineAvg and advDecLine[1] <= advDecLineAvg and CCI() >= 100 then advDecLineAvg else double.nan;
bearishCrossOverSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
bullishCrossOverSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
bearishCrossOverSignal.SetLineWeight(3);
bullishCrossOverSignal.SetLineWeight(3);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