How do I rank stocks each month and form a portfolio?
Rank the stocks of each month in a long pandas panel with groupby().rank(), give the top two equal weights, and compute the return those weights earn in the next month.
After this lesson, you can rank the stocks of each month, give the top two equal weights and compute the return those weights earn in the next month.
How do I rank stocks within each month?
panel.groupby("date")["momentum"].rank(ascending=False, method="first") ranks the stocks of each month by their momentum, with rank 1 for the highest. In January, BBB has the highest momentum, 0.30, so it gets rank 1, and DDD gets rank 2.
The example is a long panel, as in the lesson on long and wide panels, of four made-up stocks at three month-ends. Each row holds a stock’s momentum signal at the month-end and its return over that month, both in decimals. pd.date_range(..., freq="ME") gives the month-ends, np.repeat repeats each date four times and np.tile repeats the four tickers for each month.
groupby("date") ranks each month separately, so a stock is compared only with the other stocks of its month. ascending=False gives rank 1 to the highest momentum, and method="first" breaks a tie by row order.
The following box builds the panel and ranks the stocks. In February, AAA and CCC tie at 0.25.
AAA gets rank 1 in February and CCC rank 2, because AAA’s row comes first.
What happens when two stocks tie?
With the default method="average", tied stocks share the mean of their places. AAA and CCC hold places 1 and 2 in February, so both get 1.5, and a rule that keeps only rank 1 would keep neither of them. method="first" keeps the number of picks fixed.
Each month, the stocks with ranks 1 and 2 get a weight of one half, and the other two get zero. panel["rank"] <= 2 marks the picks with True, .transform("sum") puts each month’s number of picks on every row of that month, and dividing the picks by that number gives the equal weights.
The weights chosen at a month-end earn the returns of the next month. The portfolio’s return in month \(t\) is
\[
r_{p,t} = \sum_i w_{i,t-1}\, r_{i,t}
\]
Here \(w_{i,t-1}\) is stock \(i\)’s weight chosen at the end of the month before, and \(r_{i,t}\) is its return in month \(t\).
As in the lesson on shifting rows, .shift(1) moves each value one row down. groupby("ticker") shifts each stock’s weights separately, so January’s weights land on February’s rows. .groupby(panel["date"]).sum(min_count=1) adds the products of each month and gives NaN for a month without any held weight.
The following box computes the portfolio’s return in each month.
date
2025-01-31 NaN
2025-02-28 0.04
2025-03-31 0.03
dtype: float64
February’s return is 0.04, half of BBB’s 0.02 plus half of DDD’s 0.06. January has no return, because no weights were chosen before it.
Your turn
Hold only the stock with rank 1 each month and print the portfolio’s returns. You should see 0.02 in February, from BBB, and 0.05 in March, from AAA, the first of the two tied stocks.
TipShow answer
.astype(float) turns True into a weight of 1.0 and False into 0.0.