Trade Statistics
Calculate the statistics of the trades of the portfolio (get_trades).
How many trades there were, how many made money, how much they made and lost, the profit factor, expectancy, System Quality Number and Kelly fraction, how long they lasted, the longest winning and losing streaks and how much of the time a trade was held. Every statistic but the counts, the Open PnL and the coverage is calculated on the closed trades, in the order they closed.
- Win Rate = W = number of trades with \(\text{PnL} > 0\) / number of closed trades
- Payoff Ratio = R = mean(PnL⁺) / \(\vert \text{mean} (\text{PnL}^{-}) \vert\)
- Profit Factor = \(\sum \text{PnL}^{+} / \vert \sum \text{PnL}^{-} \vert\)
- Expectancy = W × mean(PnL⁺) − \(L \times \vert \text{mean} (\text{PnL}^{-}) \vert\), with L the share of losing trades, the mean PnL per trade
- Expectancy Ratio = \(\text{Expectancy} / \vert \text{mean} (\text{PnL}^{-}) \vert\), the expectancy in multiples of the average loss
- SQN = \(\sqrt{n}\) × mean(PnL) / std(PnL), with n closed trades and ddof=1
- Kelly Criterion = W − (1 − W) / R
- Coverage = share of trading days since the first transaction with at least one trade held, Overlap Coverage = share of those days with more than one
Tharp defines the expectancy and the System Quality Number on R-multiples, the PnL of every trade divided by the risk taken on it. A trade list has no stops, so the PnL in currency takes its place: the SQN is the same for any constant risk per trade, and with the average loss as the risk the Expectancy Ratio is the expectancy in R. Tharp’s later scoring caps n at \(100 (\sqrt{\min (n , 100)})\); here n is the number of trades, as in vectorbt and backtesting.py, while backtrader divides the standard deviation by n rather than n − 1. The Expectancy is vectorbt’s, in currency; backtesting.py’s “Expectancy [%]” is the mean trade return, the Average Trade here, and its profit factor uses the trade returns rather than the PnL.
Also known as: trade analysis, trade statistics, win rate, hit ratio, profit factor, System Quality Number.
Example
Install the Finance Toolkit with pip install financetoolkit -U, then call get_trade_statistics:
from financetoolkit import Portfolio
portfolio = Portfolio(example=True, api_key="FINANCIAL_MODELING_PREP_KEY")
portfolio.get_trade_statistics(by="direction")
Which returns:
| Item | All | Long | Short |
|---|---|---|---|
| Trades | 389.0000 | 389.0000 | 0 |
| Closed Trades | 109.0000 | 109.0000 | 0 |
| Open Trades | 280.0000 | 280.0000 | 0 |
| Long Trades | 389.0000 | 389.0000 | 0 |
| Short Trades | 0.0000 | 0.0000 | 0 |
| Winning Trades | 72.0000 | 72.0000 | 0 |
| Losing Trades | 37.0000 | 37.0000 | 0 |
| Win Rate | 0.6606 | 0.6606 | |
| Gross PnL | 3,490.6800 | 3,490.6800 | 0 |
| Costs | 165.0250 | 165.0250 | 0 |
| Net PnL | 3,325.6600 | 3,325.6600 | 0 |
| Open PnL | 763,525.0000 | 763,525.0000 | 0 |
| Best Trade | 636.5620 | 636.5620 | |
| Worst Trade | -16.2095 | -16.2095 | |
| Average Trade | 14.2837 | 14.2837 | |
| Average Winning Trade | 22.2973 | 22.2973 | |
| Average Losing Trade | -1.3103 | -1.3103 | |
| Average PnL | 30.5106 | 30.5106 | |
| Average Winning PnL | 61.8463 | 61.8463 | |
| Average Losing PnL | -30.4669 | -30.4669 | |
| Payoff Ratio | 2.0299 | 2.0299 | |
| Profit Factor | 3.9502 | 3.9502 | |
| Expectancy | 30.5106 | 30.5106 | |
| Expectancy Ratio | 1.0014 | 1.0014 | |
| SQN | 3.4601 | 3.4601 | |
| Kelly Criterion | 0.4933 | 0.4933 | |
| Average Duration | 3,021.0900 | 3,021.0900 | |
| Average Winning Duration | 3,541.2600 | 3,541.2600 | |
| Average Losing Duration | 2,008.8600 | 2,008.8600 | |
| Maximum Duration | 8,598.0000 | 8,598.0000 | |
| Minimum Duration | 8.0000 | 8.0000 | |
| Longest Winning Streak | 21.0000 | 21.0000 | 0 |
| Longest Losing Streak | 8.0000 | 8.0000 | 0 |
| Current Streak | -2.0000 | -2.0000 | 0 |
| Coverage | 1.0000 | 1.0000 | 0 |
| Overlap Coverage | 1.0000 | 1.0000 |
36 rows
Parameters
get_trade_statistics accepts the following parameters.
| Parameter | Type | Description | Example |
|---|---|---|---|
method | str | Which lots a sale closes first (see get_trades). Options are: “FIFO” (first in, first out: the oldest lots close first), “LIFO” (last in, first out: the newest lots close first). Defaults to “FIFO”. | "FIFO" |
by | str | None | How the trades are split. Options are: “direction” (the long and short trades next to all of them), “ticker” (every ticker next to all of them). Defaults to None, all trades together. | None, all trades together |
rounding | int | None | The number of decimals to round the results to. Defaults to None, which uses the Portfolio’s rounding. | 2 |
show_columns | str | list[str] | None | The columns to keep, a name or a list of names, matched on every column level, or the rows when no column has those names (such as the line items of a financial statement). Names that do not exist are logged, and when none exist the result is returned in full. Defaults to None, which keeps every column. | None, which keeps every column |
References
- Paper Tharp, V.K. (2008). “Van Tharp’s Definitive Guide to Position Sizing.” International Institute of Trading Mastery.
- Paper Tharp, V.K. (2006). “Trade Your Way to Financial Freedom.” 2nd edition, McGraw-Hill.
- Paper Kelly, J.L. (1956). “A New Interpretation of Information Rate.” Bell System Technical Journal, 35(4), 917-926.
- Library Shares similar logic as vectorbt’s Trades.stats
- Library Shares similar logic as vectorbt’s Ranges.coverage (coverage)
- Library Shares similar logic as backtesting.py’s Backtest.run (statistics)
- Library Shares similar logic as backtrader’s TradeAnalyzer
- Library Shares similar logic as backtrader’s SQN