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

Buy the best/top rated stocks or how to create powerful rank based trading systems

Updated on 2011-06-21

Utilizing the power of the Composite function will allow you to create powerful and advanced screens and trading systems. For more information on how to use the composite function and the different calculation methods please refer to the blog posts I have list at the end of this article.

One simple yet very powerful trading strategy you can create is to rank different stocks, ETFs, Forex currency pairs or various securities based on a specific value or ratio and then buy the top performing ones and sell the one that are no longer in the top 10, 20 or 50.
The strategy I have just described can be implemented by typing no more than three lines of code. An example of a strategy that consists of buying the top 5 stocks with the lowest relative strength index value and selling the one that are no longer in the top 5, can be implemented as follows:

rank1 = Comp(-rsi(14), "rank");
sell = rank1 > 5;

By specifying "rank" in the second parameter of the composite function, we instruct QuantShare Trading Software to calculate the rank of each stock based on the time-series we gave in the first parameter. Because the function returns the rank of each analyzed stock, we then simply compare this value to a specific threshold; in this case, we have used "5" in the buy rule as we want to buy the top five stocks.

For the sell rule, we simply use the "higher than" operator to exit positions whose rank raised above 5.
Notice that we used the minus sign before the RSI function (technical analysis indicator) to tell the program that we want to rank stocks in an ascending order. The stock that has the lowest RSI value gets the first rank.

There are an infinite number of variables you can use in rank based trading systems. You can for example use the ratio of two different moving averages (a short and a long term SMA), the difference between stock and market returns (S&P 500 Index for example), the ratio of high to low prices or any fundamental analysis metric/ratio.

Using the filter argument

There are three more arguments in the composite function. After the calculation method parameter, you can define one of the following parameters:

Group: This allows you to group the data by a specific criterion. If you set for example "close > open" then on a given date, stocks that closed above their open price will be ranked separately from stocks that closed below their open price. We will get two stocks with a rank of one. Set "1" to ignore this field.

Filter: Allows you to reject stocks from the composite calculation. For example, you can ignore stocks whose price is below \$10 by specifying: Comp(rsi(14), "rank", 1, close >= 10);

Symbols Filter: Specify one or several symbols or a list of symbols. Example: A;GOOG;MSFT

Rebalance date

Using a daily time frame, the simulator or portfolio plug-in will buy and sell stocks, depending on their rank, each trading day. Some traders prefer to rebalance or generate new signals once a week or once a month. This can be accomplished by adding an additional date-based rule that will instruct the strategy to create new orders, for example, on Monday or at the beginning of each month.

Here is how to rebalance your strategy each Monday:

rank1 = Comp(-rsi(14), "rank");
daterule = Week() != ref(Week(), 1); // Today week number is different than yesterday's week number
buy = rank1 <= 5 and daterule;
sell = rank1 > 5 and daterule;

And here is how to do a monthly rebalance:

rank1 = Comp(-rsi(14), "rank");
daterule = Month() != ref(Month(), 1); // Today month is different that than yesterday's month
buy = rank1 <= 5 and daterule;
sell = rank1 > 5 and daterule;

Remove outliers from the data

An outlier is an observation or value that seems to deviate markedly from the other values or observations in the sample data.

Let us say for example that we have 10 stocks and that the one-bar return of these stocks is as follows:
5, 1, 0.5, 0.4, 0.02, -0.001, -0.1, -0.5, -0.52, -6

In the above sample, the outlines are clearly the stocks that had 5% and -6% one-bar returns. But how to mathematically identify these outliers?

The basic and simplest answer is to use the mean and the standard deviation. By calculating the average one-bar return and the standard deviation of these returns, we can create upper and lower limits and thus reject observations that are deemed "unlikely".

Here is how to calculate the mean and the standard deviation:
avg1 = Comp(perf(close, 1), "avg");
sdv1 = Comp(perf(close, 1), "sdv");
threshold1 = avg1 + 4 * sdv1;

And here is how to use the limit threshold with the composite function to reject outliers:
rank1 = Comp(perf(close, 1), "rank", 1, perf(close, 1) <= threshold1);

Blog posts about the composite function

Create a stock index or a trading indicator using the composite tools
How to create market indicators using the composite function - Part 1
How to create market indicators using the composite function - Part 2
How to create market indicators using the composite function - Part 3

When creating a simulation and performing backtests, you should not forget to update the maximum number of positions in the trading system. It does not make sense to invest in the top 10 stocks while your portfolio limits the number of holding positions to 5.