Quant from Scratch by QuantWright

Introduction to Quant Finance · Lesson 19

Ruin

Why doubling down goes broke: a rule that wins almost every round and still loses the whole account.

Know the basics? Read the lesson notes

A rule for how much to put on a trade

Every trade in this lesson is a fair coin: it wins half the time. What you put on one trade is the stake. A win pays you the stake, a loss takes it.

We count in R, and here 1R is the first stake. The account starts at 100R. The rule only decides the stake: start at 1R, double the stake after every loss (people call this doubling down), and go back to 1R after every win. Flip ten times.

−1Rloss

Next stake 1R

Trade 0  ·  account 100R

Each chip is a trade's stake in R: + won, − lost. A bar marks a new round.

Ten trades and the account is at 102R. Look at what each win did. Lose 1R, lose 2R, then win 4R: −1 − 2 + 4 = +1R. However many losses came first, the win paid them all back plus 1R. Right now it is two losses deep, so the next stake is 4R. You'll carry on with this account in a few screens.

The ladder

A round runs from a 1R stake to its first win, or until the account is broke. Each doubling of the stake is one rung of a ladder: 1R, 2R, 4R and so on. As long as the account can pay for the next rung, a round that ends in a win ends 1R up, because each new stake is exactly 1R more than all the losses before it put together. The account can pay for a stake if the stake is no more than what is left. The ladder is as long as the account can pay for, so a bigger account affords more rungs.

The catch is in the losses. Start a fresh round with 100R and press Lose. Each press is one more loss in a row.

One round of the doubler, from 100R

Left: what is left in the account. Right: the stake lost on each trade.

Losses in a row0
Lost so far0R
Left100R
Next stake1R

Six losses in a row cost 1 + 2 + 4 + 8 + 16 + 32 = 63R. That leaves 37R, and the seventh rung should be 64R. The ladder has run out.

Our doubler then stakes everything that is left, which is called going all-in. If the all-in trade wins, the 37R doubles to 74R. If it loses, the account is at 0R. An account at 0R can't trade again: it is broke. Losing everything like this is called ruin.

So a round can end two ways. A won round is one where the ladder held: the win paid back every loss and the round ends 1R up. A lost round is one where the ladder ran out: the all-in trade then decides whether the account is broke or only far down, like 74R from 100R.

How rare is it for the ladder to run out?

From 100R, the ladder runs out after six losses in a row. A fair coin loses half the time, and one flip doesn't change the next. So six losses in a row is a half, then a half of that, six times over.

½ × ½ × ½ × ½ × ½ × ½ = 1/64.
Chances of one thing after another multiply, they don't add: ½ × ½ × ½ × ½ × ½ × ½ = 1/64.
It feels that rare, but each loss is one flip: ½ six times over is 1/64.
So a round that starts at 100R runs out of ladder about once in 64 rounds. To go broke you then need the all-in trade to lose too: a seventh loss in a row, about 1 in 128. Rare for one round, but the doubler starts a new round after every win, so it plays many rounds.

Back to your account

Your account was at 102R after ten trades, two losses into a round, with a 4R stake next. Carry on flipping.

+2Rwin
−2Rloss

Next stake 4R

Trade 10  ·  account 102R

Each chip is a trade's stake in R: + won, − lost. A bar marks a new round.

The account reached 108R on trade 14. Then seven losses in a row: six used up the ladder, and the seventh took the last 45R, staked all-in where the ladder wanted 64R. Broke on trade 21, after winning every round but one.

A guess before we go big

Now 10,000 accounts, each with 100R and the same doubling rule, each flipping its own fair coin for up to 1,000 trades. An account that goes broke stops. After 1,000 trades, what share of them are broke?

Keep that guess in mind. On the next screen you'll watch them run.

10,000 accounts, 1,000 trades

The chart draws 200 of the accounts. The counters below it count all 10,000. The coloured line is one account you can follow. Your own account from earlier broke on trade 21; press Follow another account to switch. Start with Play.

