Skip to content
All library documents

Finding the Top Sharpe-Ratio Stocks in R

Article Quant Q&A · Author: Zoey Wong Cho Ting

Summary

The document shows how to select stocks with the highest Sharpe ratios from a table in R. It recommends vectorized operations over explicit loops, since loops can be slow, and presents two selection approaches: sorting Sharpe ratios in descending order and taking the first entries, or ranking the values and selecting the highest ranks.

An example dataset illustrates both methods and returns the same ten stocks, though in different row orders. The examples assume the ratios are already calculated and stored alongside stock identifiers; the document does not explain how to estimate Sharpe ratios, choose a risk-free rate, or handle missing values and ties. The ranking approach also relies on a strict rank threshold, which may need adjustment when ratios tie or when fewer than ten observations are available. The guidance concerns data selection, not whether a high historical Sharpe ratio predicts future performance.

Key ideas

  • Vectorized sorting or ranking can select high-Sharpe stocks without an explicit loop.
  • Sorting in descending order places the largest ratios first.
  • Ranking values provides another way to identify the highest ratios.
  • The examples assume Sharpe ratios have already been computed.

Tags

Full text
# How do I loop through all the stocks and find the 10 stocks with the highest Sharpe ratio using R program?


# How do I loop through all the stocks and find the 10 stocks with the highest Sharpe ratio using R program?












I am recently doing a project, which I need to apply Sharpe ratio to all the stocks. How do I loop through all the stocks and find the 10 stocks with the highest Sharpe ratio using R program? Thanks a lot!!

## Answer by Sandeep S. Sandhu (score 1)

https://quant.stackexchange.com/a/30207

I would recommend you should avoid using loops in R unless absolutely necessary since iteration is very slow in R. Some newer data classes improve performance, but in general its always best to use a "vectorised" method if available.

You could use either `order` or `rank` to get the 10 stocks with the highest Sharpe Ratio.

Lets assume you have the following data in R:

```
mydata <- as.data.frame( cbind( scrip=letters, sharpe_ratio=seq(from=-0.25,to=1.00,by=0.05) ) )
mydata$sharpe_ratio <- as.numeric( as.character(mydata$sharpe_ratio ) )
```

Method 1: Sort and then pick up top 10:

```
mydata[ order(mydata$sharpe_ratio, decreasing =TRUE) ,][1:10,]
   scrip sharpe_ratio
26     z         1.00
25     y         0.95
24     x         0.90
23     w         0.85
22     v         0.80
21     u         0.75
20     t         0.70
19     s         0.65
18     r         0.60
17     q         0.55
```

Method 2: Find the rank and pick top 10:

```
sharpe_ranks <- rank( mydata$sharpe_ratio )
mydata[ sharpe_ranks[ sharpe_ranks>(length(sharpe_ranks)-10) ] ,]
   scrip sharpe_ratio
17     q         0.55
18     r         0.60
19     s         0.65
20     t         0.70
21     u         0.75
22     v         0.80
23     w         0.85
24     x         0.90
25     y         0.95
26     z         1.00
```

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.