[Blotter-commits] r378 - in pkg: blotter/R quantstrat quantstrat/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Aug 20 20:12:00 CEST 2010


Author: braverock
Date: 2010-08-20 20:12:00 +0200 (Fri, 20 Aug 2010)
New Revision: 378

Added:
   pkg/blotter/R/initSummary.R
Modified:
   pkg/blotter/R/addPortfInstr.R
   pkg/blotter/R/addTxn.R
   pkg/blotter/R/calcPortfAttr.R
   pkg/blotter/R/calcPortfSummary.R
   pkg/blotter/R/chart.Posn.R
   pkg/blotter/R/chart.Spread.R
   pkg/blotter/R/getBySymbol.R
   pkg/blotter/R/getPos.R
   pkg/blotter/R/getRealizedPL.R
   pkg/blotter/R/getTxn.R
   pkg/blotter/R/getTxnFees.R
   pkg/blotter/R/getTxnValue.R
   pkg/blotter/R/initPortf.R
   pkg/blotter/R/updateAcct.R
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
   pkg/quantstrat/DESCRIPTION
   pkg/quantstrat/R/orders.R
   pkg/quantstrat/R/strategy.R
   pkg/quantstrat/R/traderules.R
Log:
- add summary slot to portfolio object
- move instrument data to $symbols slot
- modify all other code to access in $symbols slot

Modified: pkg/blotter/R/addPortfInstr.R
===================================================================
--- pkg/blotter/R/addPortfInstr.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/addPortfInstr.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -7,9 +7,9 @@
 	initDate <- attr(portfolio, "initDate")    
 	currency <- attr(portfolio, "currency")
     for(instrument in symbols){
-        portfolio[[instrument]]$txn = initTxn(initDate = initDate, initPosQty = 0)
-        portfolio[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = 0)
-		portfolio[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio[[instrument]]$posPL		
+        portfolio$symbols[[instrument]]$txn = initTxn(initDate = initDate, initPosQty = 0)
+        portfolio$symbols[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = 0)
+		portfolio$symbols[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio$symbols[[instrument]]$posPL		
     }
 
     assign(paste("portfolio",as.character(pname),sep='.'),portfolio,envir=.blotter)    

Modified: pkg/blotter/R/addTxn.R
===================================================================
--- pkg/blotter/R/addTxn.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/addTxn.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -32,14 +32,14 @@
 	
 	
 	#If there is no table for the symbol then create a new one
-	if (is.null(Portfolio[[Symbol]])){ 
+	if (is.null(Portfolio$symbols[[Symbol]])){ 
 		addPortfInstr(Portfolio=pname, symbols=Symbol)
 		Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
 	}
 
     # Outputs:
     # Portfolio: hands back the entire portfolio object with the additional
-    # transaction in the correct slot: Portfolio[[Symbol]]$txn
+    # transaction in the correct slot: Portfolio$symbols[[Symbol]]$txn
 
     # FUNCTION
     # Compute transaction fees if a function was supplied
@@ -63,11 +63,11 @@
     # Store the transaction and calculations
     NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, txnfees, NetTxnRealizedPL, ConMult)), order.by=as.POSIXct(TxnDate))
     #colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Gross.Txn.Realized.PL', 'Txn.Fees', 'Net.Txn.Realized.PL', 'Con.Mult')
-    Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn, NewTxn)
+    Portfolio$symbols[[Symbol]]$txn<-rbind(Portfolio$symbols[[Symbol]]$txn, NewTxn)
 
     if(verbose)
         print(paste(TxnDate, Symbol, TxnQty, "@",TxnPrice, sep=" "))
-        #print(Portfolio[[Symbol]]$txn)
+        #print(Portfolio$symbols[[Symbol]]$txn)
     
     assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }
@@ -132,7 +132,7 @@
             NewTxns<-rbind(NewTxns, NewTxn)
         }
     }
-    Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn,NewTxns) 
+    Portfolio$symbols[[Symbol]]$txn<-rbind(Portfolio$symbols[[Symbol]]$txn,NewTxns) 
 
     if(verbose) print(NewTxns)
     
@@ -155,7 +155,7 @@
     }
     # Outputs:
     # Portfolio: hands back the entire portfolio object with the additional
-    # transaction in the correct slot: Portfolio[[Symbol]]$txn
+    # transaction in the correct slot: Portfolio$symbols[[Symbol]]$txn
 
     # FUNCTION
     # Adding a Dividend does not affect position
