[Blotter-commits] r203 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Jan 26 18:30:20 CET 2010
Author: braverock
Date: 2010-01-26 18:30:20 +0100 (Tue, 26 Jan 2010)
New Revision: 203
Added:
pkg/blotter/R/addPortfInstr.R
Modified:
pkg/blotter/R/initPortf.R
pkg/blotter/R/updatePortf.R
Log:
- remove special handling for synthetics, include addPortfInstr
Added: pkg/blotter/R/addPortfInstr.R
===================================================================
--- pkg/blotter/R/addPortfInstr.R (rev 0)
+++ pkg/blotter/R/addPortfInstr.R 2010-01-26 17:30:20 UTC (rev 203)
@@ -0,0 +1,34 @@
+addPortfInstr <- function(Portfolio,symbols,...,verbose=TRUE)
+{
+ pname<-Portfolio
+ portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter)
+ if(inherits(Portfolio,"try-error"))
+ stop(paste("Portfolio",pname," not found, use initPortf() to create a new portfolio"))
+
+ msymbols<-match(names(portfolio),symbols)
+ symbols<-symbols[which(is.na(msymbols))]
+
+ if(missing(initPosQty)) initPosQty=rep(0, length(symbols))
+
+ 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])
+ }
+
+ assign(paste("portfolio",as.character(pname),sep='.'),portfolio,envir=.blotter)
+
+}
+
+###############################################################################
+# 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$
+#
+###############################################################################
+
Property changes on: pkg/blotter/R/addPortfInstr.R
___________________________________________________________________
Name: svn:keywords
+ Revision Id Date Author
Modified: pkg/blotter/R/initPortf.R
===================================================================
--- pkg/blotter/R/initPortf.R 2010-01-25 21:51:02 UTC (rev 202)
+++ pkg/blotter/R/initPortf.R 2010-01-26 17:30:20 UTC (rev 203)
@@ -21,7 +21,7 @@
initPortf <- function(name="default", symbols, initPosQty = 0, initDate = '1950-01-01')
{ # @author Peter Carl
if(exists(paste("portfolio",name,sep='.'), envir=.blotter,inherits=TRUE))
- stop(paste("Portfolio",name,"already exists, use updatePortf() to update it."))
+ stop(paste("Portfolio",name,"already exists, use updatePortf() or addPortfInstr() to update it."))
# FUNCTION
portfolio=vector("list",length=length(symbols))
Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R 2010-01-25 21:51:02 UTC (rev 202)
+++ pkg/blotter/R/updatePortf.R 2010-01-26 17:30:20 UTC (rev 203)
@@ -7,7 +7,7 @@
#' These dates must appear in the price stream
#'
#' Outputs
-#' Regular time series of position information and PL
+#' assigns position information and PL into the environment
#'
#' @param Portfolio
#' @param Dates
@@ -15,18 +15,23 @@
updatePortf <- function(Portfolio, Dates)
{ #' @author Peter Carl
pname<-Portfolio
- Portfolio<-get(paste("portfolio",pname,sep='.'),envir=.blotter,inherits=TRUE)
+ Portfolio<-try(get(paste("portfolio",pname,sep='.'),envir=.blotter),silent=TRUE)
if(inherits(Portfolio,"try-error"))
- stop(paste("Portfolio",pname," not found, use initPortf() to create a new account"))
+ stop(paste("Portfolio",pname," not found, use initPortf() to create a new portfolio"))
# FUNCTION
symbols = names(Portfolio)
for(symbol in symbols){
- Portfolio = updatePosPL(pname, symbol, Dates, Cl(get(symbol)))
- }
- assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
- return(pname) #not sure this is a good idea
+ tmp_instr<-try(getInstrument(symbol))
+ if(inherits(tmp_instr,"try-error") | !is.instrument(tmp_instr)){
+ message(paste("Instrument",symbol," not found, assuming non-synthetic"))
+ } else {
+ updatePosPL(pname, symbol, Dates, Cl(get(symbol)))
+ }
+ }
+ assign(paste("portfolio",pname,sep='.'),Portfolio,envir=.blotter)
+ return(pname) #not sure this is a good idea
}
###############################################################################
More information about the Blotter-commits
mailing list