calculate statistics on transactions and P&L for a symbol or symbols in a portfolio or portfolios

Share:

Description

This function calculates trade-level statistics on a symbol or symbols within a portfolio or portfolios.

Usage

1
2
3
4
tradeStats(Portfolios, Symbols, use = c("txns", "trades"),
  tradeDef = "flat.to.flat", inclZeroDays = FALSE)

dailyStats(Portfolios, use = c("equity", "txns"))

Arguments

Portfolios

portfolio string

Symbols

character vector of symbol strings, default NULL

use

for determines whether numbers are calculated from transactions or round-trip trades (for tradeStats) or equity curve (for dailyStats)

tradeDef

string to determine which definition of 'trade' to use. Currently "flat.to.flat" (the default) and "flat.to.reduced" are implemented.

inclZeroDays

TRUE/FALSE, whether to include zero P&L days in daily calcs, default FALSE for backwards compatibility.

Details

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 something like textplot or data.frame, with rounding, fancy formatting, etc. as your needs dictate.

Option inclZeroDays, if 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. Default is 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:

Josh Ulrich

for adding multiple-portfolio support, fixing bugs, and improving readability of the code

Klemen Koselj

for median stats, num trades, and win/loss ratios

Mark Knecht

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!

Value

a data.frame containing:

Portfolio

name of the portfolio

Symbol

symbol name

Num.Txns

number of transactions produced by addTxn

Num.Trades

number of flat to flat trades performed

Net.Trading.PL
Avg.Trade.PL

mean trading P&L per trade

Med.Trade.PL

median trading P&L per trade

Largest.Winner

largest winning trade

Largest.Loser

largest losing trade

Gross.Profits

gross (pre-fee) trade profits

Gross.Losses

gross trade losses

Std.Dev.Trade.PL

standard deviation of trade P&L

Percent.Positive

percent of trades that end positive

Percent.Negative

percent of trades that end negative

Profit.Factor

absolute value ratio of gross profits over gross losses

Avg.Win.Trade

mean P&L of profitable trades

Med.Win.Trade

median P&L of profitable trades

Avg.Losing.Trade

mean P&L of losing trades

Med.Losing.Trade

median P&L of losing trades

Avg.Daily.PL

mean daily realized P&L on days there were transactions, see dailyStats for all days

Med.Daily.PL

median daily P&L

Std.Dev.Daily.PL

standard deviation of daily P&L

Ann.Sharpe

annualized Sharpe-like ratio, assuming no outside capital additions and 252 day count convention

Max.Drawdown

max drawdown

Avg.WinLoss.Ratio

ratio of mean winning over mean losing trade

Med.WinLoss.Ratio

ratio of median winning trade over median losing trade

Max.Equity

maximum account equity

Min.Equity

minimum account equity

Note

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

Author(s)

Lance Levenson, Brian Peterson

See Also

chart.ME for a chart of MAE and MFE derived from trades, and perTradeStats for detailed statistics on a per-trade basis

Want to suggest features or report bugs for rdrr.io? Use the GitHub issue tracker.