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:

ItemAllLongShort
Trades389.0000389.00000
Closed Trades109.0000109.00000
Open Trades280.0000280.00000
Long Trades389.0000389.00000
Short Trades0.00000.00000
Winning Trades72.000072.00000
Losing Trades37.000037.00000
Win Rate0.66060.6606
Gross PnL3,490.68003,490.68000
Costs165.0250165.02500
Net PnL3,325.66003,325.66000
Open PnL763,525.0000763,525.00000
Best Trade636.5620636.5620
Worst Trade-16.2095-16.2095
Average Trade14.283714.2837
Average Winning Trade22.297322.2973
Average Losing Trade-1.3103-1.3103
Average PnL30.510630.5106
Average Winning PnL61.846361.8463
Average Losing PnL-30.4669-30.4669
Payoff Ratio2.02992.0299
Profit Factor3.95023.9502
Expectancy30.510630.5106
Expectancy Ratio1.00141.0014
SQN3.46013.4601
Kelly Criterion0.49330.4933
Average Duration3,021.09003,021.0900
Average Winning Duration3,541.26003,541.2600
Average Losing Duration2,008.86002,008.8600
Maximum Duration8,598.00008,598.0000
Minimum Duration8.00008.0000
Longest Winning Streak21.000021.00000
Longest Losing Streak8.00008.00000
Current Streak-2.0000-2.00000
Coverage1.00001.00000
Overlap Coverage1.00001.0000

36 rows

Parameters

get_trade_statistics accepts the following parameters.

ParameterTypeDescriptionExample
methodstrWhich 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"
bystr | NoneHow 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
roundingint | NoneThe number of decimals to round the results to. Defaults to None, which uses the Portfolio’s rounding.2
show_columnsstr | list[str] | NoneThe 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
Share