The doubler, 10,000 simulated accounts

Account in R, one line per account, 200 of them drawn. A line that hits 0R is broke and stays on the floor; a dot marks where it fell, darker where several fell close together.

Trade0
Rounds won so far–
Accounts broke0.0%
Following#1 (yours)

At trade 1,000, 81.2% of the accounts were broke. Over the same accounts, 99.2% of the rounds that finished were won: accounts that grew could pay for longer ladders, so their rounds failed even less often than 1 in 64.

Both numbers are true at once. A won round adds 1R; a lost round takes a big part of the account, and about half the time all of it (the all-in trade is one more coin flip). A new round starts after every win, so an account that lasts 1,000 trades plays about 500 rounds, and most accounts meet a losing run their ladder can't pay for long before then. The broke share was still rising when we stopped.

For the preciseAll 10,000 accounts together finished 1,971,686 rounds: 1,955,883 won and 15,803 lost. Of the trades they took, 50.0% were wins, as a fair coin gives: the stakes changed, the coin didn't. Half of the accounts that went broke were broke by trade 173. The biggest single stake was 256R. The middle survivor finished 507 rounds.

The same coins, a flat stake

Now give the same 10,000 accounts the same coins, trade for trade, with one change: the stake is always 1R. Switch between the two rules.

Two of the numbers below need a word. Line up all 10,000 accounts from poorest to richest: the middle account is the one in the middle (the median). The average account adds them all up and divides by 10,000.

Two stake rules, the same simulated coins, after 1,000 trades

Broke81.2%
Below the start81.4%
Middle account0R
Average account97.7R

With flat stakes, 0.1% went broke and about half ended below the start. A fair coin has no edge: on average a trade makes nothing. A coin with an edge wins more than half the time, so each 1R staked makes something on average. With no edge, luck alone leaves about half the accounts up and half down. The middle account is still at 100R. Under the doubler, the middle account is at 0R.

The averages barely differ: 97.7R and 99.9R, both 100R give or take luck. On a fair coin no stake rule changes the average. It only changes who ends up with it. Under the doubler, most accounts lose everything and the few that survive end far above 100R.

Theory

The martingale, for any account

Doubling the stake after every loss is an old betting system called the martingale. Two rules describe it for any account.

The ladder. The stakes go 1, 2, 4, 8R and so on, so k losses in a row lose 1 + 2 + 4 + … up to the k-th stake. That total is always one less than the next stake (1 + 2 + 4 = 7, one less than 8): 2k − 1. On a fair coin, a round starts with k losses in a row with chance ½ multiplied k times. An account of B pays for the largest k whose total still fits in B.

The average. A trade with stake s wins s half the time and loses s half the time, so on average it makes ½ × s − ½ × s = 0, whatever s is. A stake rule only decides which trade gets which stake. Every trade still averages 0, so the total averages 0 too. That is why the doubler and the flat rule both averaged about 100R. The stake rule changes the shape of the results (many small wins, a rare wipe-out), never the average.

L = 1 + 2 + … + 2k−1 = 2k − 1
P = (½)k, one round in 2k
r = the largest k with 2k − 1 ≤ B
Pbroke = (½)r+1, one round in 2r+1
average of a trade = ½ × s − ½ × s = 0
k
losses in a row, counted from the round's first 1R stake
L
what those k losses lose together, in R
P
the chance that a round starts with k losses in a row, on a fair coin
B
the account at the start of the round, in R
r
the rungs the account pays for in full
Pbroke
the chance a round ends broke: its r rungs and the all-in trade all lose. (If B is exactly 2r − 1, nothing is left for an all-in trade and Pbroke = (½)r.)
s
the stake of one trade, in R
Worked example: your 100R account

Your account started at B = 100R. For k = 6: 26 − 1 = 63, which fits in 100. For k = 7: 27 − 1 = 127, which doesn't. So r = 6: six losses in a row lose 63R and leave 37R, and a fair coin starts a round that way (½)6 = 1 time in 64. Going broke also needs the all-in seventh trade to lose: Pbroke = (½)7, 1 in 128.

