Testing Popular Ideas · Lesson 3
RSI
One indicator, two opposite rules, and the 32 versions hiding inside it.
Know the basics? Read the lesson notesUp days and down days
Here are six made-up closes, which make five days of changes. Each day's close is its last price, and its change is its close minus the day before's. A rise is a gain; a fall is a loss, written as a positive size (a fall of 1 is a loss of 1).
Press Next day to sort each change into gains or losses.
Six closes, five changes (made up)
Top: the closes. Bottom: each change; orange for gains, grey for losses.
Gains: 3 + 2 + 1 = 6. Losses: 1 + 1 = 2. Over the five days, the average gain is 6 ÷ 5 = 1.2 a day and the average loss 2 ÷ 5 = 0.4 a day. Both divide by all 5 days: a down day counts as a gain of 0.
One number from 0 to 100
The RSI (relative strength index) turns those two averages into one number: the share of all the movement that was up, as a percent. Here the averages cover the last 5 days: n = 5, where n is how many days back we look.
RSI = 100 × average gain ÷ (average gain + average loss) = 100 × 1.2 ÷ (1.2 + 0.4) = 100 × 1.2 ÷ 1.6 = 75.
So 75% of the movement was up. Traders often call a reading above 70 overbought (it has risen a lot lately) and one below 30 oversold (it has fallen a lot lately).
What would make the RSI read exactly 100?
Two ways to keep the average
A sixth day comes in: the close falls from 104 to 101, a loss of 3. There are two common ways to update the averages. The length, written n, is 5 days here:
- Simple average: keep only the last 5 changes; the oldest, the gain of 3, drops out.
- Wilder's smoothing, from J. Welles Wilder Jr.'s 1978 book that defines the RSI: new average = (old average × 4 + today's) ÷ 5; in general (old × (n − 1) + today's) ÷ n. Nothing drops out. The old average is the simple one just built (gain 1.2, loss 0.4), and older days fade out gradually over many days.
Switch between them. Under each change is its weight, its share of the new average.
Six made-up days: which changes count, and how much
Simple: gains 2 + 1 = 3 and losses 1 + 1 + 3 = 5, so 3 ÷ 5 = 0.6 and 5 ÷ 5 = 1.0, and RSI = 100 × 0.6 ÷ 1.6 = 37.5. Wilder's: gain (1.2 × 4 + 0) ÷ 5 = 0.96, loss (0.4 × 4 + 3) ÷ 5 = 0.92, and RSI = 100 × 0.96 ÷ 1.88 = 51.1.
Same prices, same length, two RSIs. A rule that bought below 40 would buy on one and not the other.
The RSI on a simulated market
Now a year of a simulated market: made-up prices from a computer model, not a real stock. It rises a little on average; otherwise past prices give no clue to the next day's move (we built it so).
Below the price is its RSI. The usual length is 14 days; try 2 and 21 too, and switch the smoothing.
One year of closes and their RSI, 2017 (simulated)
Top: the daily close. Bottom: the RSI, with dashed lines at 30 and 70; orange dots: days below 30.
Switch to Wilder's smoothing and compare lengths: the 2-day RSI was below 30 on 77 days of 2017 and the 21-day on 2 (with the simple average: 91 and 13). A short RSI swings to the extremes all the time; a long one rarely gets there. The length decides how often a rule built on 30 and 70 trades at all.
Oversold, or strong?
The same RSI is read two opposite ways:
- Reversion (short for mean reversion: prices snap back): below 30 the price has fallen too far, so buy; sell when the RSI rises above 70.
- Momentum (moves carry on): above 70 the price is strong, so buy; sell when the RSI falls below 30.
Both rules either own the market, being long, or hold cash, being flat. Both trade at the next day's open, its first price, once the close has been seen (the close isn't known until trading ends; lesson E2). Show each on six years, with the 14-day RSI and Wilder's smoothing.
Two readings of one RSI, October 2015 to December 2021 (simulated)
Top: the close; shaded: days the chosen rule is long. Bottom: the 14-day RSI.
The shading flips. Once the RSI has first passed 30 or 70, a week or two in, exactly one of the two rules is long on every day. Each waits for the opposite level before it sells, so both trade rarely.
Which reading did better?
Over those six years, holding (buying on the first day and never selling) made 6.3% a year (the steady yearly growth that gives the same total). No costs yet: the fee and the slightly worse price on each trade come later. Which reading do you think did better?
Both readings against holding, before costs (simulated)
Account value, starting at 100. Flat stretches: the rule holds cash.
We built this market with no pattern for either reading to find, so which half of the days rose more was luck. Later we try 32 versions; on a market like this, some will look good by luck alone. When one reading looks good, its opposite ran on the same days.
How many versions?
To turn "trade the RSI" into a rule a computer can run, you pick the smoothing (Wilder's or simple), the length, the levels and the reading. Take four lengths, 2, 5, 14 and 21 days, and two pairs of levels, 30/70 and the stricter 20/80. Each set of picks is a version. How many versions is that?
All 32, then the bill
Now all 32 versions on the same six years. Before you look: how many beat holding? Set your guess and show them.
Then add the trading bill (the fee plus slippage, a slightly worse price than you saw) as one charge of 0.1% of the price on every buy and every sell (lesson A16, Costs).
32 versions, each one's % a year minus holding's (simulated)
One bar per version, best before costs on the left. Above the line: beat holding. With costs on, dark ticks show the before-costs height.
Before costs, 11 of 32 beat holding; after costs, 5. The best was the rule you just met, the 14-day 30/70 reversion with Wilder's smoothing: 5.5% a year more than holding. These are the textbook settings, and on this market they happened to come out best of the 32. It trades about 3 times a year (buys and sells), so its bill is about 3 × 0.1% = 0.3% a year, and +5.5 becomes +5.2. The 2-day versions trade up to 68 times a year, so costs take the most from them.
Years it never saw
So far every version was judged on 2015 to 2021, the choosing years. We kept 2022 to 2024 aside. Running the chosen version once on years like that is a held-out test (lesson B10, The held-out test).
Here are the four best versions after costs. Pick the one you would trade, then run the held-out years, costs included.
The four best after costs, then three held-out years (simulated)
Tiles: each version's lead over holding in 2015–2021. Lines: accounts of 100; orange: your pick; grey: the other three, numbered like the tiles; dotted: holding. Shaded: held-out years. Every line starts at 100 in 2015, so judge the held-out years by how each line moves inside the shading, not by which ends higher.
Holding made 10.0% a year in these years, and none of the 32 versions beat it after costs. This is a result for our coded rules on a simulated market with no pattern to find; a real market needs its own test, run the same way. Three years is a small sample, but the best of the 32 came 16th of 32, the middle of the pack, which is what luck alone would give: picking the best of 32 bought nothing.
The relative strength index
The relative strength index (RSI) compares the average gain with the average loss over about the last n days. Their ratio is the relative strength (RS). The RSI rescales it to run from 0 to 100.
- AG
- the average gain over about the last n days: each up day's rise, and 0 for a down day (with Wilder's smoothing, older days count less)
- AL
- the average loss over the same days, as a positive size
- RS
- how many times bigger the average gain is than the average loss
- n
- the length, in days; 14 is usual
The two forms are the same number: multiply the top and bottom of 100 ÷ (1 + AG ÷ AL) by AL: 100 × AL ÷ (AL + AG). Then 100 minus that is 100 × AG ÷ (AG + AL). The share form is handy: when AL = 0 the ratio AG ÷ AL divides by zero, but 100 × AG ÷ (AG + AL) still works.
AG = 1.2 and AL = 0.4, so RS = 1.2 ÷ 0.4 = 3, and RSI = 100 − 100 ÷ (1 + 3) = 100 − 25 = 75. The share form gives the same: 100 × 1.2 ÷ 1.6 = 75.
- the closes are in order, one per day, none missing or wrong;
- something moved: with AL = 0 there is no RS, so the RSI is set to 100 (50 if nothing moved).
A bad or missing price sits in the last n days (lesson B4, Data quality).
Try it on new numbers. Over 14 days the average gain was 0.8 and the average loss 0.2. What is the RSI?
Wilder's smoothing is a moving average
Wilder's update, (old × (n − 1) + today's) ÷ n, is the same as old + (today's − old) ÷ n: each day, the average moves 1 ÷ n of the way toward today's gain. That is an exponential moving average (EMA): each day moves the average a fixed share of the way toward today's value (lesson E2, Moving-average crosses). Here the share is α = 1 ÷ n.
E2's usual EMA uses α = 2 ÷ (m + 1) for a length m. For n = 14, 1 ÷ 14 = 2 ÷ 28, so m = 27: a 14-day Wilder average reacts like a 27-day EMA.
- Gt
- today's gain (0 on a down day); losses update the same way
- AGt−1
- yesterday's average gain; the first one is the simple average of the first n gains
- α
- today's share of the new average (the Greek letter alpha)
n = 5, so α = 0.2. Gain: 1.2 + 0.2 × (0 − 1.2) = 1.2 − 0.24 = 0.96. Loss: 0.4 + 0.2 × (3 − 0.4) = 0.4 + 0.52 = 0.92, as on the earlier screen.
- the average has run long enough to forget its start: the start's weight shrinks by (1 − 1 ÷ n) each day. Every test here had at least 200 earlier days of prices.
Two tests start on different dates: for the first weeks their Wilder RSIs differ on the same day, and so can their trades.
Try it. A 14-day Wilder average gain was 0.70 yesterday, and today's gain is 1.40. What is today's average gain?
What this means for your test
Lesson E1's checklist (How we test an idea), used on the RSI, one habit at a time.
- Write the exact rule before you run it.
"Buy when oversold" isn't a rule yet. Say the smoothing, length, levels and reading, when it sells, what price each trade gets, and the costs.
- Count every version, the opposite reading too.
The settings here made 32. The reading you drop still counts: it ran on the same days.
- Charge costs, beat holding, then hold out years.
The best of 32 beat holding by 5.2% a year after costs, then trailed it by 7.3% a year on years it never saw.
A friend's spreadsheet
What should it be? Round to one decimal.
Hint: six closes give five changes. Both averages divide by 5, so the totals will do.
"We tested every setting"
How many versions did it test? Type a number.
Hint: multiply the number of choices for each setting.
"Wins 87% of its trades"
Which question tells you the most?
Lesson done
The RSI is the share of recent movement that was up, from 0 to 100. Turning it into a rule takes choices (smoothing, length, levels, reading), and they multiply: 32 versions here. On a simulated market with no pattern to find, the best of 32 beat holding on the years used to pick it and trailed it on years it never saw.
Key ideas
- Average gain, average loss
- the average up move and down move over about the last n days
- RSI
- the share of recent movement that was up, 0 to 100; above 70 "overbought", below 30 "oversold"
- Wilder's smoothing
- an EMA with α = 1 ÷ n
- Reversion, momentum
- buy low and sell high on the RSI, or the opposite
- Version
- one full set of choices; each is a separate test
Formulas
RS = AG ÷ AL · RSI = 100 − 100 ÷ (1 + RS) = 100 × AG ÷ (AG + AL)
Wilder: AGt = AGt−1 + (Gt − AGt−1) ÷ n · versions = the choices for each setting, multiplied