Quant from Scratch by QuantWright

Testing Popular Ideas · Lesson 3

RSI

One indicator, two opposite rules, and the 32 versions hiding inside it.

Know the basics? Read the lesson notes

Up 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–
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?

With no down day the plain average of the losses is 0, so 100 × gain ÷ (gain + 0) = 100.
Any loss in those days keeps the RSI below 100, however big the up day. Only an average loss of 0 gives 100.
The RSI looks only at the changes in its last n days, not the price level. A record close after a down day is still below 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:

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

Average
Average gain–
Average loss–
RSI–

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.

Length (days)
Average
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:

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.

Reading
Long on–
Buys a year–

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?

Reversion made 11.8% a year; momentum −4.0% a year, a loss. They split the days, so the rise one caught is the rise the other missed.

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?

That adds the choices (2 + 4 + 2 + 2). Every choice goes with every other, so they multiply.
That's 2 × 4 × 2, which leaves out the reading. Each set of settings can be read both ways.
2 smoothings × 4 lengths × 2 pairs of levels × 2 readings = 32 versions.

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.

Costs
Beat holding?
The best?

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.

Your pick, 2015–2021–
Your pick, 2022–2024?
Its place of 32 in 2022–2024?

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.

Theory

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.

RS = AG ÷ AL
RSI = 100 − 100 ÷ (1 + RS) = 100 × AG ÷ (AG + AL)
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.

Worked example: your five days

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.

Holds when
  • 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).
Breaks when

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?

RS = 0.8 ÷ 0.2 = 4, so RSI = 100 − 100 ÷ 5 = 80. Or: 100 × 0.8 ÷ 1.0 = 80.
RS = 0.8 ÷ 0.2 = 4, so RSI = 100 − 100 ÷ (1 + 4) = 100 − 20 = 80. Or: 100 × 0.8 ÷ (0.8 + 0.2) = 80.
Theory

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.

AGt = AGt−1 + α × (Gt − AGt−1),   α = 1 ÷ n
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)
Worked example: your sixth day

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.

Holds when
  • 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.
Breaks when

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?

0.70 + (1.40 − 0.70) ÷ 14 = 0.70 + 0.05 = 0.75. It moved a fourteenth of the way toward 1.40.
0.70 + (1.40 − 0.70) ÷ 14 = 0.70 + 0.05 = 0.75. Or (0.70 × 13 + 1.40) ÷ 14 = 10.5 ÷ 14 = 0.75.

What this means for your test

Lesson E1's checklist (How we test an idea), used on the RSI, one habit at a time.

  1. 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.

Practice 1 of 3

A friend's spreadsheet

The situationA friend codes a 5-day RSI with the simple average. The last six closes were 50, 49, 51, 50, 48 and 49. Their sheet says the RSI is 57.1.

What should it be? Round to one decimal.

Hint: six closes give five changes. Both averages divide by 5, so the totals will do.

Changes −1, +2, −1, −2, +1. Gains 2 + 1 = 3, losses 1 + 1 + 2 = 4. RSI = 100 × 3 ÷ (3 + 4) = 42.9.
Changes −1, +2, −1, −2, +1. Gains 2 + 1 = 3, losses 1 + 1 + 2 = 4. RSI = 100 × 3 ÷ 7 = 42.9.
Their 57.1 is 100 × 4 ÷ 7: gains and losses swapped. A slip like that turns every "oversold" into "overbought".
Practice 2 of 3

"We tested every setting"

The situationA trading course says it tested the RSI reversion rule "on every setting": lengths of 2, 3, 4, 5 or 14 days; levels 10/90, 20/80 or 30/70; and selling either when the RSI crosses 50 or after 5 days.

How many versions did it test? Type a number.

Hint: multiply the number of choices for each setting.

5 lengths × 3 pairs of levels × 2 ways to sell = 30 versions.
Choices multiply: 5 lengths × 3 pairs of levels × 2 ways to sell = 30 versions.
It didn't say which smoothing. If it tried both, that's 60.
Practice 3 of 3

"Wins 87% of its trades"

The situationA short video says the 30/70 RSI reversion rule "wins 87% of its trades" from 2015 to 2021, with the best setting it found. The fine print: before costs.

