This function calculates trade-level statistics on a symbol or symbols within a portfolio or portfolios.
1 2 3 4
character vector of symbol strings, default NULL
for determines whether numbers are calculated from transactions or round-trip trades (for tradeStats) or equity curve (for dailyStats)
string to determine which definition of 'trade' to use. Currently "flat.to.flat" (the default) and "flat.to.reduced" are implemented.
TRUE/FALSE, whether to include zero P&L days in daily calcs, default FALSE for backwards compatibility.
Every book on trading, broker report on an analytical trading system, or blog post seems to have a slightly different idea of what trade statistics are necessary, and how they should be displayed. We choose not to make value judgments of this type, aiming rather for inclusiveness with post-processing for display.
The output of this function is a
data.frame with named columns for each statistic.
Each row is a single portfolio+symbol combination. Values are returned in full precision.
It is likely that the output of this function will have more than you wish
to display in all conditions, but it should be suitable for reshaping for display.
Building summary reports from this data.frame may be easily accomplished using
data.frame, with rounding,
fancy formatting, etc. as your needs dictate.
TRUE, will include all transaction P&L,
including for days in which the strategy was not in the market,
for daily statistics.
This can prevent irrationally good looking daily statistics for strategies
which spend a fair amount of time out of the market. For strategies which
are always in the market, the statistics should be (nearly) the same.
FALSE for backwards compatibility.
If you have additional trade statistics you want added here, please share. We find it unlikely that any transaction-level statistics that can be calculated independently of strategy rules could be considered proprietary.
Special Thanks for contributions to this function from:
for adding multiple-portfolio support, fixing bugs, and improving readability of the code
for median stats, num trades, and win/loss ratios
for suggesting Profit Factor and largest winner/largest loser
WARNING: we're not sure this function is stable/complete yet. If you're using it, please give us feedback!
name of the portfolio
number of transactions produced by
number of flat to flat trades performed
mean trading P&L per trade
median trading P&L per trade
largest winning trade
largest losing trade
gross (pre-fee) trade profits
gross trade losses
standard deviation of trade P&L
percent of trades that end positive
percent of trades that end negative
absolute value ratio of gross profits over gross losses
mean P&L of profitable trades
median P&L of profitable trades
mean P&L of losing trades
median P&L of losing trades
mean daily realized P&L on days there were transactions, see
dailyStats for all days
median daily P&L
standard deviation of daily P&L
annualized Sharpe-like ratio, assuming no outside capital additions and 252 day count convention
ratio of mean winning over mean losing trade
ratio of median winning trade over median losing trade
maximum account equity
minimum account equity
TODO document each statistic included in this function, with equations
TODO add more stats, potentially PerformanceAnalytics: skewness, kurtosis, upside/downside semidieviation, Sortino
mean absolute deviation stats
more Tharpe/Kestner/Tradestation stats, e.g. K-factor RINA Index Percent time in the market Buy and hold return
Josh has suggested adding %-return based stats too
Lance Levenson, Brian Peterson
chart.ME for a chart of MAE and MFE derived from trades,
perTradeStats for detailed statistics on a per-trade basis