At 1 in 128, each round survives with chance 127/128, and about 500 rounds multiply: 1 − (127/128)500 ≈ 98% broke. Our accounts did better, 81.2%, because each won round adds 1R, and an account that grows to 128R or more pays for a seventh rung with at least 1R left for the all-in trade, which halves its chance per round to 1 in 256.

The average: if a round stopped after those six rungs, then out of 64 rounds, on average 63 would win 1R and one would lose 63R. 63 − 63 = 0.

Holds when
  • each trade wins half the time, and one flip doesn't change the next;
  • the stake is set before each flip;
  • a win pays exactly the stake and a loss takes it, with no costs;
  • the stakes stay within what the account can pay, and the trading stops at some point.
Breaks when

The coin isn't fair. Then a trade averages its stake times the edge per 1R staked, so the stake rule does change the average: costs make the edge negative and every extra rung makes the average worse (lesson A16, Costs); with a real edge, how much to stake is Kelly (lesson C10).

A friend starts the same doubler with 500R. How many losses in a row use up its ladder: the most rungs it can pay for in full?

28 − 1 = 255 fits in 500, and 29 − 1 = 511 doesn't. So 8 rungs: eight losses in a row lose 255R and leave 245R, with a next stake of 256R. A fair coin starts a round that way (½)8 = 1 time in 256. Five times the money bought two more rungs than 100R's six.
Try k until 2k − 1 no longer fits: 28 − 1 = 255 fits in 500, 29 − 1 = 511 doesn't. So 8 rungs, losing 255R in all and leaving 245R; the ninth stake, 256R, would not fit.

What this means for your backtest

A backtest runs a rule over past data, trade by trade, to see what it would have done. A backtest of a rule that raises its stake after losses can look superb. Three habits follow. Turn them over one at a time.

  1. Read the stakes, not only the share of rounds won.

    A rule that raises its stake after losses wins most rounds by design, so a high share of rounds won tells you little. Find the biggest stake it placed and set it beside the account. From 100R, the sixth loss in a row is already a 32R stake, and the seventh stake would be 64R.

One last check

In the 10,000 accounts, 99.2% of the rounds were won and 81.2% of the accounts went broke. What makes both true?

The coins were ordinary: the same coins with a flat 1R stake left 0.1% of the accounts broke. The difference is the size of the wins and losses.
The doubler wins small and often and loses big and rarely: each won round adds only 1R, and a lost round can take the whole account. Over hundreds of rounds, most accounts meet that round.
A broke account can't recover: it has nothing left to stake. And the broke share was still rising at trade 1,000, so more trades would make it worse.
Practice 1 of 3

The bot that almost never loses

The situationA trading bot's sales page says it closed 99.4% of its rounds in profit over six months. The small print says it doubles its stake after every losing trade.

Which question tells you most about this bot?

A doubler's share of rounds won stays high right up to the round that ends it. In our accounts it was 99.2% while 81.2% of the accounts went broke.
A doubler wins most rounds by design. What decides its fate is a number of losses in a row its account can't pay for, and six months may simply not have contained that many.
The market matters less than the rule. Doubling after losses wins most rounds on any coin, and breaks on the first long run of losses in a row.
Practice 2 of 3

A bigger account

The situationA friend runs the same doubling rule in a spreadsheet, starting at 1R, but with 1,000R in the account instead of 100R.

When the ladder runs out (the next stake no longer fits), how much has the round lost? Type it in R.

Hint: add the rungs 1 + 2 + 4 + 8 + … while the total still fits in 1,000R. The next rung is the one that doesn't fit.

1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511R, with 489R left and a next stake of 512R. Nine losses in a row use up the ladder: a fair coin does that 1 time in 512 rounds. Then the 489R goes all-in, and if that tenth trade loses too, the account is broke. Rarer than 1 in 64, but each won round still only adds 1R.
Nine rungs fit: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 + 256 = 511R. That leaves 489R, and the tenth stake would be 512R, so the tenth trade goes all-in with 489R. If it loses, the account is broke. A shortcut: a ladder's total is always one less than its next rung.
Practice 3 of 3

