View source: R/perTradeStats.R
perTradeStats | R Documentation |
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
perTradeStats(
Portfolio,
Symbol,
includeOpenTrade = TRUE,
tradeDef = "flat.to.flat",
...,
includeFlatPeriods = FALSE,
combn.method = c("rbind", "list"),
envir = .blotter,
Dates = NULL
)
Portfolio |
string identifying the portfolio |
Symbol |
string identifying the symbol to examin trades for. If missing, the first symbol found in the |
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 |
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) |
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
.
A data.frame
containing:
the POSIXct
timestamp of the start of the trade
the POSIXct
timestamp of the end of the trade, when flat
transaction quantity initiating the trade
position held after the initiating transaction of the round turn trade
the maximum (largest) position held during the open trade
the remaining quantity held after closing the trade
the transaction quantity which closes the round turn trade
the number of transactions included in this trade
the largest notional investment cost of this trade
net trading P&L in the currency of Symbol
Maximum Adverse Excursion (MAE), in the currency of Symbol
Maximum Favorable Excursion (MFE), in the currency of Symbol
net trading P&L in percent of invested Symbol
price gained or lost
Maximum Adverse Excursion (MAE), in percent
Maximum Favorable Excursion (MFE), in percent
net trading P&L in ticks
Maximum Adverse Excursion (MAE) in ticks
Maximum Favorable Excursion (MFE) in ticks
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.
Brian G. Peterson, Jasen Mackie, Jan Humme
Tomasini, E. and Jaekle, U. Trading Systems - A new approach to system development and portfolio optimisation (ISBN 978-1-905641-79-6)
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.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.