@@ -184,11 +184,11 @@
     # Store the transaction and calculations
     NewTxn = xts(t(c(TxnQty, TxnPrice, TxnValue, TxnAvgCost, PosQty, PosAvgCost, GrossTxnRealizedPL, TxnFees, NetTxnRealizedPL, ConMult)), order.by=as.POSIXct(TxnDate))
     #colnames(NewTxns) = c('Txn.Qty', 'Txn.Price', 'Txn.Value', 'Txn.Avg.Cost', 'Pos.Qty', 'Pos.Avg.Cost', 'Gross.Txn.Realized.PL', 'Txn.Fees', 'Net.Txn.Realized.PL', 'Con.Mult')
-    Portfolio[[Symbol]]$txn<-rbind(Portfolio[[Symbol]]$txn, NewTxn)
+    Portfolio$symbols[[Symbol]]$txn<-rbind(Portfolio$symbols[[Symbol]]$txn, NewTxn)
 
     if(verbose)
         print(paste(TxnDate, Symbol, "Dividend", DivPerShare, "on", PrevPosQty, "shares:", TxnValue, sep=" "))
-        #print(Portfolio[[Symbol]]$txn)
+        #print(Portfolio$symbols[[Symbol]]$txn)
 
     assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
 }

Modified: pkg/blotter/R/calcPortfAttr.R
===================================================================
--- pkg/blotter/R/calcPortfAttr.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/calcPortfAttr.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -1,10 +1,10 @@
 calcPortfAttr <- function(Portfolio, Attribute, Dates=NULL, Symbols = NULL)
 {
     if(!inherits(Portfolio,"portfolio")) stop("Portfolio passed is not a portfolio object.")
-    symbols = names(Portfolio)
+    symbols = names(Portfolio$symbols)
     if(is.null(Dates)|is.na(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$symbols[[1]]$posPL)
+#    else Dates = time(Portfolio$symbols[[1]]$posPL[Dates])
 
     switch(Attribute,
         Gross.Trading.PL = {

Modified: pkg/blotter/R/calcPortfSummary.R
===================================================================
--- pkg/blotter/R/calcPortfSummary.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/calcPortfSummary.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -6,8 +6,8 @@
     # Long.Value, Short.Value, Net.Value, Trading.PL
 
     if(is.null(Dates) || is.na(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$symbols[[1]]$posPL )
+#    else Dates = time(Portfolio$symbols[[1]]$posPL[Dates])
     
     GrossTradingPL = calcPortfAttr(Portfolio, 'Gross.Trading.PL', Dates)
     NetTradingPL = calcPortfAttr(Portfolio, 'Net.Trading.PL', Dates)

Modified: pkg/blotter/R/chart.Posn.R
===================================================================
--- pkg/blotter/R/chart.Posn.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/chart.Posn.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -36,22 +36,22 @@
     tzero = xts(0,order.by=index(Prices[1,]))
 #    Prices=align.time(Prices,n) 
     
-    Trades = Portfolio[[Symbol]]$txn$Txn.Price*Portfolio[[Symbol]]$txn$Txn.Qty
-    Buys = Portfolio[[Symbol]]$txn$Txn.Price[which(Trades>0)]
+    Trades = Portfolio$symbols[[Symbol]]$txn$Txn.Price*Portfolio$symbols[[Symbol]]$txn$Txn.Qty
+    Buys = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades>0)]
 #    Buys = align.time(rbind(Buys,tzero),n)[-1]
-    Sells = Portfolio[[Symbol]]$txn$Txn.Price[which(Trades<0)]
+    Sells = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades<0)]
 #    Sells = align.time(rbind(Sells,tzero),n)[-1]
-    #Position = Portfolio[[Symbol]]$posPL$Pos.Qty # use $txn instead, and make it match the prices index
-    Position = Portfolio[[Symbol]]$txn$Pos.Qty
+    #Position = Portfolio$symbols[[Symbol]]$posPL$Pos.Qty # use $txn instead, and make it match the prices index
+    Position = Portfolio$symbols[[Symbol]]$txn$Pos.Qty
     Positionfill = na.locf(merge(Position,index(Prices)))
-    CumPL = cumsum(Portfolio[[Symbol]]$posPL$Net.Trading.PL)
+    CumPL = cumsum(Portfolio$symbols[[Symbol]]$posPL$Net.Trading.PL)
     if(length(CumPL)>1)
         CumPL = na.locf(merge(CumPL,index(Prices)))
     else 
         CumPL = NULL
     #     # These aren't quite right, as abs(Pos.Qty) should be less than prior abs(Pos.Qty)
-    # SellCover = Portfolio[[Symbol]]$txn$Txn.Price * (Portfolio[[Symbol]]$txn$Txn.Qty<0) * (Portfolio[[Symbol]]$txn$Pos.Qty==0)
-    # BuyCover = Portfolio[[Symbol]]$txn$Txn.Price * (Portfolio[[Symbol]]$txn$Txn.Qty>0) * (Portfolio[[Symbol]]$txn$Pos.Qty==0)
+    # SellCover = Portfolio$symbols[[Symbol]]$txn$Txn.Price * (Portfolio$symbols[[Symbol]]$txn$Txn.Qty<0) * (Portfolio$symbols[[Symbol]]$txn$Pos.Qty==0)
+    # BuyCover = Portfolio$symbols[[Symbol]]$txn$Txn.Price * (Portfolio$symbols[[Symbol]]$txn$Txn.Qty>0) * (Portfolio$symbols[[Symbol]]$txn$Pos.Qty==0)
     # 
     #     #Symbol 24 (up) and 25 (dn) can take bkgd colors
     # addTA(BuyCover,pch=24,type="p",col="green", bg="orange", on=1)

Modified: pkg/blotter/R/chart.Spread.R
===================================================================
--- pkg/blotter/R/chart.Spread.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/chart.Spread.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -44,18 +44,18 @@
     } else { n=mult }
     tzero = xts(0,order.by=index(Prices[1,]))
     Prices=align.time(Prices,n) 