Your own backtest

The situationYou backtest a rule over 300 trades. It starts with 100R, stakes 1R, doubles its stake after each losing trade and goes back to 1R after a win. Every one of its rounds was won, the most losses in a row was five, and the account line rises smoothly.

What does this backtest tell you about the rule's risk?

That's what a ladder looks like before its long run. Five losses in a row cost 31R, which 100R pays for. Seven in a row, with the all-in trade lost, would take the whole account.
The rule's big loss comes with more losses in a row than the account can pay for: from 100R, a seventh loss in a row with the all-in trade lost. In 300 trades the most was five, so the test never reached it. Its risk is in the runs the test never saw.
The doubler won 99.2% of its rounds on a fair coin, and 81.2% of its accounts still went broke. A high share of rounds won is what the doubling buys, not evidence that the rule makes money.
✓

Lesson done

Doubling the stake after every loss wins almost every round, 1R at a time. The first time the losses in a row outrun the money, it must stake everything left, and a loss there takes the whole account. It can't make a fair coin pay; it only decides which accounts end up with the money. When a backtest raises its stake after losses, judge it by its biggest stake and the most losses in a row it met, not by its share of rounds won.

Key ideas

Stake
what you put on one trade: a win pays it, a loss takes it
Martingale
double the stake after every loss, back to 1R after a win
Round
from a 1R stake to its first win (won, 1R up) or to broke; lost if the ladder ran out
Rung
one doubling of the stake; the account pays for only so many
Edge
what each 1R staked makes on average; a fair coin has none
Ruin
losing the whole account: a broke account can't trade again

Formulas

k losses in a row lose 2k − 1 (in R)

chance on a fair coin, from a round's start: (½)k

rungs an account B pays for: the largest k with 2k − 1 ≤ B

chance a round ends broke: (½)r+1 for r rungs; (½)r if B = 2r − 1 exactly

average of one fair trade: ½ × s − ½ × s = 0, for any stake s

Lesson notes

Doubling the stake after every loss (an old betting system called the martingale) wins small and often. The first time the losses in a row outrun the money, it must stake everything left, and a loss there takes the whole account. The stake is the amount put on one trade. On a fair coin, which wins half the time, it cannot make the average trade pay.

The rule, and why a win pays 1R

Every trade is a fair coin flip: a win pays the stake and a loss takes it. Money is counted in R: 1R is the first stake, and the account starts at 100R. The rule stakes 1R, doubles the stake after every loss and returns to 1R after a win. Two losses then a win: −1 − 2 + 4 = +1R. However many losses came first, the win pays them back plus 1R: each new stake is 1R more than all the losses before it together (1 + 2 + 4 = 7, next stake 8). A round runs from a 1R stake to its first win, or until broke. Each stake size is one rung of a ladder: 1R, 2R, 4R. It is a won round if the ladder held (the account could pay for every stake), and a lost round if the ladder ran out, even when the all-in trade that follows wins.

The ladder runs out

An account can pay a stake only if the stake is no more than what is left. From 100R, six losses in a row cost 1 + 2 + 4 + 8 + 16 + 32 = 63R. That leaves 37R, and the seventh stake should be 64R: the ladder has run out. The rule then goes all-in, staking everything left. A win doubles the 37R to 74R. A loss leaves 0R: broke, unable to trade again, which is called ruin. In a won round the ladder held and the round ends 1R up. In a lost round it ran out, and the all-in trade decides broke or only far down.

Rare once, likely over many rounds

Six losses in a row is ½ × ½ × ½ × ½ × ½ × ½ = 1/64, because chances of one thing after another multiply. A round from 100R runs out of ladder about once in 64, and goes broke only if a seventh trade loses too: 1 in 128. A new round starts after every win, and a round lasts about 2 trades on average (half of them end on the first trade, a quarter on the second, and so on), so 1,000 trades make about 1,000 ÷ 2 = 500 rounds. In the simulation, the first of the 10,000 accounts reached 108R on trade 14, then lost seven in a row: 108 − 63 = 45R was left for the all-in trade, which lost. Broke on trade 21.