Which question tells you the most?

You'd need them to rebuild the rule, but they don't show whether the result was luck. That takes later years, with costs.
Runs of winners tell you about streaks, not whether the rule beat holding on years it hadn't seen.
A rule can win most trades and still trail holding. Our best version won 7 of its 9 finished trades on the choosing years, and trailed holding by 7.3% a year on the held-out ones.
✓

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

Lesson notes

The RSI (relative strength index) turns a price's recent up and down moves into one number from 0 to 100: the share of recent movement that was up. A rule can read it two opposite ways, and each choice multiplies the versions: 32 here. On the lesson's simulated market, built with no pattern to find, the best of the 32 trailed holding on years it never saw. That is one simulated market; a real one needs its own test.

Up days and down days

A day's close is its last price; its change is its close minus the day before's. A rise is a gain, a fall a loss written as a positive size. Six made-up closes: 100, 103, 102, 104, 103 and 104. The changes are +3, −1, +2, −1, +1. Gains: 3 + 2 + 1 = 6. Losses: 1 + 1 = 2. Both averages divide by all 5 days (a down day is a gain of 0): average gain 6 ÷ 5 = 1.2, average loss 2 ÷ 5 = 0.4.

One number from 0 to 100

RSI = 100 × average gain ÷ (average gain + average loss), over the last n days. With n = 5: 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 (risen a lot lately) and below 30 oversold (fallen a lot lately). With the simple average, it is exactly 100 only when the last n days hold no down day, so the average loss is 0.

Two ways to keep the average

A sixth made-up day closes at 101, down from 104: a loss of 3. The simple average keeps only the last 5 changes, so the gain of 3 drops out: gains 2 + 1 = 3, losses 1 + 1 + 3 = 5, averages 0.6 and 1.0, RSI = 100 × 0.6 ÷ 1.6 = 37.5. Wilder's smoothing (from J. Welles Wilder Jr., who published the RSI in 1978): new average = (old × (n − 1) + today's) ÷ n; nothing drops out, older days fade. Gain (1.2 × 4 + 0) ÷ 5 = 0.96, loss (0.4 × 4 + 3) ÷ 5 = 0.92, RSI = 100 × 0.96 ÷ 1.88 = 51.1.

Length on a simulated market

A simulated market is made-up prices from a computer model; the lesson's rises a little on average, and otherwise past prices give no clue to which way the next move goes. In the lesson's simulated 2017, the 2-day RSI with Wilder's smoothing was below 30 on 77 days and the 21-day on 2. On this market, a short RSI swings to the extremes all the time and a long one rarely, so length decides how often a 30/70 rule trades.

Two readings of one number

Reversion (mean reversion: prices snap back) reads below 30 as a price fallen too far: buy, and sell when the RSI rises above 70. Momentum (moves carry on) reads above 70 as strength: buy, and sell when the RSI falls below 30. Each rule is long (owning the market) or flat (holding cash), and trades at the next day's open, its first price, because the close isn't known until trading ends.

On the lesson's simulated market, October 2015 to December 2021, with the 14-day RSI and Wilder's smoothing, holding (buying on day one, never selling) made 6.3% a year, the steady yearly growth giving the same total. Before costs, reversion made 11.8% a year and momentum −4.0%. The market has no pattern for either to find, so which reading came out ahead was luck. The opposite reading ran on the same days, so one reading looking good is a weaker finding than it sounds.

How many versions?

A version is one full set of choices: the smoothing (2), the length (2, 5, 14 or 21 days: 4), the levels (30/70 or the stricter 20/80: 2) and the reading (2). Choices multiply: 2 × 4 × 2 × 2 = 32 versions. The dropped reading still counts.

All 32, then the bill

In the lesson's simulation, 11 of the 32 beat holding before costs and 5 after. The costs are 0.1% of the price on every buy and sell (the fee and a slightly worse price than you saw). The best, the 14-day 30/70 reversion with Wilder's smoothing, was 5.5% a year ahead of holding before costs. These are the textbook settings, and on this market they happened to come out best of the 32. It makes about 3 buys and sells a year, each charged 0.1%: a bill of about 3 × 0.1% = 0.3% a year, so 5.5 − 0.3 = 5.2. The 2-day versions make up to 68 buys and sells a year.