-    Trades = Portfolio[[Symbol]]$txn$Txn.Price*Portfolio[[Symbol]]$txn$Txn.Qty
-    Buys = Portfolio[[Symbol]]$txn$Txn.Price[which(Trades>0)]
+    Trades = Portfolio$symbols[[Symbol]]$txn$Txn.Price*Portfolio$symbols[[Symbol]]$txn$Txn.Qty
+    Buys = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades>0)]
     Buys = align.time(rbind(Buys,tzero),n)[-1]
     #because this is a spread, we need to use the price of the spread at the time of the synthetic 'buy'
     Buys = Prices[unique(Hmisc::find.matches(index(Buys),index(Prices))[[1]])]
-    Sells = Portfolio[[Symbol]]$txn$Txn.Price[which(Trades<0)]
+    Sells = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades<0)]
     Sells = align.time(rbind(Sells,tzero),n)[-1]
     #because this is a spread, we need to use the price of the spread at the time of the synthetic 'sell'
     Sells = Prices[unique(Hmisc::find.matches(index(Sells),index(Prices))[[1]])]
     #     # These aren't quite right, as abs(Pos.Qty) should be less than prior abs(Pos.Qty)
-    # SellCover = Portfolio[[Symbol]]$txn$Txn.Price * (Portfolio[[Symbol]]$txn$Txn.Qty<0) * (Portfolio[[Symbol]]$txn$Pos.Qty==0)
-    # BuyCover = Portfolio[[Symbol]]$txn$Txn.Price * (Portfolio[[Symbol]]$txn$Txn.Qty>0) * (Portfolio[[Symbol]]$txn$Pos.Qty==0)
+    # SellCover = Portfolio$symbols[[Symbol]]$txn$Txn.Price * (Portfolio$symbols[[Symbol]]$txn$Txn.Qty<0) * (Portfolio$symbols[[Symbol]]$txn$Pos.Qty==0)
+    # BuyCover = Portfolio$symbols[[Symbol]]$txn$Txn.Price * (Portfolio$symbols[[Symbol]]$txn$Txn.Qty>0) * (Portfolio$symbols[[Symbol]]$txn$Pos.Qty==0)
     # 
     #     #Symbol 24 (up) and 25 (dn) can take bkgd colors
     # addTA(BuyCover,pch=24,type="p",col="green", bg="orange", on=1)
@@ -65,10 +65,10 @@
     plot(addTA(Buys,pch=2,type='p',col='green', on=1));
     plot(addTA(Sells,pch=6,type='p',col='red', on=1));
 
-    #Position = Portfolio[[Symbol]]$posPL$Pos.Qty # use $txn instead, and make it match the prices index
+    #Position = Portfolio$symbols[[Symbol]]$posPL$Pos.Qty # use $txn instead, and make it match the prices index
     i=1
     for(Symbol in tmp_instr$memberlist$members){
-        Position = Portfolio[[Symbol]]$txn$Pos.Qty
+        Position = Portfolio$symbols[[Symbol]]$txn$Pos.Qty
         Position = na.locf(merge(Position,index(Prices)))
         plot(addTA(Position,type='b',col=i, lwd=1));
         i=i+1

Modified: pkg/blotter/R/getBySymbol.R
===================================================================
--- pkg/blotter/R/getBySymbol.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getBySymbol.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -17,8 +17,8 @@
 
     # FUNCTION
     if(is.null(Dates) | is.na(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$symbols[[1]]$posPL)
+    # else  Dates = time(Portfolio$symbols[[1]]$posPL[Dates])
     if(!is.null(attr(Portfolio,'currency')) & Local==FALSE) {
         p.ccy.str<-attr(Portfolio,'currency')
         namePosPL = paste("posPL", p.ccy.str, sep=".")
@@ -31,12 +31,12 @@
     table = NULL 
       ## Need a reference time index
     if(is.null(Symbols))
-        symbols=names(Portfolio)
+        symbols=names(Portfolio$symbols)
     else
         symbols = Symbols
     
     for (symbol in symbols) {
-        tmp_col = Portfolio[[symbol]][[namePosPL]][Dates,Attribute,drop=FALSE]
+        tmp_col = Portfolio$symbols[[symbol]][[namePosPL]][Dates,Attribute,drop=FALSE]
         if(is.null(table)) table = tmp_col
         else table = merge(table, tmp_col)
     }

Modified: pkg/blotter/R/getPos.R
===================================================================
--- pkg/blotter/R/getPos.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getPos.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -14,7 +14,7 @@
     # This should get much more complicated from here, particularly when it's conditional on symbol, etc.
     
     # FUNCTION
-    PosData = Portfolio[[Symbol]]$txn
+    PosData = Portfolio$symbols[[Symbol]]$txn
     toDate = paste('::', Date, sep="")
     # It may not make sense to return realized P&L with the position information, so only position and 
     # position average cost are returned.

Modified: pkg/blotter/R/getRealizedPL.R
===================================================================
--- pkg/blotter/R/getRealizedPL.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getRealizedPL.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -19,7 +19,7 @@
     # Realized PL calculated for the transaction
 
     # FUNCTION
-    PosData = Portfolio[[Symbol]]$txn
+    PosData = Portfolio$symbols[[Symbol]]$txn
     RealizedPL = sum(as.numeric(PosData[Date, 'Realized.PL', drop=FALSE]))
     return(RealizedPL)
 }

Modified: pkg/blotter/R/getTxn.R
===================================================================
--- pkg/blotter/R/getTxn.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getTxn.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -15,7 +15,7 @@
 
     # Outputs
     # Table of transactions made in the Symbol during the time period given
-    TxnData = Portfolio[[Symbol]]$txn
+    TxnData = Portfolio$symbols[[Symbol]]$txn
     Txns = TxnData[Date,c('Txn.Qty', 'Txn.Price', 'Txn.Fees', 'Txn.Value', 'Txn.Avg.Cost')]
     return(Txns)
 }

Modified: pkg/blotter/R/getTxnFees.R
===================================================================
--- pkg/blotter/R/getTxnFees.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getTxnFees.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -6,7 +6,7 @@
     if(inherits(Portfolio,"try-error"))
         stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
     
-    TxnData = Portfolio[[Symbol]]$txn
+    TxnData = Portfolio$symbols[[Symbol]]$txn
     TxnFees = sum(TxnData[Date,'Txn.Fees'])
     return(TxnFees)
 }

Modified: pkg/blotter/R/getTxnValue.R
===================================================================
--- pkg/blotter/R/getTxnValue.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/getTxnValue.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -18,7 +18,7 @@
     # Numeric value of the most recent position.
 
     # FUNCTION
-    TxnData = Portfolio[[Symbol]]$txn
+    TxnData = Portfolio$symbols[[Symbol]]$txn
     TxnValue = sum(TxnData[Date,'Txn.Value'])
     return(TxnValue)
 }

