- SMA
- EMA
- RSI
- technical indicators
- moving average
- ewm()
- relative strength index
- momentum
- signal generation
Technical Indicators — Signal Generation
Scene: a systematic trading desk, 7:02 a.m. The head of the desk wants one line before the open: "Is this stock overheating, or washed out?" You have 100 days of closing prices and ten minutes. Systematic traders answer this question every morning by encoding market intuitions into mathematical signals. In this lesson you will build three of the most widely used indicators — SMA, EMA and RSI — and print a desk-ready indicator table.
📜 The Century-Long Debate: Do These Signals Work?
1884 — Charles Dow began publishing stock averages in the Customers' Afternoon Letter (predecessor to the Wall Street Journal, which he co-founded). Dow observed that markets moved in primary trends (months to years), secondary trends (weeks to months), and minor trends. His observations — now called Dow Theory — are the intellectual ancestor of every moving average crossover strategy.
1948 — Edwards & Magee published Technical Analysis of Stock Trends — the bible of chart patterns. It codified head-and-shoulders, support/resistance, and trend-following into a systematic framework. The book is still in print in its 11th edition.
1978 — J. Welles Wilder Jr. published New Concepts in Technical Trading Systems, introducing the RSI (Relative Strength Index), ATR (Average True Range), and Parabolic SAR. Wilder was a mechanical engineer who became a commodities trader. He computed RSI by hand using a calculator. It became one of the most widely used indicators in finance.
The academic counterpoint — Eugene Fama's 1970 Efficient Market Hypothesis (EMH) (Nobel 2013) argued that all public information is immediately priced into markets, making technical analysis useless. This created a 50-year intellectual war between academics ("markets are efficient, technical signals are random") and practitioners ("we make money with these signals").
The current consensus (2024): Some technical signals — particularly momentum (price trends over 3–12 months) and mean-reversion (RSI extremes over 2–5 days) — have been documented as statistically significant even in academic literature (Jegadeesh & Titman 1993, Lo & MacKinlay 1990). However, their magnitude is small, and transaction costs absorb most of the edge in standard implementations. The SMA and RSI you are building are not magic — they are structured ways to summarise price history that, in certain regimes, contain predictive signal.
How signals flow: the analysis pipeline
Simple Moving Average (SMA)
The plain average of the last n closes — every day gets an equal vote:
SMA_n = (P_t + P_t-1 + ... + P_t-n+1) / n
Exponential Moving Average (EMA)
The same idea, but recent closes count more — weights decay exponentially:
EMA_t = α · P_t + (1 − α) · EMA_t-1, α = 2 / (n + 1)
EMA reacts faster to recent price changes than SMA — useful in fast-moving markets. In pandas: df['close'].ewm(span=20, adjust=False).mean().
Relative Strength Index (RSI-14)
Wilder built RSI in two moves. First, a raw relative strength ratio:
RS = avg gain (14d) / avg loss (14d) ← a raw ratio, from 0 to infinity
Then — the step that made it famous — squeeze RS onto a bounded 0–100 dial:
RSI = 100 − 100 / (1 + RS)
| RS (raw ratio) | RSI (dial) | Desk reading |
|---|---|---|
| 0 — only losses | 0 | maximum oversold |
| 1 — gains = losses | 50 | perfectly balanced |
| 2.33 | 70 | overbought threshold |
| ∞ — only gains | 100 | maximum overbought |
- ›RSI > 70 → overbought → potential sell signal
- ›RSI < 30 → oversold → potential buy signal
🧠 The mental model: two memories and a mood dial
- ›SMA is a committee where every one of the last 20 days gets an equal vote. Slow, stable, democratic — and late to every party.
- ›EMA is the same committee, but recent days shout louder. Each older day's voice fades by a constant factor, so news moves it sooner.
- ›RSI is a mood dial for momentum. The raw ratio RS lives on 0→∞, so a fixed threshold like "70" would be meaningless. Wilder's transform
100 − 100/(1 + RS)pins every asset, every era, onto the same 0–100 dial — that is why "over 70" and "under 30" mean the same thing on any chart in the world. The rescaling is the indicator — and it is exactly the line you will write.
Your Task
The starter code already computes SMA-20, EMA-20 and the raw RS ratio, then prints the indicator table for the last 5 sessions plus a signal census. But one line is wrong: it stores the raw RS ratio in the rsi_14 column instead of applying Wilder's rescaling. Run it as-is and admire the damage — every reading sits near 1, so the x < 30 test brands 99 out of 100 days "Oversold". A ratio is not a dial.
Fix the single # 👉 YOUR TURN line.
Predict before you run: over the final 14 days of the sample, average gains ran about 1.6× average losses (RS ≈ 1.6). Do the mental arithmetic — 100 − 100/2.6 — is the stock overbought (>70), oversold (<30), or neutral? Then run and check yourself against the Latest RSI-14 line.