3. Computing the Relative Strength Rating: comparing each share with the market
Level 4: Stock Screening
Before you can apply the Trend Template you need a figure that comes ready-made nowhere: each share's Relative Strength Rating against the market. It is computed in two steps.
The first step is to compute, for each share and for the index itself, the cumulative return over the period you care about — normally the last year.
retorno_indice = (1 + indice["Adj Close"].pct_change()).cumprod().iloc[-1]
retorno_multiplo = {}
for ticker in tickers:
df = descargar_precios(ticker)
retorno_accion = (1 + df["Adj Close"].pct_change()).cumprod().iloc[-1]
retorno_multiplo[ticker] = round(retorno_accion / retorno_indice, 2)pct_change() computes the day-by-day percentage return; adding 1 and applying cumprod() (cumulative product) gives the total compounded return over the period. Dividing the share's return by the index's return gives the "return multiple": a value of 2.0 means that share has returned twice as much as the market over the same period; a value of 0.5, half as much.
The second step turns that multiple into a percentile within the whole universe of shares, which is what is really known as the RS Rating:
import pandas as pd
tabla = pd.DataFrame(list(retorno_multiplo.items()), columns=["Ticker", "Retorno_multiplo"])
tabla["RS_Rating"] = tabla["Retorno_multiplo"].rank(pct=True) * 100
top_30 = tabla[tabla["RS_Rating"] >= tabla["RS_Rating"].quantile(0.70)]rank(pct=True) turns each value into its percentile within the full column, rather than into its absolute value — so a share with an RS_Rating of 85 means it has performed better than 85% of all the other shares in the universe, whatever the exact figure of its return multiple. Multiplying by 100 puts it on the 0 to 100 scale that Minervini uses in his seventh condition.
quantile(0.70) computes the value that leaves 70% of the data below it — filtering by RS_Rating >= that value keeps, by definition, the 30% of shares with the best relative behavior. It is exactly the same percentile idea that decides whether a share passes the seventh Trend Template condition or not.
To go deeper into this lesson
- Momentumin the glossary
- Momentum investing: what it is and its S&P 500 recordguide