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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Aug 14 16:40:48 CEST 2013


Author: braverock
Date: 2013-08-14 16:40:48 +0200 (Wed, 14 Aug 2013)
New Revision: 1497

Modified:
   pkg/blotter/R/chart.Posn.R
Log:
- stop if there's nothing to chart

Modified: pkg/blotter/R/chart.Posn.R
===================================================================
--- pkg/blotter/R/chart.Posn.R	2013-08-14 12:20:21 UTC (rev 1496)
+++ pkg/blotter/R/chart.Posn.R	2013-08-14 14:40:48 UTC (rev 1497)
@@ -31,33 +31,36 @@
             daily = { mult=86400 },
             {mult=86400}
     )
-    if(!isTRUE(freq$frequency*mult == round(freq$frequency,0)*mult)) { 
+    if(!isTRUE(freq$frequency*mult == round(freq$frequency,0)*mult)) {
         # if the equality
         n=round((freq$frequency/mult),0)*mult
     } else { n=mult }
-    
+
     tzero = xts(0,order.by=index(Prices[1,]))
     if(is.null(Dates)) Dates<-paste(first(index(Prices)),last(index(Prices)),sep='::')
-    
+
     #scope the data by Dates
     Portfolio$symbols[[Symbol]]$txn<-Portfolio$symbols[[Symbol]]$txn[Dates]
     Portfolio$symbols[[Symbol]]$posPL<-Portfolio$symbols[[Symbol]]$posPL[Dates]
-    
-	Trades = Portfolio$symbols[[Symbol]]$txn$Txn.Qty
-	
-	Buys = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades>0)]
+
+    Trades = Portfolio$symbols[[Symbol]]$txn$Txn.Qty
+
+    Buys = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades>0)]
     Sells = Portfolio$symbols[[Symbol]]$txn$Txn.Price[which(Trades<0)]
 
     Position = Portfolio$symbols[[Symbol]]$txn$Pos.Qty
+
+    if(!nrow(Position)>1) stop ('no transactions/positions to chart')
+
     if(as.POSIXct(first(index(Prices)))<as.POSIXct(first(index(Position)))) Position<-rbind(xts(0,order.by=first(index(Prices)-1)),Position)
     Positionfill = na.locf(merge(Position,index(Prices)))
-    
+
     CumPL = cumsum(Portfolio$symbols[[Symbol]]$posPL$Net.Trading.PL)
     if(length(CumPL)>1)
         CumPL = na.omit(na.locf(merge(CumPL,index(Prices))))
-    else 
+    else
         CumPL = NULL
-    
+
     if(!is.null(CumPL)) {
         CumMax <- cummax(CumPL)
         Drawdown <- -(CumMax - CumPL)
@@ -68,20 +71,20 @@
     #     # These aren't quite right, as abs(Pos.Qty) should be less than prior abs(Pos.Qty)
     # 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)
     # addTA(SellCover,pch=25,type="p",col="red", bg="orange", on=1)
 
     # scope the Price data by Dates
     if(!is.null(Dates)) Prices=Prices[Dates]
-    
+
     chart_Series(Prices, name=Symbol, TA=TA, ...)
     if(!is.null(nrow(Buys)) && nrow(Buys) >=1 ) (add_TA(Buys,pch=2,type='p',col='green', on=1));
     if(!is.null(nrow(Sells)) && nrow(Sells) >= 1) (add_TA(Sells,pch=6,type='p',col='red', on=1));
     if(nrow(Position)>=1) {
-        (add_TA(Positionfill,type='h',col='blue', lwd=2))   
-        (add_TA(Position,type='p',col='orange', lwd=2, on=2))   
+        (add_TA(Positionfill,type='h',col='blue', lwd=2))
+        (add_TA(Position,type='p',col='orange', lwd=2, on=2))
     }
     if(!is.null(CumPL))  (add_TA(CumPL, col='darkgreen', lwd=2))
     if(!is.null(Drawdown)) (add_TA(Drawdown, col='darkred', lwd=2, yaxis=c(0,-max(CumMax))))
@@ -90,7 +93,7 @@
 
 ###############################################################################
 # Blotter: Tools for transaction-oriented trading systems development
-# for R (see http://r-project.org/) 
+# for R (see http://r-project.org/)
 # Copyright (c) 2008-2011 Peter Carl and Brian G. Peterson
 #
 # This library is distributed under the terms of the GNU Public License (GPL)



More information about the Blotter-commits mailing list