In the last two lessons, we changed the question we asked about price.
Momentum
→ How many price units did price change?
ROC
→ How large was that change relative to the old price?
If you want to review those ideas first, see Momentum and Rate of Change (ROC).
Now we will ask a different question.
Over recent days,
were the upward moves stronger
or were the downward moves stronger?
That question leads us to the Relative Strength Index, or RSI.
1. Start with the Smallest Possible Change
Momentum and ROC compared two prices separated by a lookback. RSI begins one step closer to the raw data.
today's Close
-
yesterday's Close
=
one-period change
If price rises from 100 to 103:
103 - 100 = +3
If price falls from 103 to 101:
101 - 103 = -2
RSI starts by looking at a sequence of these small changes.
2. Split Every Change into Gain and Loss
RSI does not keep positive and negative changes in one column. It separates them.
If change = +3
Gain = 3
Loss = 0
If change = -2
Gain = 0
Loss = 2
Notice that Loss is stored as a positive magnitude.
A two-dollar fall becomes Loss = 2, not -2.
price change
↓
positive? ── yes ──→ Gain
│
no
↓
Loss magnitude
3. "Relative Strength" Does Not Mean Another Stock
The name can be confusing.
In RSI, Relative Strength does not mean:
AAPL
versus
S&P 500
It means a ratio inside the same price series:
Relative Strength
=
Average Gain
────────────
Average Loss
So we are comparing recent upward movement with recent downward movement.
4. Work Through a Tiny Five-Period Example
We will use a five-period example by hand because it is easier to see. The real chart later will use the common 14-period setting.
Prices
100
102
101
104
103
105
These six prices create five changes:
100 → 102 +2
102 → 101 -1
101 → 104 +3
104 → 103 -1
103 → 105 +2
Split them into gains and losses:
Change Gain Loss
+2 2 0
-1 0 1
+3 3 0
-1 0 1
+2 2 0
Now add each side.
Total Gain = 2 + 0 + 3 + 0 + 2 = 7
Total Loss = 0 + 1 + 0 + 1 + 0 = 2
Divide by five:
Average Gain = 7 / 5 = 1.4
Average Loss = 2 / 5 = 0.4
5. Turn the Two Averages into Relative Strength
Now calculate the ratio:
RS
=
1.4 / 0.4
=
3.5
In this small window, the average gain is 3.5 times the average loss.
But 3.5 has no fixed upper limit.
RSI transforms this ratio into a number between 0 and 100.
6. Transform RS into RSI
RSI
=
100 - 100 / (1 + RS)
Substitute RS = 3.5:
RSI
=
100 - 100 / (1 + 3.5)
=
100 - 100 / 4.5
≈ 77.78
That is our first five-period RSI value.
The important idea is not the number 77.78 by itself. It is the path that created it:
price changes
↓
gains and losses
↓
average gain / average loss
↓
Relative Strength
↓
RSI
↓
0 to 100 scale
7. Why Do We Need Six Prices for a Five-Period RSI?
A five-period RSI needs five changes.
But one change needs two prices.
Price 1 → Price 2 change 1
Price 2 → Price 3 change 2
Price 3 → Price 4 change 3
Price 4 → Price 5 change 4
Price 5 → Price 6 change 5
So:
5 changes
need
6 prices
That is why the first period RSI values in our list are None.
8. Wilder Did Not Recalculate a Fresh Average Every Day
The first average gain and average loss are simple averages. After that, RSI uses a recursive update usually called Wilder's smoothing.
If you want to understand why Wilder created RSI and why he chose a bounded 0–100 scale, see the story behind Welles Wilder and RSI.
new average
=
(previous average × (period - 1) + new value)
/
period
Add one more price to our hand example:
105 → 104
change = -1
Gain = 0
Loss = 1
The previous averages were:
Average Gain = 1.4
Average Loss = 0.4
With a five-period RSI:
New Average Gain
=
(1.4 × 4 + 0) / 5
=
1.12
New Average Loss
=
(0.4 × 4 + 1) / 5
=
0.52
Then:
RS = 1.12 / 0.52
New RSI ≈ 68.29
The oldest observations do not suddenly disappear. Their effect fades through the previous average.
9. Build RSI from Scratch
We need one new Learning Block:
relative_strength_index()
The heart of the function is this sequence:
change = price_now - price_before
gain = max(change, 0.0)
loss = max(-change, 0.0)
Then the first averages are:
average_gain = sum(gains) / period
average_loss = sum(losses) / period
And later values use Wilder's update:
average_gain = (
average_gain * (period - 1) + gain
) / period
average_loss = (
average_loss * (period - 1) + loss
) / period
Finally:
relative_strength = average_gain / average_loss
rsi = 100 - 100 / (1 + relative_strength)
The complete runnable Python file contains validation checks and edge-case handling.
10. What Happens When Average Loss Is Zero?
If every change in the smoothed history is a gain, then:
Average Loss = 0
We cannot divide by zero.
In that case, the RSI reaches its upper boundary:
RSI = 100
If both average gain and average loss are zero, our educational implementation returns the neutral midpoint:
RSI = 50
This keeps the edge case explicit instead of hiding it.
11. Validate the New Block Before Using Market Data
The Python file runs the tiny hand example first.
hand_prices = [
100,
102,
101,
104,
103,
105,
104,
]
For a five-period RSI, the expected values are:
first RSI ≈ 77.777778
next RSI ≈ 68.292683
The script checks those values before downloading AAPL data.
hand calculation
↓
Python function
↓
same answer
↓
then use real market data
This is more useful than trusting a long function because it looks complicated.
12. Reuse Every Block We Already Know
We do not rewrite the code from the earlier lessons.
simple_moving_average()
→ REUSE
exponential_moving_average()
→ REUSE
momentum()
→ REUSE
rate_of_change()
→ REUSE
draw_candlesticks()
→ REUSE
create_stacked_panels()
→ REUSE
relative_strength_index()
→ NEW
The new lesson adds one main algorithmic idea: compare smoothed gains with smoothed losses.
13. The Plot Block Grows from Three Panels to Four
In Phase 3-10:
fig, axes = create_stacked_panels(
panel_count=3,
height_ratios=[3, 1, 1],
)
For RSI, we do not change the function. We only change its inputs.
fig, axes = create_stacked_panels(
panel_count=4,
height_ratios=[3, 1, 1, 1],
)
Now:
axes[0] → price
axes[1] → momentum
axes[2] → ROC
axes[3] → RSI
The Frozen Plot Block stays familiar while the chart grows.
14. What Do the Four Panels Ask?
Panel 1
Candles + SMA 20 + EMA 20
→ What does the price path look like?
Panel 2
Momentum 10
→ How many price units changed?
Panel 3
ROC 10
→ How large was that change in percent?
Panel 4
RSI 14
→ How strong have recent gains been
relative to recent losses?
All four panels start from the same market data. The calculations simply ask different questions.
15. Why Is 50 a Natural Center?
Suppose:
Average Gain
=
Average Loss
Then:
RS = 1
Put that into the RSI formula:
RSI
=
100 - 100 / (1 + 1)
=
50
So the midpoint has a simple mathematical meaning.
RSI > 50
→ smoothed average gain > smoothed average loss
RSI < 50
→ smoothed average loss > smoothed average gain
RSI = 50
→ the two are equal
That is enough interpretation for this lesson. We do not need a trading rule yet.
16. Why Is RSI Bounded Between 0 and 100?
RS itself can become very large.
Average Gain
────────────
Average Loss
But the RSI transformation compresses the ratio.
very weak gains relative to losses
→ RSI moves toward 0
equal average gains and losses
→ RSI = 50
very strong gains relative to losses
→ RSI moves toward 100
That fixed scale makes RSI different from raw Momentum or ROC.
17. RSI Is Still Not a Prediction
If RSI is high, we can say something about the completed recent changes.
We cannot automatically say:
price must fall next
And if RSI is low, we cannot automatically say:
price must rise next
Those are trading hypotheses. They need a separate test.
For now:
RSI
=
description of recent gain/loss balance
not
=
guaranteed next move
18. Change One Thing Yourself
Start with:
rsi_period = 14
Then try:
rsi_period = 7
and:
rsi_period = 21
Ask:
Which RSI line reacts faster?
Which line changes more slowly?
Does the 50 crossing happen
on the same dates?
What price changes caused
the RSI to move?
The goal is not to find the best period.
The goal is to understand how the averaging horizon changes the indicator.
Check Your Understanding
- RSI begins with one-period price changes.
- Positive changes become gains; negative changes become positive loss magnitudes.
- Relative Strength inside RSI is Average Gain divided by Average Loss.
- The first average gain and loss are simple averages over the selected period.
- Later values use Wilder's recursive smoothing.
- A period-length RSI needs period + 1 prices to create period changes.
- RSI transforms the gain/loss ratio into a bounded 0–100 number.
- RSI = 50 when smoothed average gain equals smoothed average loss.
- RSI describes completed recent movement; it does not guarantee the next move.
create_stacked_panels()grows from three panels to four without changing the function.
What You Just Learned
Close prices
↓
one-period changes
↓
Gain / Loss
↓
Average Gain / Average Loss
↓
Relative Strength
↓
RSI
↓
0 to 100
If one idea stays in your head after this lesson, let it be this:
RSI is not magic and it is not a prediction. It is a structured way to compare recent upward and downward price movement on one bounded scale.
Sources
- J. Welles Wilder, New Concepts in Technical Trading Systems, Trend Research, 1978 — Google Books record.
- TA-Lib official RSI documentation — formula, 14-period default, first simple averages, and Wilder recursive smoothing.