perTradeStats: calculate trade statistics for round turn trades.

View source: R/perTradeStats.R

perTradeStatsR Documentation

calculate trade statistics for round turn trades.

Description

One 'trade' is defined as a series of transactions which make up a 'round turn'. It may contain many transactions. This function reports statistics on these round turn trades which may be used on their own or which are also used by other functions, including tradeStats and tradeQuantiles

Usage

perTradeStats(Portfolio, Symbol, includeOpenTrade = TRUE,
  tradeDef = "flat.to.flat", ..., includeFlatPeriods = FALSE,
  combn.method = c("rbind", "list"), envir = .blotter, Dates = NULL)

Arguments

Portfolio

string identifying the portfolio

Symbol

string identifying the symbol to examin trades for. If missing, the first symbol found in the Portfolio portfolio will be used

includeOpenTrade

whether to process only finished trades, or the last trade if it is still open, default TRUE

tradeDef

string, one of 'flat.to.flat', 'flat.to.reduced', 'increased.to.reduced' or 'acfifo'. See Details.

...

any other passthrough parameters

includeFlatPeriods

boolean, default FALSE, whether to include flat periods in output, mostly useful for Monte Carlo simulation as in txnsim

combn.method

string, either 'rbind' or 'list'. If missing, 'rbind' will be used

envir

the environment to retrieve the portfolio from, defaults to .blotter

Dates

optional xts-style ISO-8601 time range to run trade stats over, default NULL (will use all timestamps)

Details

Additional methods of determining 'round turns' are also supported.

Supported Methods for tradeDef:

flat.to.flat

From the initial transaction that moves the position away from zero to the last transaction that flattens the position make up one round turn trade for the purposes of 'flat to flat' analysis.

flat.to.reduced

The flat.to.reduced method starts the round turn trade at the same point as flat.to.flat, at the first transaction which moves the position from zero to a new open position. The end of each round turn is described by transactions which move the position closer to zero, regardless of any other transactions which may have increased the position along the way.

increased.to.reduced

The increased.to.reduced method is appropriate for analyzing round turns in a portfolio which is rarely flat, or which regularly adds to and reduces positions. Every transaction which moves the position closer to zero (reduced position) will close a round turn. this closing transaction will be paired with one or more transaction which move the position further from zero to locate the initiating transactions. acfifo is an alias for this method.

As with the rest of blotter, perTradeStats uses average cost accounting. For the purposes of round turns, the average cost in force is the average cost of the open position at the time of the closing transaction.

Note that a trade that is open at the end of the measured period will be marked to the timestamp of the end of the series. If that trade is later closed, the stats for it will likely change. This is 'mark to market' for the open position, and corresponds to most trade accounting systems and risk systems in including the open position in reporting.

Net.Trading.PL and MAE/MFE are calculated somewhat differently depending on the tradeDef. All three tradeDef types utilize Period.Realized.PL to calculate the trading P&L of the round turn trade. If the method is flat.to.flat, the cumulative sum of all transaction realized P&L's during the round turn is added. If the method is flat.to.reduced, the realized P&L is the period realized P&L of the closing transaction. If the method is increased.to.reduced (or the equvalent acfifo), the realized P&L for the round turn trade will be the period realized P&L potentially pro-rated by the difference in size between the initiating and closing transactions.

MAE and MFE are pro-rated for increased.to.reduced (or acfifo) and flat.to.reduced using the proportion of the total traded quantity over timespan that is attributable to this round turn. For these definitions of round turns, this is complicated because there can be multiple initating transactions which will adjust the average cost (and thus the net P&L) of the position. After pro-rating the cash measures, the percent and tick versions are constructed by dividing by the maximum notional position cost and the tick value, respectively.

If includeFlatPeriods=TRUE, perTradeStats will include periods when the series is flat (holds no position). Flat periods at the beginning of the series will be removed, as they are presumed to hold no information, and may be easily retrived if desired. This information is likely most useful in constructing stylized facts about the trading style, calculating values such as time in market. It is also extremely useful for Monte Carlo simulation of random trading strategies with similar style to the series under investigation. For more information on this latter use, see txnsim.

Value

A data.frame containing:

Start

the POSIXct timestamp of the start of the trade

End

the POSIXct timestamp of the end of the trade, when flat

Init.Qty

transaction quantity initiating the trade

Init.Pos

position held after the initiating transaction of the round turn trade

Max.Pos

the maximum (largest) position held during the open trade

End.Pos

the remaining quantity held after closing the trade

Closing.Txn.Qty

the transaction quantity which closes the round turn trade

Num.Txns

the number of transactions included in this trade

Max.Notional.Cost

the largest notional investment cost of this trade

Net.Trading.PL

net trading P&L in the currency of Symbol

MAE

Maximum Adverse Excursion (MAE), in the currency of Symbol

MFE

Maximum Favorable Excursion (MFE), in the currency of Symbol

Pct.Net.Trading.PL

net trading P&L in percent of invested Symbol price gained or lost

Pct.MAE

Maximum Adverse Excursion (MAE), in percent

Pct.MFE

Maximum Favorable Excursion (MFE), in percent

tick.Net.Trading.PL

net trading P&L in ticks

tick.MAE

Maximum Adverse Excursion (MAE) in ticks

tick.MFE

Maximum Favorable Excursion (MFE) in ticks

duration

difftime describing the duration of the round turn, in seconds

If Symbol is a vector with more than a symbol, return is either a data.frame containing the above statistics for all the symbol specified (default) or a list containing the above statistics for each symbol specified, depending on the combn.method chosen.

Author(s)

Brian G. Peterson, Jasen Mackie, Jan Humme

References

Tomasini, E. and Jaekle, U. Trading Systems - A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)

See Also

chart.ME for a chart of MAE and MFE derived from this function, and tradeStats for a summary view of the performance, and tradeQuantiles for round turns classified by quantile.


braverock/blotter documentation built on Feb. 13, 2023, 1 p.m.