From 637d5c217b10b5e71d52d7aa21ba748e2b227666 Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Sun, 13 Feb 2022 01:17:58 +0300 Subject: [PATCH 1/6] Add Dates parameter to dailyStats function --- R/tradeStats.R | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index 94563870..8c57a1a4 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -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 @@ -157,8 +160,8 @@ tradeStats <- function( Portfolios ## Trade Statistics for (symbol in symbols){ - txn <- Portfolio$symbols[[symbol]]$txn - posPL <- Portfolio$symbols[[symbol]]$posPL + txn <- Portfolio$symbols[[symbol]]$txn[Dates] + posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] posPL <- posPL[-1,] # Use gross transaction P&L to identify transactions that realized @@ -192,7 +195,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] @@ -317,7 +320,8 @@ 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) { ret <- NULL for (Portfolio in Portfolios){ @@ -354,7 +358,8 @@ 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, ...) { ret <- NULL for (Portfolio in Portfolios){ @@ -368,10 +373,10 @@ dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, env ## Trade Statistics for (symbol in symbols){ if (isTRUE(native)){ - posPL <- Portfolio$symbols[[symbol]]$posPL + posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] } else { currPosPL <- paste0('posPL.',attributes(Portfolio)$currency) - posPL <- Portfolio$symbols[[symbol]][[currPosPL]] + posPL <- Portfolio$symbols[[symbol]][[currPosPL]][Dates] } posPL <- posPL[-1,] # remove initialization row @@ -399,12 +404,13 @@ 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 switch (use, Eq =, eq =, Equity =, equity =, cumPL = { - dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native) + dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native, Dates=NULL) }, Txns =, txns =, Trades =, trades = { dailyPL <- dailyTxnPL(Portfolios, ..., envir=envir) From 3c6934a5c8f87343e1a69f73aaa27bc5e8e42555 Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Sun, 13 Feb 2022 01:26:56 +0300 Subject: [PATCH 2/6] Add Dates parameter to dailyStats function --- R/tradeStats.R | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index 8c57a1a4..116c4bdb 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -160,7 +160,7 @@ tradeStats <- function( Portfolios ## Trade Statistics for (symbol in symbols){ - txn <- Portfolio$symbols[[symbol]]$txn[Dates] + txn <- Portfolio$symbols[[symbol]]$txn posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] posPL <- posPL[-1,] @@ -373,12 +373,13 @@ dailyEqPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, ## Trade Statistics for (symbol in symbols){ if (isTRUE(native)){ - posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] + posPL <- Portfolio$symbols[[symbol]]$posPL } else { currPosPL <- paste0('posPL.',attributes(Portfolio)$currency) - posPL <- Portfolio$symbols[[symbol]][[currPosPL]][Dates] + posPL <- Portfolio$symbols[[symbol]][[currPosPL]] } posPL <- posPL[-1,] # remove initialization row + posPL <- posPL[Dates] Equity <- cumsum(posPL$Net.Trading.PL) if(!nrow(Equity)){ @@ -408,6 +409,9 @@ 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, Dates=NULL) From 4e8ab2ff7d715d6cb124e712da8a40a3da77415b Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Sun, 13 Feb 2022 01:30:35 +0300 Subject: [PATCH 3/6] Add Dates parameter to dailyStats function --- R/tradeStats.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index 116c4bdb..ca80b453 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -414,7 +414,7 @@ dailyStats <- function(Portfolios,use=c('equity','txns'),perSymbol=TRUE,..., } switch (use, Eq =, eq =, Equity =, equity =, cumPL = { - dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native, Dates=NULL) + dailyPL <- dailyEqPL(Portfolios, ..., envir=envir, native=native, Dates=Dates) }, Txns =, txns =, Trades =, trades = { dailyPL <- dailyTxnPL(Portfolios, ..., envir=envir) From 554b80c955e950c98730d565069aae36bb160c59 Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Mon, 14 Feb 2022 07:53:13 +0300 Subject: [PATCH 4/6] Add dates param to tradeStats --- R/tradeStats.R | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index ca80b453..37668dc2 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -160,7 +160,7 @@ tradeStats <- function( Portfolios ## Trade Statistics for (symbol in symbols){ - txn <- Portfolio$symbols[[symbol]]$txn + txn <- Portfolio$symbols[[symbol]]$txn[Dates] posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] posPL <- posPL[-1,] @@ -321,8 +321,11 @@ tradeStats <- function( Portfolios #' @seealso tradeStats #' @export dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, - envir=.blotter) + envir=.blotter, Dates=NULL) { + if (is.null(Dates)) { + Dates <- "/" + } ret <- NULL for (Portfolio in Portfolios){ pname <- Portfolio @@ -337,6 +340,7 @@ dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, 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)){ @@ -361,6 +365,9 @@ dailyTxnPL <- function(Portfolios, Symbols, drop.time=TRUE, incl.total=FALSE, 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 @@ -417,7 +424,7 @@ dailyStats <- function(Portfolios,use=c('equity','txns'),perSymbol=TRUE,..., 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) } ) From 71184ed1e1713056f93e082da6325b7d5d826815 Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Mon, 14 Feb 2022 08:09:28 +0300 Subject: [PATCH 5/6] Fix txn dates --- R/tradeStats.R | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index 37668dc2..ee203d5d 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -160,9 +160,11 @@ tradeStats <- function( Portfolios ## Trade Statistics for (symbol in symbols){ - txn <- Portfolio$symbols[[symbol]]$txn[Dates] - posPL <- Portfolio$symbols[[symbol]]$posPL[Dates] + txn <- Portfolio$symbols[[symbol]]$txn + 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. From 3bdeb4be511db1926ff70b7dede7641916b45518 Mon Sep 17 00:00:00 2001 From: evgeniavolkova Date: Sun, 20 Feb 2022 23:08:14 +0300 Subject: [PATCH 6/6] Check for df length before rbind --- R/tradeStats.R | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/R/tradeStats.R b/R/tradeStats.R index ee203d5d..f300982f 100644 --- a/R/tradeStats.R +++ b/R/tradeStats.R @@ -161,7 +161,9 @@ tradeStats <- function( Portfolios ## Trade Statistics for (symbol in symbols){ txn <- Portfolio$symbols[[symbol]]$txn - txn <- rbind(txn[1,], txn[Dates]) + if (nrow(txn[Dates]) > 0) { + txn <- rbind(txn[1,], txn[Dates]) + } posPL <- Portfolio$symbols[[symbol]]$posPL posPL <- posPL[-1,] posPL <- posPL[Dates]