Modified: pkg/blotter/R/initPortf.R
===================================================================
--- pkg/blotter/R/initPortf.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/initPortf.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -2,7 +2,7 @@
 #' 
 #' Initializes a portfolio object, which is constructed from is constructed of the following:
 #' symbols: the identifier used for each instrument contained in the portfolio.
-#'   Use names(Portfolio) to get a list of symbols.
+#'   Use names(Portfolio$symbols) to get a list of symbols.
 #' $[symbol]$txn: irregular xts object of transactions data 
 #' $[symbol]$posPL: regular xts object of positions P&L calculated from 
 #'   transactions
@@ -24,18 +24,20 @@
         stop(paste("Portfolio",name,"already exists, use updatePortf() or addPortfInstr() to update it."))
     
     # FUNCTION
-    portfolio=vector("list",length=length(symbols))
-    names(portfolio)=symbols
+    portfolio=list()
+	portfolio$symbols=vector("list",length=length(symbols))
+    names(portfolio$symbols)=symbols
     if(length(initPosQty)==1)
 	initPosQty=rep(initPosQty, length(symbols))
     if(length(initPosQty)!=length(symbols))
 	stop("The length of initPosQty is unequal to the number of symbols in the portfolio.")
     for(instrument in symbols){
     	i = match(instrument, symbols)
-        portfolio[[instrument]]$txn = initTxn(initDate = initDate, initPosQty = initPosQty[i])
-        portfolio[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = initPosQty[i])
-        portfolio[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio[[instrument]]$posPL
+        portfolio$symbols[[instrument]]$txn = initTxn(initDate = initDate, initPosQty = initPosQty[i])
+        portfolio$symbols[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = initPosQty[i])
+        portfolio$symbols[[instrument]][[paste('posPL',currency,sep='.')]] = portfolio$symbols[[instrument]]$posPL
     }
+	portfolio$summary<-initSummary(initDate=initDate)
     class(portfolio)<-c("blotter_portfolio", "portfolio")
     attr(portfolio,'currency')<-currency
 	attr(portfolio,'initDate')<-initDate

Added: pkg/blotter/R/initSummary.R
===================================================================
--- pkg/blotter/R/initSummary.R	                        (rev 0)
+++ pkg/blotter/R/initSummary.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -0,0 +1,19 @@
+initSummary <- function(initDate="1950-01-01")
+{ # @author Brian Peterson
+    summary <- xts( as.matrix(t(rep(0,9))), order.by=as.POSIXct(initDate) )
+    colnames(summary) <- c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Realized.PL', 'Unrealized.PL', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
+    class(summary)<-c("portfolio_summary",class(summary))
+    return(summary)
+}
+
+###############################################################################
+# Blotter: Tools for transaction-oriented trading systems development
+# for R (see http://r-project.org/) 
+# Copyright (c) 2008-2010 Peter Carl and Brian G. Peterson
+#
+# This library is distributed under the terms of the GNU Public License (GPL)
+# for full details see the file COPYING
+#
+# $Id: initTxn.R 306 2010-03-26 21:19:51Z peter_carl $
+#
+###############################################################################

Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/updateAcct.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -22,10 +22,10 @@
     # TODO FIXME this so that it finds the date range in *any*/all portfolios, not just the first
     if(is.null(Dates)) 
         #[[1]] here is the first instrument in the portfolio
-        Dates = time(Portfolio[[1]]$posPL ) # if no date is specified, get all available dates
+        Dates = time(Portfolio$symbols[[1]]$posPL ) # if no date is specified, get all available dates
 #    if(!is.timeBased(Dates) ){
         # Dates is an xts range, turn it into a list of Dates
-#    else Dates = time(Portfolio[[1]]$posPL[Dates])
+#    else Dates = time(Portfolio$symbols[[1]]$posPL[Dates])
 #    } 
     
     #TODO FIXME do two loops, one over portfolios to update each portfolio 
@@ -42,7 +42,7 @@
             Portfolio = getPortfolio(pname)
 
             CurrentDate = Dates[d]
-            PrevDate = time(Portfolio[[1]]$posPL[Portfolio[[1]]$posPL[CurrentDate,which.i=TRUE]-1 ] ) # which.i is new in [.xts
+            PrevDate = time(Portfolio$symbols[[1]]$posPL[Portfolio$symbols[[1]]$posPL[CurrentDate,which.i=TRUE]-1 ] ) # which.i is new in [.xts
             if (length(PrevDate)==0) next() #no price data, keep looking
             PrevDateWidth = xts:::.parseISO8601(PrevDate)
             PrevDateLast = PrevDateWidth$last.time

Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/updatePortf.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -21,19 +21,22 @@
 
     # FUNCTION
     if(is.null(Symbols)){
-        Symbols = names(Portfolio)
+        Symbols = names(Portfolio$symbols)
     } 
     for(symbol in Symbols){
         tmp_instr<-try(getInstrument(symbol))
         updatePosPL(Portfolio=pname, Symbol=as.character(symbol), Dates=Dates, Prices=Prices)            
     }
+	
     # Calculate and store portfolio summary table
     Portfolio<-getPortfolio(pname) # refresh with an updated object
-    Symbols = names(Portfolio)
+    #Symbols = names(Portfolio$symbols)
     Attributes = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Realized.PL', 'Unrealized.PL', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
-    summary = matrix()
+    summary = NULL
     for(attribute in Attributes) {
+		result=NULL
         table = .getBySymbol(Portfolio = Portfolio, Attribute = attribute, Dates = Dates, Symbols = Symbols)
+		
         switch(attribute,
             Gross.Value = {
                 result = xts(rowSums(abs(table), na.rm=TRUE), order.by=index(table))
@@ -49,10 +52,19 @@
             { result = xts(rowSums(table, na.rm=TRUE), order.by=index(table))
             }
         )
+		
         colnames(result) = attribute
-        summary = merge(summary, result)
+		if(is.null(summary)) {summary=result}
+		else {summary=cbind(summary,result)}
     }
-    # TODO: Now store summary in the correct slot in the Portfolio object
+	
+	if(!is.timeBased(Dates)) Dates = time(Portfolio$summary[Dates])
+	startDate = xts:::.parseISO8601(first(Dates))$first.time-1 #does this need to be a smaller delta for millisecond data?
+	# trim summary slot to not double count, related to bug 831 on R-Forge, and rbind new summary 
+	Portfolio$summary<-rbind(Portfolio$summary[paste('::',startDate,sep='')],summary)
+	# assign Portfolio to environment
+	assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
+	
     return(pname) #not sure this is a good idea
 }
 

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/blotter/R/updatePosPL.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -39,10 +39,16 @@
 	
 	#	***** Vectorization *****#
 	# trim posPL slot to not double count, related to bug 831 on R-Forge 
-	Portfolio[[Symbol]]$posPL<-Portfolio[[Symbol]]$posPL[paste('::',startDate,sep='')]
-	Portfolio[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]]<-Portfolio[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]][paste('::',startDate,sep='')]
+	Portfolio$symbols[[Symbol]]$posPL<-Portfolio$symbols[[Symbol]]$posPL[paste('::',startDate,sep='')]
+	Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]]<-Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]][paste('::',startDate,sep='')]
 	
