RSI Explained: The Math Behind the Most Popular Trading Indicator
RSI (Relative Strength Index) appears on the screen of virtually every trader. Created by J. Welles Wilder Jr. in 1978, it's one of the oldest and most used technical indicators. But most traders use it as a magic number — "below 30 = buy, above 70 = sell" — without understanding the math behind it.
What RSI Actually Measures
RSI measures the speed and magnitude of price changes. It compares the average gains on up-days to average losses on down-days over a rolling window (default: 14 periods). The result is a number between 0 and 100.
- RSI above 70 — asset has been rising hard, may be overbought
- RSI below 30 — asset has been falling hard, may be oversold
- RSI at 50 — equal gains and losses, neutral momentum
The Formula
Wilder's formula in three steps:
Where n = 14 by default (Wilder's original choice).
TypeScript Implementation (from TradeClaw)
Here's the exact implementation we use — no third-party TA library, just math:
function calculateRSI(closes: number[], period = 14) {
const result = new Array(closes.length).fill(NaN);
if (closes.length < period + 1) return result;
let gains = 0, losses = 0;
// Initial average: simple mean of first period
for (let i = 1; i <= period; i++) {
const delta = closes[i] - closes[i - 1];
if (delta > 0) gains += delta;
else losses -= delta;
}
let avgGain = gains / period;
let avgLoss = losses / period;
result[period] = avgLoss === 0 ? 100 : 100 - (100 / (1 + avgGain / avgLoss));
// Wilder's smoothing for all subsequent values
for (let i = period + 1; i < closes.length; i++) {
const delta = closes[i] - closes[i - 1];
avgGain = (avgGain * (period - 1) + Math.max(0, delta)) / period;
avgLoss = (avgLoss * (period - 1) + Math.max(0, -delta)) / period;
result[i] = avgLoss === 0 ? 100 : 100 - (100 / (1 + avgGain / avgLoss));
}
return result;
}How TradeClaw Uses RSI in Signal Scoring
RSI contributes up to 20 points in TradeClaw's 90-point signal scoring system:
| RSI Value | BUY score | SELL score |
|---|---|---|
| < 30 (Oversold) | +20 pts | −10 pts |
| 30–40 | +15 pts | +0 pts |
| 40–50 | +8 pts | +0 pts |
| 50–60 | +0 pts | +8 pts |
| 60–70 | +0 pts | +15 pts |
| > 70 (Overbought) | −10 pts | +20 pts |
RSI Limitations
RSI alone isn't enough. These are its known weaknesses:
- Trending markets — in a strong uptrend, RSI can stay above 70 for weeks. "Overbought" can mean "strong trend" not "reversal."
- Period sensitivity — RSI(14) behaves very differently from RSI(5) or RSI(30). Shorter = more signals, more noise.
- No price level context — RSI tells you about momentum, not whether the price is at a key level.
That's why TradeClaw combines RSI with MACD, EMA trend, Bollinger Bands, and Stochastic before emitting any signal. See the full scoring algorithm for details.
The TradeClaw signal engine is fully open source. View on GitHub · See calibration data · See accuracy stats