trade_summary

Defined in fynance.metrics

trade_summary(trades)[source]

Summary statistics of a set of round-trip trades.

Parameters:
tradesnp.ndarray[TRADE_DTYPE, ndim=1]

Output of extract_trades (or any array sharing its dtype and field semantics). max_win_streak / max_loss_streak are computed over trades in the order given, so pass a single asset’s trades (already time-ordered) for a meaningful streak – e.g. trades[trades['asset'] == k].

Returns:
dict of str to float
  • n_trades – number of trades.

  • win_rate – share of trades with ret > 0 (of all trades, not just wins + losses).

  • profit_factorsum(wins) / abs(sum(losses)); inf if there are winning trades and no losing ones; NaN if there are no trades or none with a nonzero return either way.

  • avg_win, avg_loss – mean ret of winning / losing trades (avg_loss is negative); NaN if that side is empty.

  • payoff_ratioavg_win / abs(avg_loss); NaN unless both sides are non-empty.

  • expectancy – mean ret over all trades.

  • max_win_streak, max_loss_streak – longest run of consecutive winning / losing trades (a breakeven ret == 0 trade breaks both streaks).

  • mean_bars, median_bars – mean / median holding period.

trades empty: n_trades, win_rate, max_win_streak and max_loss_streak are 0.0; every other field is NaN (there is nothing to average).

See also

extract_trades

Examples

>>> import numpy as np
>>> positions = np.array([1.0, 1.0, -1.0, -1.0, 0.0, 1.0])
>>> returns = np.array([0.5, 0.5, 0.5, -0.5, 0.0, 0.25])
>>> trades = extract_trades(positions, returns)
>>> s = trade_summary(trades)
>>> s['n_trades'], s['win_rate']
(3.0, 0.6666666666666666)
>>> round(s['profit_factor'], 4)
6.0
>>> s['max_win_streak'], s['max_loss_streak']
(1.0, 1.0)

An empty set of trades reports zero counts and NaN averages:

>>> empty = extract_trades(np.zeros(4), np.zeros(4))
>>> trade_summary(empty)['n_trades'], trade_summary(empty)['profit_factor']
(0.0, nan)