#twitter-follow { border: 0;position: fixed; top: 240px; left:0;} #live-support { border: 0;position: fixed; top: 370px; left:0;} #knowledge-base { border: 0;position: fixed; top: 170px; left:0;}

### Ranking stocks based on their correlation with the S&P 500 Index

Updated on 2011-06-14

I would like to share with you a script that will allow you to plot composite data (rank and percentile) on a chart. You might already know how to create composites using the "Comp" function but if this is not the case then I suggest you take a look at the following posts: How to create market indicators using the composite function - Part 1, Part 2, Part 3, More.

The usage of the "comp" function is restricted to some plug-ins. For example, the watchlist and the chart tools are not allowed to use the "comp" function simply because this function analyzes all stocks in your database and it takes some time to perform its calculation.
Therefore, there is no direct way, for example, to display the graph that corresponds to the rank of a stock for the one-bar rate of change compared to all stocks in a stock market/exchange.

The rest of this post will show you step by step how to overcome this problem, but before we start, let me show you some other benefits you can get:
- This technique will allow you to reduce the time it takes to conduct analysis/backtests
- You will be able to display Rank and Percentile charts and compare graphs of several stocks, securities or funds.
- You will be able to use the composite data with the watchlist tool (It is not possible to use directly the "Comp" function in the watchlist tool)
- You can export the data into Excel or any other application

Correlation between stocks and the S&P 500

The correlation between a stock and an index returns such as the S&P 500 is obtained using the "Correl" function.

Right click on a chart, edit the formula then type the following lines:
a = Correl(perf(close, 1), perf(GetSeries('^GSPC', close), 1), 60);
plot(a, "", colorBlue, ChartLine, StyleOwnScale);

NB: ^GSPC is the ticker symbol of the S&P 500. You might also use another stock index or an ETF fund such as SPY (Standard & Poor's 500 Index Depository Receipts) or IWM (iShares Russell 2000).

To rank stocks by their correlation with the S&P 500, we can create a screen, pivot table or a simulation and use the following formula:
b = GetSeries('^GSPC', close);
a = comp(correl(perf(close, 1), perf(b, 1), 60), "rank");

On a given date, the stock that has the highest correlation with the S&P 500 will get a value of one. The next most correlated stock will get the value 2 and so on.

We can improve the above formula by using the "filter" parameter to ignore penny stocks and those that are illiquid. The new formula becomes:
b = GetSeries('^GSPC', close);
lrule = close > 2 and close*sma(volume,10)>50000;
a = comp(correl(perf(close, 1), perf(b, 1), 60), "rank", 1, lrule);

The third parameter is used to group stocks so that results are returned for each group instead of individual stocks. The "group" option is disabled if we set its value to one.

However, as we explained above you will not be able to plot directly the stock rank on a chart. In order to overcome this, we will have to perform few steps.

Create a custom historical database

The first step is to create a custom historical database that will be filled with the ranking data.

- Select "Data" then "Edit databases"
- Select "Manage databases" tab, then create a new database (Name: Correl - Type: Historical)
- Select "Databases" tab, select "Custom" then "correl" database
- In "Add/update fields", type "sp500", select "double" field then click on "Add field"

Create a List of Symbols

The list of symbols we are about to create will be passed to the composite function and in return, this function will calculate ranks for stocks specified in this list.

- Select "Symbol" then "Symbols View"
- Right click on the new control then "Create a new Symbols Filter"
- Specify the symbols that you want to rank then click on "OK". For example, you can add all stocks, S&P 500 components or stocks listed on a particular stock exchange such as NYSE or NASDAQ.
- Type "Stocks" as the name for this list then click on "Save Item".

Implement the script

The script will calculate the rank over time of each stock and fill the "Correl" database with the data.
This script is implemented using the global script, a powerful tool that can be used to fully automate QuantShare.

- Select "Tools" then "Script Editor"
- Select "File" then "New"
- Type the script name then click on "OK"

The script is available here and here is a brief explanation of what it does:

- Creates a parser for the composite formula that ranks stocks based on their correlation with the S&P 500 Index. The higher the correlation is the higher the stock rank.
- Loops through each symbol specified in "Stocks" list, executes the parser, gets ranks data and stores data in the "sp500" field of the "Correl" database.

Once the script is implemented, you can add it to the bookmark panel for quick use later.

To run the script, click on "Execute" button or double click on it in the bookmark panel.

Display Stock Ranks Graph

The above script could take some time to complete depending on the number of symbols to rank.

Once the calculation is done:

- Select a chart, create a new pane then click on its "Add Indicator" icon (first icon on the top of the chart)
- Select "Database/field" tab, "correl" database, "sp500" field then click on "OK".

Display Ranks data

To display raw ranks data, open the database editor by selecting "Data" then "Edit databases"
Select "Custom", "correl" database then choose a symbol.

Here is a basic trading system that buys the top 5 stocks that have the highest correlation with the S&P 500 Index and sell these stocks if their rank increase above 20.

buy = GetData("correl", "sp500", Zero) <= 5;
sell = GetData("correl", "sp500", Zero) > 20;