-	Txns <- Portfolio[[Symbol]]$txn[dateRange]
+	Txns <- Portfolio$symbols[[Symbol]]$txn[dateRange]
+	if(nrow(Txns)==0) {
+		message("No Transactions to process for ",Symbol," in ",dateRange)
+		return()
+		#TODO this isn't quite right either, but it will suffice to avoid errors for what we need for the moment...
+		#eventually, we'll need to get the last row in the posPL table, and mark from there if we have a position
+	}
 	#	 line up transaction with Dates list
 	tmpPL <- merge(Txns, Prices) # most Txn columns will get discarded later
 	# na.locf any missing prices with last observation (this assumption seems the only rational one for vectorization)
@@ -83,12 +89,12 @@
 	tmpPL <- tmpPL[,c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value',  'Realized.PL', 'Unrealized.PL','Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')]
 
 	# rbind to $posPL slot
-	Portfolio[[Symbol]]$posPL<-rbind(Portfolio[[Symbol]]$posPL,tmpPL)
+	Portfolio$symbols[[Symbol]]$posPL<-rbind(Portfolio$symbols[[Symbol]]$posPL,tmpPL)
 		
 
     
     # now do the currency conversions for the whole date range
-    TmpPeriods<-Portfolio[[Symbol]]$posPL[dateRange]
+    TmpPeriods<-Portfolio$symbols[[Symbol]]$posPL[dateRange]
 
 	CcyMult = NA 
 	FXrate = NA
@@ -144,7 +150,7 @@
     }
     TmpPeriods[,'Ccy.Mult']<-CcyMult
     #stick it in posPL.ccy
