Quant Analytics: from scratch to pro · Indicators, honestly measured · Beginner
Oscillators: what RSI and MACD compute
The Relative Strength Index and the MACD are two of the most widely displayed indicators. Both are plain arithmetic on past closes. This lesson computes them step by step so that you know exactly what the numbers are, and what they are not.
In this lesson
- Compute an RSI(14) from closing prices using Wilder's smoothing
- Compute the MACD line, signal line and histogram from their definitions
- Explain why an oscillator reading is a description of recent moves, not a forecast
- Say what the conventional 30 and 70 levels are and what they are not
Read first: Moving averages and their lag
An oscillator turns recent price changes into a number that moves within a fixed range, to show at a glance whether recent moves have mostly been up or mostly down. The two best known are the RSI and the MACD. Their definitions are short, and writing them out removes most of the mystery.
The Relative Strength Index
J. Welles Wilder introduced the RSI in his 1978 book New Concepts in Technical Trading Systems. It compares the size of recent gains with the size of recent losses over n periods, with n = 14 as the standard choice.
- For each period, take the change in close. A positive change is a gain; a negative one is a loss (stored as a positive number). The other side is zero.
- The first average gain and average loss are plain means over the first 14 changes.
- After that, use Wilder's smoothing:
avg_new = (avg_old x 13 + current) / 14. - Relative strength:
RS = average gain / average loss. RSI = 100 - 100 / (1 + RS).
The result lies between 0 (no gains in the window) and 100 (no losses). Equal gains and losses give 50. The chart shows the shape: RSI rises steeply for small RS and flattens out as RS grows.
Worked example
Take 17 closes: 44.00, 44.34, 44.09, 44.15, 43.61, 44.33, 44.83, 45.10, 45.42, 45.84, 46.08, 45.89, 46.03, 45.61, 46.28, 46.00, 46.41. The first 14 changes include ten gains and four losses.
- Sum of gains: 3.68. Sum of losses: 1.40.
- Average gain
3.68 / 14= 0.2629. Average loss1.40 / 14= 0.1000. RS = 0.2629 / 0.1000= 2.63, soRSI = 100 - 100 / 3.63= 72.44.
Now the close changes by -0.28 (to 46.00). Gain 0, loss 0.28:
- Average gain
(0.2629 x 13 + 0) / 14= 0.2441. - Average loss
(0.1000 x 13 + 0.28) / 14= 0.1129. - RSI = 68.38.
The next day adds +0.41:
- Average gain 0.2559, average loss 0.1048, RSI = 70.95.
The index moved from 72 to 68 and back above 70 in two days, on changes of a few tenths. Small changes in the inputs move it across the conventional lines.
What the 30 and 70 lines are
Wilder's text and later practice mark readings above 70 and below 30 as the stretched ends. These are labels chosen by convention. An RSI of 70 means RS = 2.33: average gains are 2.33 times average losses over the window. That is a description of the past 14 periods. Whether such readings are followed by anything is a testable claim, and the answer can differ by market, period and holding time. A price in a strong, steady trend keeps the RSI high or low for as long as the trend lasts.
MACD
Gerald Appel developed the Moving Average Convergence Divergence in the late 1970s. It uses two exponential averages (see moving averages).
- MACD line = EMA(12) - EMA(26), with
alphaof 0.1538 and 0.0741. - Signal line = EMA(9) of the MACD line, with
alpha = 0.2. - Histogram = MACD line - signal line.
If EMA(12) is 105.0 and EMA(26) is 103.2, the MACD line is 1.8. With yesterday's signal at 1.2, today's is 0.2 x 1.8 + 0.8 x 1.2 = 1.32 and the histogram is 0.48.
MACD is the gap between a faster and a slower average. It is positive when the short average is above the long one and crosses zero when they cross. The signal line smooths that gap, and the histogram says whether the gap is currently widening or narrowing. All three are built from the same closes, so they carry the same lag as the averages inside them. The values depend on the price scale, so MACD levels of two assets are not comparable.
What these indicators are not
They are not forecasts. Both are rescaled functions of past price changes. Neither contains information that is not in the price series, though they may arrange it in a more useful way. Whether they do is a question for evidence, not for the look of a chart; see measuring an indicator honestly.
Common mistakes
- Reading 70 as "about to fall" and 30 as "about to rise".
- Using a different smoothing from the one the software uses, then wondering why the numbers differ. Simple averages and Wilder's smoothing give different RSI values.
- Comparing MACD values across assets with different price levels.
- Tuning 12, 26, 9 or 14 to fit the past.
- Forgetting that the first values depend on the start of the data.
In practice
Check the definition before you compare numbers from two sources: period, smoothing, price used. Treat an oscillator as a summary of recent change and keep the question of use separate from the question of calculation. Analytics on FinUties applies measures like these to the markets it covers, so they are at hand when you read a chart.
RSI as a function of relative strength
Worked numbers
Each result is recomputed from the formula shown.
| What | Formula | Result |
|---|---|---|
| sum of the 14 gains | 0.34 + 0.06 + 0.72 + 0.50 + 0.27 + 0.32 + 0.42 + 0.24 + 0.14 + 0.67 | 3.68 |
| sum of the 14 losses (absolute) | 0.25 + 0.54 + 0.19 + 0.42 | 1.40 |
| first average gain | gains / 14 | 0.2629 |
| first average loss | losses / 14 | 0.1000 |
| first relative strength | ag1 / al1 | 2.63 |
| first RSI(14) | 100 - 100 / (1 + rs1) | 72.44 |
| average gain after a day of -0.28 | (ag1 * 13 + 0) / 14 | 0.2441 |
| average loss after a day of -0.28 | (al1 * 13 + 0.28) / 14 | 0.1129 |
| RSI after that day | 100 - 100 / (1 + ag2 / al2) | 68.38 |
| average gain after a day of +0.41 | (ag2 * 13 + 0.41) / 14 | 0.2559 |
| average loss after a day of +0.41 | (al2 * 13 + 0) / 14 | 0.1048 |
| RSI after that day | 100 - 100 / (1 + ag3 / al3) | 70.95 |
| relative strength at an RSI of 70 | 70 / (100 - 70) | 2.33 |
| EMA(12) smoothing factor | 2 / 13 | 0.1538 |
| EMA(26) smoothing factor | 2 / 27 | 0.0741 |
| MACD line | 105.0 - 103.2 | 1.8 |
| signal line update | 0.2 * macd_line + 0.8 * 1.2 | 1.32 |
| histogram | macd_line - signal | 0.48 |
Check yourself
Over 14 periods the average gain is 0.30 and the average loss is 0.10. What is the RSI?
RS = 0.30 / 0.10 = 3, so RSI = 100 - 100 / (1 + 3) = 75.
Can the RSI be 100?
Yes, if there were no losses in the window. Average loss is then zero and RS is infinite. It is 0 when there were no gains.
An EMA(12) is 105.0 and an EMA(26) is 103.2. What is the MACD line?
105.0 - 103.2 = 1.8.
Yesterday's signal was 1.2 and today's MACD is 1.8. What is today's signal line (EMA of 9)?
alpha = 2 / 10 = 0.2, so the signal is 0.2 x 1.8 + 0.8 x 1.2 = 1.32, and the histogram is 1.8 - 1.32 = 0.48.
A price rises every day for a month. What does the RSI show, and does it mean the price is about to turn?
It stays high, near the top of its range. The RSI only measures recent gains against losses. A persistent trend keeps it high for as long as it lasts, so a high reading alone says nothing about a turn.
Key terms
Relative Strength IndexMACDExponential moving averageMoving average
Sources
- New Concepts in Technical Trading Systems. Trend Research (J. Welles Wilder Jr.), 1978. Accessed 9 Nov 2026
- Technical Analysis: Power Tools for Active Investors. Financial Times Prentice Hall (Gerald Appel), 2005. Accessed 9 Nov 2026
- The Econometrics of Financial Markets. Princeton University Press (John Y. Campbell, Andrew W. Lo, A. Craig MacKinlay), 1997. Accessed 9 Nov 2026