Skip to content
Wildflower
Join free
Backtesting in Python (optional)

Free Trading School · Level 7 · Course 7.5

Shuffling trades to see the range

Lesson 14 of 15 in Backtesting in Python (optional)Module 3: Results and checks

Advanced · Backtesting in Python

Shuffling trades to see the range

Wildflower Trading School · Lesson 7.5.3.4

Wildflower

In Level 4 you saw how reshuffling a list of trades shows the drawdowns the same trades could have given you. By the end of this lesson, you'll do it ten thousand times in code, and read the range it gives. You'll also know what shuffling can and can't tell you.

1 of 9

What you'll learn

  • Shuffle the trade order. The total stays the same, but the drawdown changes.
  • Run thousands of shuffles. Set a seed so you get the same shuffles each time.
  • Read the range. Look at the 5%, middle and 95% drawdowns, and see where yours sits.
  • Put it in dollars. Multiply each drawdown by your own 1R.
  • Check what it assumes. Shuffling treats trades as separate and can't add new ones.

Exercise

  1. Shuffle the R column of your own test 10,000 times.
  2. Find the 5%, 50% and 95% drawdowns, and see where your real order sits.
  3. Turn the 5% figure into dollars with your own 1R.

Next: Checking your code against a chart.

Quick check

  1. 1. You shuffle a trade list. What stays the same in every order?

  2. 2. Why pass a number like 42 to default_rng?

  3. 3. Losses in your strategy tend to cluster in one kind of market. What can shuffling do?

0 of 3 answered.

Finished this lesson?

Tick it off and keep your place

Make a free account and we'll remember where you are, so “Checking your code against a chart” is waiting next time.

Read the full notes

Shuffling the same trades thousands of times in code keeps the total but changes the , so you see the range of paths those trades could have taken. It assumes trades don't depend on each other.

  • Shuffling the order of the same trades leaves the total unchanged but changes the and losing runs (Shuffling trade order). Doing it thousands of times is a .
  • Code: load the trades with pd.read_csv; rng = np.random.default_rng(42) (the 42 makes the shuffles repeat); in a loop of 10_000, rng.permutation(r.values) makes a shuffled copy, and max_drawdown stores its deepest drawdown; pd.Series(worst).quantile([0.05, 0.5, 0.95]) reads the range.
  • Made-up list: 36 trades, total −3R. Real order −13R. Shuffled: 5% point −14R (at least one order in twenty that deep or deeper), middle −9R, 95% at least −6R, worst −21R.
  • In dollars, with a made-up 1R of $200: middle $1,800; 5% point $2,800; worst $4,200.
  • Limits: it assumes trades don't depend on each other; it can't add trades that never happened; it describes these trades, not future ones; a small list stays small (Sample size). More: Monte Carlo simulation.

Worked example

Made-up numbers

Putting the range into dollars

Made-up numbers · 1R = $200 · middle −9R · 5% point −14R · worst −21R

  1. Middle order: 9 × $200 = $1,800 drawdown
  2. 5% point: 14 × $200 = $2,800
  3. Worst of 10,000: 21 × $200 = $4,200
  4. Same 36 trades, same total, different order.

The range shows what the same trades could have put you through.

Your turn

Drill

Made-up numbers · 1R = $250 · 5% point of the shuffled drawdowns: −12R

Trading is risky. You can lose money. Test results are hypothetical, and a test can't tell you what will happen next. This lesson explains how testing works. It isn't advice.