10,000 accounts

In the lesson's simulation, 10,000 accounts each start at 100R and double after losses, each flipping its own fair coin, for up to 1,000 trades. Of finished rounds, 99.2% were won, yet 81.2% of the accounts were broke at trade 1,000, and in the simulation that share was still rising when it stopped. A won round adds 1R, a lost round takes a big part of the account and about half the time all of it.

With the same coins and a flat stake (always 1R), 0.1% went broke. The middle account (the median, halfway when ranked) ended at 100R flat and 0R doubling. The average account (total divided by 10,000) ended at 97.7R doubling and 99.9R flat, both 100R give or take luck. An edge is what each 1R staked makes on average; a fair coin has none (a coin that wins more than half the time has one). On a fair coin no stake rule changes the average. It only changes who ends up with it.

Formula: the ladder and the average

L = 1 + 2 + … + 2k−1 = 2k − 1
P = (½)k, one round in 2k
r = the largest k with 2k − 1 ≤ B
Pbroke = (½)r+1, one round in 2r+1
average of a trade = ½ × s − ½ × s = 0
k
losses in a row, counted from the round's first 1R stake
L
what those k losses lose together, in R
P
the chance that a round starts with k losses in a row, on a fair coin
B
the account at the start of the round, in R
r
the rungs the account pays for in full
Pbroke
the chance a round ends broke: its r rungs and the all-in trade all lose. (If B is exactly 2r − 1, nothing is left for an all-in trade and Pbroke = (½)r.)
s
the stake of one trade, in R
Worked example: the lesson's 100R account

The account started at B = 100R. For k = 6: 26 − 1 = 63, which fits in 100. For k = 7: 27 − 1 = 127, which doesn't. So r = 6: six losses in a row lose 63R and leave 37R, and a fair coin starts a round that way (½)6 = 1 time in 64. Going broke also needs the all-in seventh trade to lose: Pbroke = (½)7, 1 in 128.

At 1 in 128, each round survives with chance 127/128, and about 500 rounds multiply: 1 − (127/128)500 ≈ 98% broke. In the lesson's simulation, 81.2% of the accounts were broke, fewer than 98%. The lesson's explanation: each won round adds 1R, and an account that grows to 128R or more pays for a seventh rung with at least 1R left for the all-in trade, which halves its chance per round to 1 in 256.

The average: if a round stopped after those six rungs, then out of 64 rounds, on average 63 would win 1R and one would lose 63R. 63 − 63 = 0.

Holds when Breaks when

The coin isn't fair. Then a trade averages its stake times the edge (its average gain per 1R staked), so the stake rule does change the average: costs make that average gain negative and every extra rung makes the average worse (lesson A16, Costs); with a real edge, how much to stake is Kelly (lesson C10).

One more worked example: a 300R account

Worked example: a new account

A doubler starts with B = 300R. 28 − 1 = 255 fits in 300 and 29 − 1 = 511 doesn't, so r = 8. Eight losses lose 255R and leave 300 − 255 = 45R, and a fair coin starts a round that way 1 time in 28 = 256. The ninth stake would be 256R, so the round goes all-in with 45R: Pbroke = (½)9, 1 round in 512 (B is not exactly 28 − 1). If the all-in trade wins, 45R doubles to 90R, down 210R from 300R.

Common mistakes

Check yourself

  1. A doubler starts a round with 200R. How many losses in a row use up its ladder, and how much is left for the all-in trade?
    AnswerRungs are the largest k with 2k − 1 ≤ 200: 27 − 1 = 127 fits and 28 − 1 = 255 doesn't, so 7 losses. They leave 200 − 127 = 73R for the all-in trade.
  2. A doubler with 100R loses three trades in a row, then wins. Where does the account end?
    AnswerStakes 1R, 2R, 4R lose 7R, leaving 93R. The next stake, 8R, wins: 93 + 8 = 101R, 1R up.
  3. A bot won 98% of its rounds and doubles its stake after every loss. Does that show it makes money?
    AnswerNo. Doubling wins most rounds by design. Ask how big its stakes got and how many losses in a row the test met.
  4. How can 99.2% of rounds be won while 81.2% of accounts go broke?
    AnswerA won round adds only 1R, but a lost round takes a big part of the account, about half the time all of it. An account plays about 500 rounds in 1,000 trades, so most accounts meet a run of losses their ladder can't pay for.

Try it yourself

Run the simulation. Every line and number in this lesson comes from one Python script with a fixed random seed (below). Change the starting account, the coin's win chance or the stake rule and see what moves.

Play it by hand. Take a real coin and a sheet of paper. Start with 20R, so the ladder runs out after four losses (1 + 2 + 4 + 8 = 15R). On each line write the stake, the result and the account, and count how many rounds you win before too many losses in a row end it.

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

As an 84-second video: Backtesting from Scratch, Part 3, "The bot that almost never loses". Same 10,000 accounts, same numbers.

Going further: would more money fix it?

A bigger account pays for more rungs. To see what that does, picture a simpler doubler whose account pays for exactly k rungs and no more. A round then either wins 1R, or loses all k rungs: 1 + 2 + 4 + …, which adds up to one less than the next rung.

Then try a coin that wins 49% of the time instead of 50%. On every 1R staked it makes 0.49 − 0.51 = −0.02R on average: a negative edge.

The average result of one round

Each round wins 1R or loses every rung. Across: how many rungs the account pays for.

Smallest account for these rungs63R
The ladder breaks1 round in 64
A broken round's result−63R
Average round0R

On the fair coin the average round is exactly 0R at every size. With six rungs, 64 rounds bring on average 63 wins of 1R and one loss of 63R: 63 − 63 = 0. More rungs make the break rarer and bigger, never better on average. On the 49% coin every extra rung makes the average round worse: −0.13R with six rungs, −0.20R with nine. With a negative edge, more money on the ladder means more to lose.

Going further: flat stakes and a negative edge

Doubling is the fast road to ruin. A flat stake is a slow one, once the edge is negative. Take 100R, stake 1R on every trade, and keep going until the account either doubles to 200R or goes broke. This is the classic gambler's ruin problem, and its answer is exact, no simulation needed.

Flat 1R stakes from 100R: the chance to reach 200R before 0R

Coin wins
Chance to reach 200R first–

On a fair coin it's a coin toss: 50%. At 49.5% it's 11.9%, at 49% it's 1.8%, and at 48% about 3 in 10,000. Each trade loses very little on average, but the account takes thousands of trades to drift 100R either way, and every one of them leans the same way. With a negative edge and no stopping point, the chance of ruin is 100%: the only question is when.

For the preciseWith win chance p, q = 1 − p and r = q / p, the chance to reach N before 0 from i (flat 1-unit stakes) is (ri − 1) / (rN − 1), and i / N when p = ½. Here i = 100, N = 200: 0.5, 0.1192, 0.0180 and 0.0003. As N grows without limit, the chance of reaching it goes to 0 when p < ½, so ruin is certain.
For the precise: the books of one roundA ladder of k rungs on a coin with loss chance q wins 1R with probability 1 − qk and loses 2k − 1 R with probability qk. The average is (1 − qk) − qk(2k − 1) = 1 − (2q)k: 0 for q = ½, and 1 − 1.02k for q = 0.51 (−0.126R at k = 6, −0.195R at k = 9). More generally, no rule for choosing stakes turns a fair coin into a positive average when the stakes are limited by the account and the game stops in finite time (Doob's optional stopping theorem). The doubler's average final account, 97.7R, is 100R give or take luck: its standard error over 10,000 accounts is 2.1R.

Honest notes

Where this comes from