#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;}

Diversify your portfolio by investing in stocks from various industries

Updated on 2011-12-22

What do you need to have a well-diversified portfolio?

This post shows you a way to diversify your portfolio by investing in stocks from various industries.

By diversifying your portfolio, you can minimize your risk and achieve your long-term financial goals. Diversification is very important in today's volatile markets.

Trading System Entry & Exit Rules

- Consider only the top 2 performing stocks in each industry
- Buy the 5 stocks that have the highest correlation with the S&P 500 Index
- Sell all positions after a month and repeat the process

Diversify your portfolio by getting the top performing stocks in each industry

To create a short list of the best performing stocks in each industry, we need only two functions:

Perf: Calculates the performance of a security during the last N-Bars

a1 = perf(close, 25); // One-month return

Comp: Creates a composite or ranks assets based on a specified indicator (depends on the calculation method passed to the function)

a2 = comp(a1, "rank", industry(), close > 2) <= 2;

Parameter 1 & 2: Returns the rank of each stock based on the monthly return. Example: Stock that have the highest return will get a rank of 1

Param 3: Performs ranking separately for each industry. This means that we can get several stocks with a rank of 1.

Use the screener to get a clear view of how the grouping parameter of the composite function works. Refer to the formula in "Note 2" below.

Param 4: Ignores stocks that do not meet the filter criteria specified here. This means that low priced stocks will never get the lowest ranks.

Before moving to the next buy rule, I want to add an additional condition that prevents the simulator/portfolio from buying stocks whose industry field is not specified. "Note 1" below will show you how to fill the industry field of each U.S. Stock.

a2 = a2 and StringLength(Industry()) > 0;

Correlation with the S&P 500 Index

How to get the S&P 500 Index:

Depending on the downloader you are using, the symbol name of the S&P 500 Index may vary. The default downloader (Historical Stock Market Data) uses "^GSPC" to reference this index.

sp500 = GetSeries('^GSPC', Close);

The "GetSeries" function allows you to reference an external series. In the above example, we will obtain the close price series of the S&P 500.

How to calculate the correlation between the current stock and the S&P 500 Index:

The "correl" technical analysis function calculates the correlation between two time-series for the past N-bars.

a3 = correl(perf(close, 1), perf(sp500, 1), 25);

The above function returns the correlation, during the previous month, between the daily return of each analyzed stock and the daily return of the "^GSPC" index.

How to get those stocks that have the highest correlation:

Again, the "comp" function will come to the rescue here. Instead of the monthly return, the correlation time-series should be passed to the composite function.

a4 = comp(a3, "rank") <= 5;

a1 = perf(close, 25);
a2 = comp(a1, "rank", industry(), close > 2) <= 2;
a2 = a2 and StringLength(Industry()) > 0;

sp500 = GetSeries('^GSPC', Close);
a3 = correl(perf(close, 1), perf(sp500, 1), 25);
a4 = comp(a3, "rank", 1, a2) <= 5;

Portfolio Settings

- N-Bar Stop: 25
- Number of positions: 5

Note 1:

If the stocks in your database are not associated with industries then you can use one of following downloaders to fix this: Update the Industry Name of your U.S. Stocks or Industry and Sector Information for the US Stock Market.

Note 2:

I personally use the screener each time I want to debug and analyze the results of a portfolio or trading system's rules in a specific day.

Here is how to create a screen that displays the monthly return, rank and industry of each stock. This is also a good way to understand how the "comp" function works:

a1 = perf(close, 25);
a2 = comp(a1, "rank", industry(), close > 2);
filter = a2 < 5 and close > 2;

Note 3:

Here are two other posts that give you examples of entry and exit rules translated into QuantShare language:

Example of a trading system implemented in QuantShare Software

Note 4:

If you want to enter trades only at the beginning of a new month, you can add an additional rule to your portfolio/strategy:

buy = a4 and month() != ref(month(), 1);

Click on "Add a review" link below if you have any question or comment.