[Blotter-commits] r239 - pkg/blotter/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Tue Feb 9 14:08:22 CET 2010


Author: braverock
Date: 2010-02-09 14:08:22 +0100 (Tue, 09 Feb 2010)
New Revision: 239

Modified:
   pkg/blotter/R/chart.Posn.R
Log:
- update for cases where there are potentially no buys or Sells
- add simple date scoping where Dates is an xts range

Modified: pkg/blotter/R/chart.Posn.R
===================================================================
--- pkg/blotter/R/chart.Posn.R	2010-02-08 18:49:06 UTC (rev 238)
+++ pkg/blotter/R/chart.Posn.R	2010-02-09 13:08:22 UTC (rev 239)
@@ -2,10 +2,8 @@
 chart.Posn <- function(Portfolio, Symbol = NULL, Dates = NULL, ...)
 { # @author Peter Carl
     pname<-Portfolio
-    Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
-    if(inherits(Portfolio,"try-error"))
-        stop(paste("Portfolio",name," not found, use initPortf() to create a new account"))
-    
+    Portfolio<-getPortfolio(pname)
+
     # DESCRIPTION
     # Charts the transaction series of a symbol against prices
 
@@ -21,8 +19,6 @@
     # FUNCTION
 
     require(quantmod)
-    # TODO: check that Portfolio is a Portfolio object
-    # TODO: add date scoping
     Prices=get(Symbol)
     freq = periodicity(Prices)
     switch(freq$scale,
@@ -36,8 +32,10 @@
         # if the equality
         n=round((freq$frequency/mult),0)*mult
     } else { n=mult }
+    
+    tzero = xts(0,order.by=index(Prices[1,]))
     Prices=align.time(Prices,n) 
-    tzero = xts(0,order.by=index(Prices[1,]))
+    
     Trades = Portfolio[[Symbol]]$txn$Txn.Price*Portfolio[[Symbol]]$txn$Txn.Qty
     Buys = Portfolio[[Symbol]]$txn$Txn.Price[which(Trades>0)]
     Buys = align.time(rbind(Buys,tzero),n)[-1]
@@ -57,10 +55,13 @@
     # addTA(BuyCover,pch=24,type="p",col="green", bg="orange", on=1)
     # addTA(SellCover,pch=25,type="p",col="red", bg="orange", on=1)
 
+    # scope the date, this is heavy-handed, but should work
+    if(!is.null(Dates)) Prices=Prices[Dates]
+    
     chartSeries(Prices, TA=NULL,...)
-    plot(addTA(Buys,pch=2,type='p',col='green', on=1));
-    plot(addTA(Sells,pch=6,type='p',col='red', on=1));
-    plot(addTA(Position,type='h',col='blue', lwd=2));
+    if(nrow(Buys)>=1) plot(addTA(Buys,pch=2,type='p',col='green', on=1));
+    if(nrow(Sells)>=1) plot(addTA(Sells,pch=6,type='p',col='red', on=1));
+    if(nrow(Position)>=1) plot(addTA(Position,type='b',col='blue', lwd=2));
     if(!is.null(CumPL))  plot(addTA(CumPL, col='darkgreen', lwd=2))
 }
 



More information about the Blotter-commits mailing list