[Blotter-commits] r58 - pkg/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Mar 3 03:59:04 CET 2009


Author: peter_carl
Date: 2009-03-03 03:59:04 +0100 (Tue, 03 Mar 2009)
New Revision: 58

Modified:
   pkg/R/addTxn.R
   pkg/R/initAcct.R
   pkg/R/initPosPL.R
   pkg/R/initTxn.R
   pkg/R/updateAcct.R
   pkg/R/updatePosPL.R
Log:
- changed as.Date to as.POSIXct to support finer timescale trading


Modified: pkg/R/addTxn.R
===================================================================
--- pkg/R/addTxn.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/addTxn.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -37,7 +37,7 @@
     RealizedPL = calcRealizedPL(TxnQty, TxnAvgCost, PrevPosAvgCost, PosQty, PrevPosQty)
 
     # Store the transaction and calculations
-    NewTxn = xts(t(c(TxnQty, TxnPrice, TxnFees, TxnValue, TxnAvgCost, PosQty, PosAvgCost, RealizedPL)), order.by=as.Date(TxnDate))
+    NewTxn = xts(t(c(TxnQty, TxnPrice, TxnFees, TxnValue, TxnAvgCost, PosQty, PosAvgCost, RealizedPL)), order.by=as.POSIXct(TxnDate))
     colnames(NewTxn) = c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Realized.PL')
     Portfolio[[Symbol]]$txn <- rbind(Portfolio[[Symbol]]$txn, NewTxn) 
 

Modified: pkg/R/initAcct.R
===================================================================
--- pkg/R/initAcct.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/initAcct.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -48,7 +48,7 @@
     account[["TOTAL"]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0,0,initEq))), order.by=as.Date(initDate) )
     colnames(account[["TOTAL"]]) = c('Additions', 'Withdrawals', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
     for(portfolio in portfolios){
-        account[[portfolio]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0))), order.by=as.Date(initDate) )
+        account[[portfolio]] = xts( as.matrix(t(c(0,0,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
         colnames(account[[portfolio]]) = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Txn.Fees','Realized.PL', 'Unrealized.PL', 'Trading.PL')
     }
     return(account)

Modified: pkg/R/initPosPL.R
===================================================================
--- pkg/R/initPosPL.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/initPosPL.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -15,7 +15,7 @@
     # Constructs multi-column xts object used to store derived position information
 
     # FUNCTION
-    posPL <- xts( as.matrix(t(c(initPosQty,0,0,0,0,0,0))), order.by=as.Date(initDate) )
+    posPL <- xts( as.matrix(t(c(initPosQty,0,0,0,0,0,0))), order.by=as.POSIXct(initDate) )
     colnames(posPL) <- c('Pos.Qty', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL','Trading.PL')
     return(posPL)
 }

Modified: pkg/R/initTxn.R
===================================================================
--- pkg/R/initTxn.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/initTxn.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -16,8 +16,8 @@
 
     # FUNCTION
     ## @todo: Add 'Txn.Type' column
-    ## @todo: DIVIDEND creates a realized gain
-    txn <- xts( as.matrix(t(c(0,0,0,0,0,initPosQty,0,0))), order.by=as.Date(initDate) )
+    ## @todo: DIVIDEND Txn.Type creates a realized gain
+    txn <- xts( as.matrix(t(c(0,0,0,0,0,initPosQty,0,0))), order.by=as.POSIXct(initDate) )
     colnames(txn) <- c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Realized.PL')
     return(txn)
 }

Modified: pkg/R/updateAcct.R
===================================================================
--- pkg/R/updateAcct.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/updateAcct.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -24,8 +24,7 @@
     if(is.null(Dates)) # if no date is specified, get all available dates
         Dates = time(Portfolio[[1]]$posPL)
     else
-        Dates = time(Portfolio[[1]]$posPL[Dates])
-
+        Dates = time(Portfolio[[1]]$posPL[Dates,])
     # For each date, calculate realized and unrealized P&L
     for(d in 1:length(Dates)){ # d is a date slot counter
     # I shouldn't have to do this but I lose the class for the element when I do
@@ -33,16 +32,16 @@
         # Append the portfolio summary data to the portfolio slot
         for(i in 1:length(Portfolios)){
             Portfolio = get(Portfolios[i])
-            row = xts(calcPortfSummary(Portfolio, Dates[d]), order.by=as.Date(Dates[d])) ## comes back as zoo?
+            row = calcPortfSummary(Portfolio, Dates[d])
             Account[[i+1]] = rbind(Account[[i+1]],row)
         }
 
         # Now aggregate the portfolio information into the TOTAL slot
-        TxnFees = as.numeric(calcAcctAttr(Account, 'Txn.Fees', Dates[d]))
+        TxnFees = as.numeric(calcAcctAttr(Account = Account, Attribute = 'Txn.Fees', Date = Dates[d]))
         RealizedPL = as.numeric(calcAcctAttr(Account, 'Realized.PL', Dates[d]))
         UnrealizedPL = as.numeric(calcAcctAttr(Account, 'Unrealized.PL', Dates[d]))
         TradingPL = as.numeric(calcAcctAttr(Account, 'Trading.PL', Dates[d]))
-        row = xts(t(c(0, 0, TxnFees, RealizedPL, UnrealizedPL, 0, TradingPL, 0, 0, 0)), order.by=as.Date(Dates[d]))
+        row = xts(t(c(0, 0, TxnFees, RealizedPL, UnrealizedPL, 0, TradingPL, 0, 0, 0)), order.by=Dates[d])
         colnames(row) = c('Additions', 'Withdrawals', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Int.Income', 'Trading.PL', 'Advisory.Fees', 'Net.Performance', 'End.Eq')
         Account[['TOTAL']] <- rbind(Account[['TOTAL']], row)
     # This function does not calculate End.Eq 

Modified: pkg/R/updatePosPL.R
===================================================================
--- pkg/R/updatePosPL.R	2009-03-03 02:57:04 UTC (rev 57)
+++ pkg/R/updatePosPL.R	2009-03-03 02:59:04 UTC (rev 58)
@@ -55,7 +55,7 @@
         RealizedPL = getRealizedPL(Portfolio, Symbol, CurrentDate)
         UnrealizedPL = TradingPL - RealizedPL #
 
-        NewPeriod = as.xts(t(c(PosQty, PosValue, TxnValue, TxnFees, RealizedPL, UnrealizedPL, TradingPL)), order.by=as.Date(CurrentDate))
+        NewPeriod = as.xts(t(c(PosQty, PosValue, TxnValue, TxnFees, RealizedPL, UnrealizedPL, TradingPL)), order.by=as.POSIXct(CurrentDate))
         colnames(NewPeriod) = c('Pos.Qty', 'Pos.Value', 'Txn.Value', 'Txn.Fees', 'Realized.PL', 'Unrealized.PL', 'Trading.PL')
         Portfolio[[Symbol]]$posPL <- rbind(Portfolio[[Symbol]]$posPL, NewPeriod) 
     }



More information about the Blotter-commits mailing list