6. Combining momentum and value: MACD, RSI and the P/E ratio
Level 4: Stock Screening
The two previous screeners are purely technical: they look only at price. The third adds a layer of fundamental valuation, combining three criteria of different natures at once.
The first criterion is the MACD, which you already know from Level 3, required to be greater than zero — a sign that the fast average is above the slow one, that is, upward momentum. The second is the 14-period RSI (Relative Strength Index), a momentum oscillator on a 0 to 100 scale that measures the speed and size of recent price changes; a value above 50 indicates that recent momentum is predominantly upward. The third is completely unlike the other two: the price/earnings ratio (P/E), a fundamental valuation measure that compares the share price with the company's earnings per share — a P/E below 10 may point to an undervalued share, provided the company's fundamentals are sound.
Computing MACD and RSI by hand, as you did in Level 3, works, but for a screener with many indicators a specialized library is worth it: pandas-ta, which adds more than 130 technical indicators directly onto a pandas DataFrame, without having to rewrite every formula.
import pandas_ta as ta
datos["MACD"] = ta.macd(datos["Close"]).iloc[:, 0]
datos["RSI14"] = ta.rsi(datos["Close"], length=14)With the three indicators available — two computed on the price series, one taken from the company's fundamentals — the final filter combines conditions of different natures with the & operator (row-level AND, the vectorized equivalent of the and you already know):
filtrado = datos[
(datos["RSI14"] > 50) &
(datos["MACD"] > 0) &
(pe_ratio < 10)
]This screener illustrates an important idea that goes beyond the code: mixing a momentum criterion (MACD, RSI) with a value one (P/E) is a deliberate bet on finding shares that are, at the same time, technically strong and fundamentally cheap — two qualities that in practice rarely coincide, precisely because the market tends to pay up for whatever is already rising. When a screener like this returns results, it is worth asking why the market has not already corrected that apparent inefficiency, before assuming you have found a safe opportunity.
To go deeper into this lesson
- Momentumin the glossary
- P/E ratio (price to earnings)in the glossary
- Momentum investing: what it is and its S&P 500 recordguide