Years it never saw

The versions were judged on 2015 to 2021. The lesson kept 2022 to 2024 aside; running the chosen version once on such years, with costs, is a held-out test. Holding made 10.0% a year there, and none of the 32 beat it after costs. The best came 16th of 32, the middle of the pack, which luck alone would give. It ended 7.3% a year behind holding. Three years is a small sample, on a simulated market with no pattern to find; a real market needs its own test.

Formula: the relative strength index

The relative strength (RS) is the ratio of the average gain to the average loss. The RSI rescales it to run from 0 to 100.

RS = AG ÷ AL
RSI = 100 − 100 ÷ (1 + RS) = 100 × AG ÷ (AG + AL)
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 still works when AL = 0, where AG ÷ AL would divide by zero.

Worked example: the lesson's five days

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.

Holds whenBreaks when

A bad or missing price sits in the last n days (lesson B4, Data quality).

Formula: 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), with the share α = 1 ÷ n (lesson E2). 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.

AGt = AGt−1 + α × (Gt − AGt−1),   α = 1 ÷ n
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)
Worked example: the lesson's sixth day

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 above.

Holds whenBreaks when

Two tests start on different dates: for the first weeks their Wilder RSIs differ on the same day, and so can their trades.

One more worked example

Made-up round numbers

Closes 80, 82, 81, 79, 83, n = 4: changes +2, −1, −2, +4. AG = (2 + 4) ÷ 4 = 1.5, AL = (1 + 2) ÷ 4 = 0.75. RSI = 100 × 1.5 ÷ 2.25 = 66.7.

A sixth close of 80 (a loss of 3), simple, the +2 drops out: AG = 4 ÷ 4 = 1, AL = (1 + 2 + 3) ÷ 4 = 1.5, RSI = 100 × 1 ÷ 2.5 = 40. Wilder: AG = (1.5 × 3 + 0) ÷ 4 = 1.125, AL = (0.75 × 3 + 3) ÷ 4 = 1.3125, RSI ≈ 46.2.

Common mistakes

