The advance/decline line is a market technical indicator that compares the daily number of increasing stocks with the daily number of decreasing stocks (negative return).
It is one of the most used market breadth indicators and commonly called A-D line. This indicator can be calculated for any group of stocks. You can for example compute the A-D line of NYSE stocks, A-D line of S&P 500 or A-D line of all stocks listed on U.S. Exchanges.
Here is the formula for the advance/decline line:
AD line = (# of advancing stocks – # of declining stocks) + yesterday's A/D line value
Before we start, read the following post to learn how to create a market composite:
How to create a composite index/indicator
Creating the advance/decline line
We could have built this line by creating two composites (one for advancing stocks and another one for declining stocks) then creating a custom indicator to calculate the AD line.
However, we will show you here how to create this market breadth indicator using a single composite item.
- Open the composites form, click on "Add", select a list of stocks then click on "Next"
- In the formula panel (Step 2), we must perform two composite calculations.
This can be done using the "AddComposite" function.
AddComposite("Adv", close > close, _CompCount);
AddComposite("Dec", close <= close, _CompCount);
composite = 1; // Not used here
Usually, a basic market indicator is calculated by taking the sum, average, min or max of the values returned by the "composite" variable (for each stock) for the same bar/date.
The "AddComposite" function allows you to create additional composites that can then be used in a C# script to perform additional calculations and return a single composite.
This is what we are going to do with this advance/decline market breadth indicator.
Creating the C# Script
Click on "Script" button located just below the "Formula" panel of the "Create Composite" form.
The composite script is executed once after the composite formula calculation is completed.
In order to create the advance/decline line, we must implement the AD line formula (shown above) inside the composite script.
How to get a reference to composites created using the "AddComposite" function (QuantShare Language):
double adv = Functions.GetVariable("Adv");
double dec = Functions.GetVariable("Dec");
The "adv" array now gets the number of advancing stocks for each trading bar, while the "dec" array gets the number of declining stocks.
Note that "adv" and "dec" arrays are synchronized, which means that for each array, the value at index 25 (for example) references the same date/time.
How to compute the AD line:
double comp = new double[adv.Length];
comp = adv - dec;
for(int i=1;i < adv.Length;i++)
comp[i] = (adv[i] - dec[i]) + comp[i - 1];
The above C# code, loops through each element of the array and then calculates the AD line for each trading bar.
Passing the result to the composite plug-in:
Now that we have created the AD line from the number of advancing and declining stocks, we would like to pass the new composite array (AD Line -> 'comp' Array) to the composite Plug-in.
Here is how to do this:
Using a Filter
Back to the composite formula, we can add a "filter" variable to instruct QS trading software to ignore some stocks.
Stocks whose filter variable value is False (equal to zero) on a specific bar will be ignored for that bar (not included in the composite calculation).
To create the advance/decline line of stocks trading above $50, simply add the following line:
filter = close > 50;
Advance Decline Data
I know you can now create your own advance and decline stocks composites.
However, in case you want to download ready-to-use advance/decline/unchanged data from several exchanges (AMEX, NASDAQ, and NYSE), here is a downloader link:
Advance-Decline-Unchanged issues for NYSE, AMEX and NASDAQ
Market Breadth Indicators in QuantShare Sharing Server
Several market breadth indicators were shared by our members, here are some:
Absolute Breadth Index
Arms Index - TRIN
Zweig Breadth Thrust
Advance Decline Ratio
Percentage Hughes Breadth Momentum Oscillator
Open ARMS Index - Open-10 TRIN