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
- Run these lines on the A trade’s result after commissions and fees are taken off.More in the glossary R column of your own test.
- Check the win rate and the expectancy by hand, and draw the equity curve.
- Then put the numbers on one page (A one-page results summary).
Quick check
1. What does r.cumsum() give you?
2. A test starts with three 1R losses. Without .clip(lower=0), what does the drawdown code miss?
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
Read the full notes
From a column of A result measured in units of the amount at risk. If a trade risks 4 points, 1R is 4 points and a 6-point gain is +1.5R.More in the glossary results, A free add-on for Python that works with tables of data, such as a file of price bars, a bit like a spreadsheet you control with code.More in the glossary gives the win rate, expectancy and profit factor in a few lines, cumsum gives the An account’s balance drawn trade by trade, or day by day, showing the path to the total.More in the glossary, 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]. The percentage of trades that made money. It doesn't show how big the wins or losses were.More in the glossary = len(wins) / len(r); The average result per trade over a list of trades: the total divided by the number of trades. It describes past trades, not the next one.More in the glossary = r.mean(); Everything a list of trades won, divided by everything it lost. Above 1, more was won than lost.More in the glossary = wins.sum() / -losses.sum() (Expectancy, Profit factor).
- An account’s balance drawn trade by trade, or day by day, showing the path to the total.More in the glossary: equity = r.cumsum(). A fall in an account’s balance from a high point. On a funded account, it also names the loss limit the balance has to stay above.More in the glossary: equity − equity.cummax().clip(lower=0); its minimum is the The biggest fall from a high point in an account’s balance over a test or a stretch of trading.More in the glossary (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 numbersChecking the code by hand
Made-up numbers · 12 trades · 5 wins totalling 11R · 7 losses of 1R each
- Win rate: 5 ÷ 12 = 41.7%
- Expectancy: (11 − 7) ÷ 12 = 0.33R a trade
- Profit factor: 11 ÷ 7 = 1.57
- 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
DrillMade-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.