Check yourself

  1. Closes 60, 58, 59, 56 and 57, simple average, n = 4. What is the RSI?
    Answer

    Changes: −2, +1, −3, +1. Gains: 1 + 1 = 2. Losses: 2 + 3 = 5. AG = 2 ÷ 4 = 0.5 and AL = 5 ÷ 4 = 1.25. RSI = 100 × 0.5 ÷ (0.5 + 1.25) = 50 ÷ 1.75 ≈ 28.6.

  2. A 14-day Wilder average gain was 0.50 yesterday, and today's gain is 1.20. What is today's average gain?
    Answer

    old + (today's − old) ÷ n = 0.50 + (1.20 − 0.50) ÷ 14 = 0.50 + 0.05 = 0.55.

  3. A test uses Wilder's or simple smoothing, lengths 3, 7 or 14, levels 30/70 or 25/75, and both readings. How many versions?
    Answer

    Choices multiply: 2 smoothings × 3 lengths × 2 pairs of levels × 2 readings = 24.

  4. An average gain of 0.3 and an average loss of 0.9. What is the RSI?
    Answer

    RSI = 100 × AG ÷ (AG + AL) = 100 × 0.3 ÷ 1.2 = 25. Check with the other form: RS = 0.3 ÷ 0.9 = ⅓, and 100 − 100 ÷ (1 + ⅓) = 100 − 75 = 25.

  5. In the lesson's simulation, the best of 32 versions led holding by 5.5% a year before costs and came 16th of 32 on years it never saw. What does that suggest, and what does it not settle?
    Answer

    It suggests picking the best of 32 bought nothing: 16th of 32 is the middle of the pack, which luck alone would give on a market with no pattern to find. It does not settle it alone: three years is a small sample, on one simulated market, and a real market needs its own test.

Try it yourself

The rule, written before the code. Daily closes. Gain = the day's change if above 0, else 0; loss = minus the change if below 0, else 0. Average gain and loss over n days, by the simple average of the last n, or by Wilder's smoothing started from the simple average of the first n. RSI = 100 × AG ÷ (AG + AL); 100 if AL = 0; 50 if both are 0. Reversion: if flat and the RSI is below the lower level, go long; if long and it is above the upper level, go flat. Momentum: the reverse. Long only. Decide at each close, fill at the next open. 0.1% of the price per buy and per sell, or no costs. Compared with buying at the first test close and holding, which pays 0.1% once.

Run the script. Every number in this lesson comes from one Python script (below) on SimMarket, the course's simulated market. Add an exit at 50, or a 3-day length, and watch the version count and the best result move.

By hand. In a spreadsheet with closes in column B from row 2: gain in C3 =MAX(B3-B2,0), loss in D3 =MAX(B2-B3,0), copied down. For a 14-day simple RSI, the average gain in E16 is =AVERAGE(C3:C16), the average loss in F16 =AVERAGE(D3:D16), and the RSI in G16 =100*E16/(E16+F16), all copied down. For Wilder's smoothing, keep E16 and F16 as they are and set E17 to =(E16*13+C17)/14 and F17 to =(F16*13+D17)/14, copied down.

Written by QuantWright from the published definition (Wilder, 1978: the formula only). No code from TradingView, LuxAlgo or other indicator scripts.

About QuantWright

QuantWright, which publishes this course, sells backtesting software. Its backtests include costs (commission, slippage, next-bar fills), and every result shows its code, data window, data source and costs. Its Honesty Score counts every distinct version your account has tested on that market in the last 30 days, so trying 50 variations does not make the best one look good. A held-out test re-runs the chosen strategy once over years kept out of sight; run it again in the same study and the product says it no longer counts. quantwright.com

The script needs simmarket.py, the course's simulated market, in the same folder.

The code behind every number (Python, SimMarket seed 20261109)
Loading…

Going further: opposite readings split holding

Once a level has been crossed, the reversion and momentum versions of the same settings are never long on the same day. Before costs, their daily results add up to holding's on every such day. So their growths multiply to holding's: (1 + reversion's % a year) × (1 + momentum's % a year) = 1 + holding's, over the same days. So if one beats holding, the other must lose money: (1 + holding's) ÷ (1 + the winner's) is below 1.

That makes "one reading beat holding" a weaker finding than it sounds: it always comes with "the opposite reading lost money on the same days". Counting the pair as two versions, as this lesson does, is the honest minimum.

For the preciseMarket: SimMarket Market 1, seed 20261109, effect=False (no planted pattern), daily bars, the same data and periods as lesson E2. First decision at the close of 9 Oct 2015 (the 200th day). Choosing years: that close to 31 Dec 2021 (6.23 calendar years); held-out: to 31 Dec 2024 (3.00 years); each version carries its position across 2022. % a year = exp(sum of daily log returns)1 ÷ years − 1, years = calendar days ÷ 365.25. Fill at the next open: the night before the open earns the old position, the day after it the new one. Costs: a factor (1 − 0.001) per buy or sell on the day the new position starts. Holding: bought at the first test close, paying 0.1% once after costs: 6.32% a year before costs and 6.30% after (choosing), 10.00% (held-out). The 14-day Wilder 30/70 pair, before costs: reversion 11.81% a year, momentum −4.03%, long on 50.0% and 49.6% of days, 1.61 and 1.44 buys a year; its first signal came on 19 Oct 2015, and holding's log growth before the first fill was −0.0577, which is why 1.1181 × 0.9597 = 1.073 rather than holding's 1.063 a year. Before costs 11 of 32 beat holding (median −5.26 points), after costs 5 (median −6.21). Best before and after costs: Wilder 14 30/70 reversion, +5.5 then +5.2 points, 3.05 trades a year (19 in all), 7 of 9 finished round trips gained after costs; held-out: 2.68% a year, 16th of 32. 2017, days with the RSI below 30 (Wilder's / simple): 2-day 77 / 91, 5-day 31 / 54, 14-day 9 / 24, 21-day 2 / 13; on the same 2017 day the 14-day RSIs by the two smoothings differed by up to 25.54 points. The Wilder 21-day 20/80 reversion version never bought. On the held-out years the top place went to the Wilder 21-day 20/80 momentum version: it was long on every held-out day, so it matched holding exactly.

Honest notes

Where this comes from