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

Free Trading School · Level 7 · Course 7.5

Results table and equity curve

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

Advanced · Backtesting in Python

Results table and equity curve

Wildflower Trading School · Lesson 7.5.3.1

Wildflower

In Level 4 you worked out a test's results by hand: win rate, expectancy, profit factor and drawdown. By the end of this lesson, you'll get all of them from a list of R results in a few lines. You'll also catch one easy drawdown mistake, and draw the equity curve.

1 of 9

What you'll learn

  • Work out the main numbers. Win rate, expectancy and profit factor take a few lines.
  • Draw the equity curve. cumsum adds up the R results, and plot draws the line.
  • Count the start as a peak. Clip the peaks at zero so early losses show in drawdown.
  • Track the longest losing run. Add one per loss, reset on a win, keep the record.
  • Check the code by hand. When your sums match, trust it with long lists.

Exercise

  1. Run these lines on the R column of your own test.
  2. Check the win rate and the expectancy by hand, and draw the equity curve.
  3. Then put the numbers on one page (A one-page results summary).

Next: Splitting data into build and test.

Quick check

  1. 1. What does r.cumsum() give you?

  2. 2. A test starts with three 1R losses. Without .clip(lower=0), what does the drawdown code miss?

  3. 3. Which line gives the expectancy of an R column called r?

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 “Splitting data into build and test” is waiting next time.

Read the full notes

From a column of results, gives the win rate, expectancy and profit factor in a few lines, cumsum gives the , and the drawdown needs the starting balance counted as a peak.

  • From a pandas column of R results, r: wins = r[r > 0], losses = r[r < 0]. = len(wins) / len(r); = r.mean(); = wins.sum() / -losses.sum() (Expectancy, Profit factor).
  • : equity = r.cumsum(). : equity − equity.cummax().clip(lower=0); its minimum is the (Drawdown).
  • The easy mistake: without .clip(lower=0), the starting balance isn't counted as a peak, and early losses go missing (−2R instead of −3R in the made-up list).
  • Longest losing run: a loop that adds one to a streak on each loss and resets it on a win, keeping the record (Longest losing run).
  • Plot: equity.plot(title="...") draws the curve with matplotlib. Made-up list: 12 trades, 41.7% win rate, 0.33R expectancy, profit factor 1.57, maximum drawdown 3R, longest losing run 3. More: How to read backtest results.

Worked example

Made-up numbers

Checking the code by hand

Made-up numbers · 12 trades · 5 wins totalling 11R · 7 losses of 1R each

  1. Win rate: 5 ÷ 12 = 41.7%
  2. Expectancy: (11 − 7) ÷ 12 = 0.33R a trade
  3. Profit factor: 11 ÷ 7 = 1.57
  4. Drawdown: from 0 down to −3 after three losses = 3R

The code and the sums agree, so the code can be trusted with long lists.

Your turn

Drill

Made-up numbers · R results: 2, −1, −1, 3, −1

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.