-    Portfolio[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]]<-rbind(Portfolio[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]],TmpPeriods)
+    Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]]<-rbind(Portfolio$symbols[[Symbol]][[paste('posPL',p.ccy.str,sep='.')]],TmpPeriods)
     # assign Portfolio to environment
     assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
 }

Modified: pkg/quantstrat/DESCRIPTION
===================================================================
--- pkg/quantstrat/DESCRIPTION	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/quantstrat/DESCRIPTION	2010-08-20 18:12:00 UTC (rev 378)
@@ -1,11 +1,11 @@
 Package: quantstrat
 Type: Package
 Title: Quantitative Strategy Model Framework
-Version: 0.2
-Date: 2010-02-06
+Version: 0.3
+Date: $Date$
 Author: Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey A.
     Ryan, Joshua Ulrich
-Depends: xts(>= 0.7-0),TTR(>= 0.2),blotter(>= 0.4),
+Depends: xts(>= 0.7-0),TTR(>= 0.2),blotter(>= 0.7),
     FinancialInstrument, quantmod (>= 0.3-14)
 Suggests: PerformanceAnalytics,PortfolioAnalytics
 Maintainer: Jeffrey A. Ryan <jeff.a.ryan at gmail.com>

Modified: pkg/quantstrat/R/orders.R
===================================================================
--- pkg/quantstrat/R/orders.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/quantstrat/R/orders.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -40,7 +40,7 @@
     
     if(is.null(symbols)) {
         pfolio<-getPortfolio(portfolio)
-        symbols<-names(pfolio)
+        symbols<-names(pfolio$symbols)
     }
     if(!is.null(symbols)){
         for (symbol in symbols){

Modified: pkg/quantstrat/R/strategy.R
===================================================================
--- pkg/quantstrat/R/strategy.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/quantstrat/R/strategy.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -90,7 +90,7 @@
 		
 		ret[[portfolio]]<-list() # this is slot [[i]] which we will use later
         pobj<-getPortfolio(portfolio)
-        symbols<-names(pobj)
+        symbols<-names(pobj$symbols)
         sret<-list()
         for (symbol in symbols){
             ret[[portfolio]][[symbol]]<-list()

Modified: pkg/quantstrat/R/traderules.R
===================================================================
--- pkg/quantstrat/R/traderules.R	2010-08-20 14:22:48 UTC (rev 377)
+++ pkg/quantstrat/R/traderules.R	2010-08-20 18:12:00 UTC (rev 378)
@@ -133,16 +133,16 @@
 addPosLimit <- function(portfolio, symbol, timestamp, maxpos, longlevels=1, minpos=0, shortlevels=0){
     portf<-getPortfolio(portfolio)
     newrow <- xts(c(maxpos, longlevels, minpos, shortlevels),order.by=as.POSIXct(timestamp))
-    if(is.null(portf[[symbol]]$PosLimit)) {
-        portf[[symbol]]$PosLimit <- newrow 
-        colnames(portf[[symbol]]$PosLimit)<-c("MaxPos","LongLevels","MinPos","ShortLevels")
+    if(is.null(portf$symbols[[symbol]]$PosLimit)) {
+        portf$symbols[[symbol]]$PosLimit <- newrow 
+        colnames(portf$symbols[[symbol]]$PosLimit)<-c("MaxPos","LongLevels","MinPos","ShortLevels")
     } else {
         if(!is.null(portf[[symbol]]$PosLimit[timestamp])){
             # it exists already, so replace
-            portf[[symbol]]$PosLimit[timestamp]<-newrow
+            portf$symbols[[symbol]]$PosLimit[timestamp]<-newrow
         } else {
             # add a new row on timestamp
-            portf[[symbol]]$PosLimit <- rbind(portf[[symbol]]$PosLimit,newrow)
+            portf$symbols[[symbol]]$PosLimit <- rbind(portf[[symbol]]$PosLimit,newrow)
         }
     }
     assign(paste("portfolio",portfolio,sep='.'),portf,envir=.blotter)
@@ -157,7 +157,7 @@
     portf<-getPortfolio(portfolio)
     # try to get on timestamp, otherwise find the most recent
     toDate = paste('::', as.character(timestamp), sep="")
-    PosLimit = last(portf[[symbol]]$PosLimit[toDate])
+    PosLimit = last(portf$symbols[[symbol]]$PosLimit[toDate])
     return(PosLimit)
 }
 



More information about the Blotter-commits mailing list