Skip to main content

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.

4 min read Reviewed Revision 1

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

RSI as a function of relative strength

0 20 40 60 80 100 00.5124810 Relative strength (average gain / average loss)
RSI. Computed from the definition, not from market data. The RSI is a bounded rescaling of the ratio of average gain to average loss: equal gains and losses give 50.

Worked numbers

Each result is recomputed from the formula shown.

WhatFormulaResult
sum of the 14 gains0.34 + 0.06 + 0.72 + 0.50 + 0.27 + 0.32 + 0.42 + 0.24 + 0.14 + 0.673.68
sum of the 14 losses (absolute)0.25 + 0.54 + 0.19 + 0.421.40
first average gaingains / 140.2629
first average losslosses / 140.1000
first relative strengthag1 / al12.63
first RSI(14)100 - 100 / (1 + rs1)72.44
average gain after a day of -0.28(ag1 * 13 + 0) / 140.2441
average loss after a day of -0.28(al1 * 13 + 0.28) / 140.1129
RSI after that day100 - 100 / (1 + ag2 / al2)68.38
average gain after a day of +0.41(ag2 * 13 + 0.41) / 140.2559
average loss after a day of +0.41(al2 * 13 + 0) / 140.1048
RSI after that day100 - 100 / (1 + ag3 / al3)70.95
relative strength at an RSI of 7070 / (100 - 70)2.33
EMA(12) smoothing factor2 / 130.1538
EMA(26) smoothing factor2 / 270.0741
MACD line105.0 - 103.21.8
signal line update0.2 * macd_line + 0.8 * 1.21.32
histogrammacd_line - signal0.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

Informational content. Not investment advice.