Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tradeStats() doesn't honor Dates #113

Open
joshuaulrich opened this issue Sep 9, 2021 · 0 comments
Open

tradeStats() doesn't honor Dates #113

joshuaulrich opened this issue Sep 9, 2021 · 0 comments

Comments

@joshuaulrich
Copy link
Collaborator

Passing a Dates value to tradeStats() doesn't affect the output; the entire period is always returned.

Reproducible example:

library(blotter)
demo("longtrend")
tradeStats("longtrend")
tradeStats("longtrend", Dates = "2018")

Here's a patch that seems to work. But I didn't thoroughly check the output.

diff --git a/R/tradeStats.R b/R/tradeStats.R
index 9456387..0370fa2 100644
--- a/R/tradeStats.R
+++ b/R/tradeStats.R
@@ -148,6 +148,9 @@ tradeStats <- function( Portfolios
     
     use <- use[1] #use the first(default) value only if user hasn't specified
     tradeDef <- tradeDef[1]
+    if (is.null(Dates)) {
+        Dates <- "/"
+    }
     for (Portfolio in Portfolios){
         pname <- Portfolio
         Portfolio<-.getPortfolio(pname, envir=envir)
@@ -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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant