[Blotter-commits] r1495 - pkg/blotter/R
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Aug 14 14:01:06 CEST 2013
Author: braverock
Date: 2013-08-14 14:01:05 +0200 (Wed, 14 Aug 2013)
New Revision: 1495
Modified:
pkg/blotter/R/updateAcct.R
pkg/blotter/R/updatePortf.R
Log:
- convert tabs to spaces to clean up formatting
Modified: pkg/blotter/R/updateAcct.R
===================================================================
--- pkg/blotter/R/updateAcct.R 2013-08-14 03:32:01 UTC (rev 1494)
+++ pkg/blotter/R/updateAcct.R 2013-08-14 12:01:05 UTC (rev 1495)
@@ -12,17 +12,17 @@
a.ccy.str<-attr(Account,'currency')
}
- Portfolios = names(Account$portfolios)
-
- if(is.null(Dates)) Dates<-index(getPortfolio(Portfolios[1])$summary)[-1]
-
- #trim to only time prior to Dates
- if(last(index(Account$summary))>.parseISO8601(Dates)$first.time){
- whichi<-first(Account$summary[paste(.parseISO8601(Dates)$first.time,'::',sep=''), which.i = TRUE])
- if(!is.null(whichi)) whichi=whichi-1
- if(whichi<1) whichi=1
- Account$summary = Account$summary[1:whichi,]
- }
+ Portfolios = names(Account$portfolios)
+
+ if(is.null(Dates)) Dates<-index(getPortfolio(Portfolios[1])$summary)[-1]
+
+ #trim to only time prior to Dates
+ if(last(index(Account$summary))>.parseISO8601(Dates)$first.time){
+ whichi<-first(Account$summary[paste(.parseISO8601(Dates)$first.time,'::',sep=''), which.i = TRUE])
+ if(!is.null(whichi)) whichi=whichi-1
+ if(whichi<1) whichi=1
+ Account$summary = Account$summary[1:whichi,]
+ }
# Append the portfolio summary data to the portfolio slot
@@ -31,52 +31,52 @@
if(!is.null(attr(Portfolio,'currency'))) {
p.ccy.str<-attr(Portfolio,'currency')
}
-
+
# Test whether portfolio and account are of the same ccy
- psummary = Portfolio$summary[Dates]
- if( a.ccy.str != p.ccy.str ){
+ psummary = Portfolio$summary[Dates]
+ if( a.ccy.str != p.ccy.str ){
# If not, translate the portfolio summary to the account currency
- CcyMult <- NA
- port_currency<-try(getInstrument(p.ccy.str), silent=TRUE)
- if(inherits(port_currency,"try-error") | !is.instrument(port_currency)){
- warning("Currency",p.ccy.str," not found, using currency multiplier of 1")
- CcyMult<-1
- } else {
- FXrate.str<-paste(p.ccy.str,a.ccy.str,sep='') # currency quote convention is EURUSD which reads as "USD per EUR"
- FXrate<-try(get(FXrate.str), silent=TRUE)
- #TODO FIXME: this uses convention to sort out the rate, we should check $currency and $counter_currency and make sure directionality is correct
- if(inherits(FXrate,"try-error")){
- FXrate.str<-paste(a.ccy.str,p.ccy.str,sep='')
- FXrate<-try(get(FXrate.str), silent=TRUE)
- if(inherits(FXrate,"try-error")){
- warning("Exchange Rate",FXrate.str," not found for symbol,',Symbol,' using currency multiplier of 1")
- CcyMult<-1
- } else {
- invert=TRUE
- }
- }
- }
- if(is.na(CcyMult) && !is.na(FXrate)) {
- if(inherits(FXrate,'xts')){
- CcyMult <- FXrate[Dates]
- CcyMult <- na.locf(merge(CcyMult,index(psummary)))
- CcyMult <- drop(CcyMult[index(psummary)])
- } else {
- CcyMult<-as.numeric(FXrate)
- }
- } else {
- CcyMult<-1
- }
- if(isTRUE(invert)){
- # portfolio and instrument have different currencies, and FXrate was in the wrong direction
- CcyMult<-1/CcyMult
- }
-
- #multiply by the currency multiplier
- psummary<-psummary*CcyMult
+ CcyMult <- NA
+ port_currency<-try(getInstrument(p.ccy.str), silent=TRUE)
+ if(inherits(port_currency,"try-error") | !is.instrument(port_currency)){
+ warning("Currency",p.ccy.str," not found, using currency multiplier of 1")
+ CcyMult<-1
+ } else {
+ FXrate.str<-paste(p.ccy.str,a.ccy.str,sep='') # currency quote convention is EURUSD which reads as "USD per EUR"
+ FXrate<-try(get(FXrate.str), silent=TRUE)
+ #TODO FIXME: this uses convention to sort out the rate, we should check $currency and $counter_currency and make sure directionality is correct
+ if(inherits(FXrate,"try-error")){
+ FXrate.str<-paste(a.ccy.str,p.ccy.str,sep='')
+ FXrate<-try(get(FXrate.str), silent=TRUE)
+ if(inherits(FXrate,"try-error")){
+ warning("Exchange Rate",FXrate.str," not found for symbol,',Symbol,' using currency multiplier of 1")
+ CcyMult<-1
+ } else {
+ invert=TRUE
+ }
+ }
+ }
+ if(is.na(CcyMult) && !is.na(FXrate)) {
+ if(inherits(FXrate,'xts')){
+ CcyMult <- FXrate[Dates]
+ CcyMult <- na.locf(merge(CcyMult,index(psummary)))
+ CcyMult <- drop(CcyMult[index(psummary)])
+ } else {
+ CcyMult<-as.numeric(FXrate)
+ }
+ } else {
+ CcyMult<-1
+ }
+ if(isTRUE(invert)){
+ # portfolio and instrument have different currencies, and FXrate was in the wrong direction
+ CcyMult<-1/CcyMult
+ }
+
+ #multiply by the currency multiplier
+ psummary<-psummary*CcyMult
}
- # now bind it
- Account$portfolios[[pname]] = rbind(Account$portfolios[[pname]],psummary)
+ # now bind it
+ Account$portfolios[[pname]] = rbind(Account$portfolios[[pname]],psummary)
}
summary = NULL
@@ -136,11 +136,11 @@
Advisory.Fees = ,
Net.Performance = ,
End.Eq = {
- ## TODO no cash handling for now, add this in later, but for now, zeroes
+ ## TODO no cash handling for now, add this in later, but for now, zeroes
result = xts(rep(0,obsLength),order.by=obsDates)
}
)
-
+
colnames(result) = Attribute
if(is.null(summary)) {summary=result}
else {summary=cbind(summary,result)}
Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R 2013-08-14 03:32:01 UTC (rev 1494)
+++ pkg/blotter/R/updatePortf.R 2013-08-14 12:01:05 UTC (rev 1495)
@@ -1,16 +1,16 @@
#' update Portfilio P&L over a Dates range
-#'
+#'
#' The \code{updatePortf} function goes through each symbol and calculates the PL for each period prices are available.
#'
-#' Note that the portfolio will be marked on every time stamp where prices are available.
+#' Note that the portfolio will be marked on every time stamp where prices are available.
#' As such, your \code{Dates} range must reflect timestamps which appear in the price stream.
-#' Also note that you probably don't want to mark the portfolio on every tick,
-#'
-#'
+#' Also note that you probably don't want to mark the portfolio on every tick,
+#'
+#'
#' @return assigns position information and PL into the environment
-#'
+#'
#' @param Portfolio string identifying a portfolio
-#' @param Symbols character vector identifying symbols to update the portfolio for, default NULL
+#' @param Symbols character vector identifying symbols to update the portfolio for, default NULL
#' @param Dates xts-style ISO-8601 time range to run updatePortf over, default NULL (will use times from Prices
#' @param Prices optional xts object containing prices and timestamps to mark the book on, default NULL
#' @param \dots any other passthrough parameters
@@ -23,59 +23,59 @@
# FUNCTION
if(is.null(Symbols)){
Symbols = names(Portfolio$symbols)
- }
+ }
for(symbol in Symbols){
tmp_instr<-try(getInstrument(symbol), silent=TRUE)
- .updatePosPL(Portfolio=pname, Symbol=as.character(symbol), Dates=Dates, Prices=Prices, ...=...)
+ .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
- if(is.null(Dates)) Dates <- xts:::time.xts(Portfolio$symbols[[1]]$posPL) #not quite right, only using first symbol...
+ if(is.null(Dates)) Dates <- xts:::time.xts(Portfolio$symbols[[1]]$posPL) #not quite right, only using first symbol...
#Symbols = names(Portfolio$symbols)
Attributes = c('Long.Value', 'Short.Value', 'Net.Value', 'Gross.Value', 'Period.Realized.PL', 'Period.Unrealized.PL', 'Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')
summary = NULL
- tmp.attr=NULL
+ tmp.attr=NULL
for(attribute in Attributes) {
- result=NULL
+ result=NULL
switch(attribute,
- Net.Value =,
+ Net.Value =,
Gross.Value =,
- Long.Value =,
- Short.Value =,{
- # all these use Pos.Value
- if(is.null(tmp.attr)){
- table = .getBySymbol(Portfolio = Portfolio, Attribute = "Pos.Value", Dates = Dates, Symbols = Symbols)
- tmp.attr="Pos.Value"
- }
- switch(attribute,
- Gross.Value = { result = xts(rowSums(abs(table), na.rm=TRUE), order.by=index(table))},
- Long.Value = { tmat = apply(table,MARGIN=c(1,2),FUN=max,0)# comes out a matrix
- result = xts(rowSums(tmat, na.rm=TRUE), order.by=index(table))
- },
- Short.Value = { tmat = apply(table,MARGIN=c(1,2),FUN=min,0) # comes out a matrix
- result = xts(rowSums(tmat, na.rm=TRUE), order.by=index(table))
- },
- Net.Value = { result = xts(rowSums(table, na.rm=TRUE), order.by=index(table)) }
- )
+ Long.Value =,
+ Short.Value =,{
+ # all these use Pos.Value
+ if(is.null(tmp.attr)){
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = "Pos.Value", Dates = Dates, Symbols = Symbols)
+ tmp.attr="Pos.Value"
+ }
+ switch(attribute,
+ Gross.Value = { result = xts(rowSums(abs(table), na.rm=TRUE), order.by=index(table))},
+ Long.Value = { tmat = apply(table,MARGIN=c(1,2),FUN=max,0)# comes out a matrix
+ result = xts(rowSums(tmat, na.rm=TRUE), order.by=index(table))
},
- Period.Realized.PL =,
- Period.Unrealized.PL =,
- Gross.Trading.PL =,
- Txn.Fees =,
- Net.Trading.PL = {
- table = .getBySymbol(Portfolio = Portfolio, Attribute = attribute, Dates = Dates, Symbols = Symbols)
- tmp.attr = NULL
- result = xts(rowSums(table, na.rm=TRUE), order.by=index(table))
+ Short.Value = { tmat = apply(table,MARGIN=c(1,2),FUN=min,0) # comes out a matrix
+ result = xts(rowSums(tmat, na.rm=TRUE), order.by=index(table))
+ },
+ Net.Value = { result = xts(rowSums(table, na.rm=TRUE), order.by=index(table)) }
+ )
+ },
+ Period.Realized.PL =,
+ Period.Unrealized.PL =,
+ Gross.Trading.PL =,
+ Txn.Fees =,
+ Net.Trading.PL = {
+ table = .getBySymbol(Portfolio = Portfolio, Attribute = attribute, Dates = Dates, Symbols = Symbols)
+ tmp.attr = NULL
+ result = xts(rowSums(table, na.rm=TRUE), order.by=index(table))
}
)
-
+
colnames(result) = attribute
- if(is.null(summary)) {summary=result}
- else {summary=cbind(summary,result)}
+ if(is.null(summary)) {summary=result}
+ else {summary=cbind(summary,result)}
}
- # get rid of duplicated indices in the summary data,
+ # get rid of duplicated indices in the summary data,
# thanks to Guy Yollin for the bug report and Josh Ulrich for the elegant approach to fixing it
d <- duplicated(.index(summary)) | duplicated(.index(summary), fromLast=TRUE)
if(any(d)){
@@ -89,24 +89,24 @@
slist <- period.apply(summary.dups, c(0, which(ds)), f)
summary <- rbind(summary[!d,], slist) # put it all back together
}
-
- if(!is.timeBased(Dates)) Dates = xts:::time.xts(Portfolio$symbols[[1]][["posPL"]][Dates])
- startDate = first(xts:::.parseISO8601(Dates))$first.time-.00001
- # trim summary slot to not double count, related to bug 831 on R-Forge, and rbind new summary
- if( as.POSIXct(attr(Portfolio,'initDate'))>=startDate || length(Portfolio$summary)==0 ){
- Portfolio$summary<-summary #changes to subset might not return a empty dimnames set of columns
- }else{
- Portfolio$summary<-rbind(Portfolio$summary[paste('::',startDate,sep='')],summary)
- }
- # assign Portfolio to environment
- assign( paste("portfolio",pname,sep='.'), Portfolio, envir=.blotter )
-
+
+ if(!is.timeBased(Dates)) Dates = xts:::time.xts(Portfolio$symbols[[1]][["posPL"]][Dates])
+ startDate = first(xts:::.parseISO8601(Dates))$first.time-.00001
+ # trim summary slot to not double count, related to bug 831 on R-Forge, and rbind new summary
+ if( as.POSIXct(attr(Portfolio,'initDate'))>=startDate || length(Portfolio$summary)==0 ){
+ Portfolio$summary<-summary #changes to subset might not return a empty dimnames set of columns
+ }else{
+ 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
}
###############################################################################
# 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