Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions R/tradeStats.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ tradeStats <- function( Portfolios
stringsAsFactors = FALSE)

use <- use[1] #use the first(default) value only if user hasn't specified
if (is.null(Dates)) {
Dates <- "/"
}
tradeDef <- tradeDef[1]
for (Portfolio in Portfolios){
pname <- Portfolio
Expand All @@ -158,8 +161,12 @@ tradeStats <- function( Portfolios
## Trade Statistics
for (symbol in symbols){
txn <- Portfolio$symbols[[symbol]]$txn
if (nrow(txn[Dates]) > 0) {
txn <- rbind(txn[1,], txn[Dates])
}
posPL <- Portfolio$symbols[[symbol]]$posPL
posPL <- posPL[-1,]
posPL <- posPL[Dates]

# Use gross transaction P&L to identify transactions that realized
# (non-fee) P&L, but use net transaction P&L to calculate statistics.
Expand Down Expand Up @@ -192,7 +199,7 @@ tradeStats <- function( Portfolios
#moved above for daily stats for now
},
trades = {
trades <- perTradeStats(pname,symbol,tradeDef=tradeDef, envir=envir)
trades <- perTradeStats(pname,symbol,tradeDef=tradeDef, envir=envir, Dates=Dates)
PL.gt0 <- trades$Net.Trading.PL[trades$Net.Trading.PL > 0]
PL.lt0 <- trades$Net.Trading.PL[trades$Net.Trading.PL < 0]
PL.ne0 <- trades$Net.Trading.PL[trades$Net.Trading.PL != 0]
Expand Down Expand Up @@ -317,8 +324,12 @@ tradeStats <- function( Portfolios
#' @return a multi-column \code{xts} time series, one column per symbol, one row per day
#' @seealso tradeStats
#' @export
dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, envir=.blotter, ...)
dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE,
envir=.blotter, Dates=NULL)
{
if (is.null(Dates)) {
Dates <- "/"
}
ret <- NULL
for (Portfolio in Portfolios){
pname <- Portfolio
Expand All @@ -333,6 +344,7 @@ dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, en
for (symbol in symbols){
txn <- Portfolio$symbols[[symbol]]$txn
txn <- txn[-1,] # remove initialization row
txn <- txn[Dates]

PL.ne0 <- txn$Net.Txn.Realized.PL[txn$Net.Txn.Realized.PL != 0]
if(!nrow(PL.ne0)){
Expand All @@ -354,8 +366,12 @@ dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, en
#' @param native if TRUE, return statistics in the native currency of the instrument, otherwise use the Portfolio currency, default TRUE
#' @rdname dailyTxnPL
#' @export
dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, envir=.blotter, native=TRUE, ...)
dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE,
envir=.blotter, native=TRUE, Dates=NULL, ...)
{
if (is.null(Dates)) {
Dates <- "/"
}
ret <- NULL
for (Portfolio in Portfolios){
pname <- Portfolio
Expand All @@ -374,6 +390,7 @@ dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, env
posPL <- Portfolio$symbols[[symbol]][[currPosPL]]
}
posPL <- posPL[-1,] # remove initialization row
posPL <- posPL[Dates]

Equity <- cumsum(posPL$Net.Trading.PL)
if(!nrow(Equity)){
Expand All @@ -399,15 +416,19 @@ dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, env
#' @param native if TRUE, return statistics in the native currency of the instrument, otherwise use the Portfolio currency, default TRUE
#' @param \dots any other passthrough params (e.g. \code{method} for skewness/kurtosis)
#' @export
dailyStats <- function(Portfolios,use=c('equity','txns'),perSymbol=TRUE,..., envir=.blotter, native=TRUE)
dailyStats <- function(Portfolios,use=c('equity','txns'),perSymbol=TRUE,...,
envir=.blotter, native=TRUE, Dates=NULL)
{
use=use[1] #take the first value if the user didn't specify
if (is.null(Dates)) {
Dates <- "/"
}
switch (use,
Eq =, eq =, Equity =, equity =, cumPL = {
dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native)
dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native, Dates=Dates)
},
Txns =, txns =, Trades =, trades = {
dailyPL <- dailyTxnPL(Portfolios, ..., envir=envir)
dailyPL <- dailyTxnPL(Portfolios, ..., envir=envir, Dates=Dates)
}
)

Expand Down