Click here to Login

Trading System: Buy stocks with the highest Sharpe ratio

Updated on 2012-01-31

Today's trading system will be based on the composite function and rank method. This system will ensure that your portfolio gets the best performing securities, based on a metric of your choice (The Sharpe ratio in this example).

The trading system that we are going to implement here consists of:

- Considering medium and large capitalized stocks
- Ranking the list of stocks by their Sharpe ratio
- Buying the top 20
- Selling stocks whose rank increases above 40

The buy and sell process is done on a monthly basis (at the beginning of the month)

No stop rules (stop loss, trailing...) or money management rules are defined.

For instructions on how to create a trading system, please look at this post: Example of a trading system implemented in QuantShare Software

Another example of trading strategy implementation:
Day Trading: A trading system that combines intraday and EOD data

Medium and Large Cap Stocks

If you have access to a market capitalization or number of outstanding shares database then you can create a trading rule that gets data from this database and compares it value with "$1 billion". Below that level, a stock is considered to be a small cap stock.

filter1 = GetData("market_cap", "market_cap") < 1000; // Assuming your database name is "market_cap" and numbers are expressed in millions.

If you do not have access to such database, you can create a trading rule based on the price and volume fields:

filter1 = close * sma(volume, 10) > 500000;

The average trading volume in dollars must be higher 500,000. The rule will reject cheap/penny stocks as well as many small cap stocks.

Sharpe Ratio Indicator

The Sharpe ratio of the last three months can be obtained by typing:

a = sharpe(close, 75); // There are about 25 trading days in a month

The Sharpe ratio is a risk-adjusted measure calculated using the standard deviation and excess return (Excess return is ignored here when calculating this ratio for individual stocks).

Ranking Stock by their Sharpe Ratio

Besides computing indices and market indicators, the composite function (comp) is able to rank assets based on any indicator or metric of your choice.

The function syntax is very simple:

b = comp(a, "rank");

The above indicator will return the rank of each stock based on the three month Sharpe ratio.
The stock that has the highest Sharpe ratio gets the first rank and the stock that has the lowest Sharpe ratio gets the last rank (which is equal to the total number of analyzed assets).

In this trading strategy, we want to rank medium and large cap companies only and for this reason, we must pass the filter criterion we have created earlier to the composite function.

b = comp(a, "rank", 1, filter1);

The third parameter is used to group results (by sector, industry or market for example). It was disabled by passing it a fixed numeric value of "1".

You can get more info about the "Group" parameter here:
Create a stock index or a trading indicator using the composite tools
Diversify your portfolio by investing in stocks from various industries

For more info about the composite function:
How to create market indicators using the composite function - Part 1, Part 2, Part 3

Trading on a Monthly Basis

The idea here is to rebalance the portfolio once per month. At the beginning of each month, we must sell securities whose rank has increased and purchase those whose rank has decreased.

To detect a new month, we can compare the current bar month with the previous bar month:

newmonth = month() != ref(month(), 1);

The "ref" function is used to reference a previous bar value.

We can use the same idea to rebalance weekly using "week()" function or yearly using "year()" function.

How to Buy the Top 20

Ranking data are stored in variable "b". Investing in the top 20 consists of buying stocks that have a rank lower or equal to 20.

buy = b <= 20 and newmonth;

We have also added the new month rule so that the buying process is performed on a monthly basis.

How to Sell a Stock whose Rank Increases Above 40

The sell rule consists also of comparing the rank of each stock with the "40" threshold.

sell = b > 40 and newmonth;

Complete Trading System Formula

filter1 = close * sma(volume, 10) > 500000;
a = sharpe(close, 75);
b = comp(a, "rank", 1, filter1);
newmonth = month() != ref(month(), 1);

buy = b <= 20 and newmonth;
sell = b > 40 and newmonth;

The purpose of this post is to show you how to implement a trading system using QuantShare trading software.

3 comments (Log in)

QuantShare Blog
Search Posts

Recent Posts

Create Graphs using the Grid Tool
Posted 1187 days ago

Profile Graphs
Posted 1292 days ago

Previous Posts

More Posts


Create an account
Affiliate Program
Contact Us
Trading Forum
How-to Lessons
About Us
Terms of Use

Copyright 2024
Social Media
Follow us on Facebook
Twitter Follow us on Twitter
Follow us on Google+
RSS Trading Items

Trading financial instruments, including foreign exchange on margin, carries a high level of risk and is not suitable for all investors. The high degree of leverage can work against you as well as for you. Before deciding to invest in financial instruments or foreign exchange you should carefully consider your investment objectives, level of experience, and risk appetite. The possibility exists that you could sustain a loss of some or all of your initial investment and therefore you should not invest money that you cannot afford to lose. You should be aware of all the risks associated with trading and seek advice from an independent financial advisor if you have any doubts.