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

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Mar 28 19:05:22 CEST 2010


Author: braverock
Date: 2010-03-28 19:05:22 +0200 (Sun, 28 Mar 2010)
New Revision: 311

Modified:
   pkg/blotter/R/initAcct.R
   pkg/blotter/R/initPortf.R
   pkg/blotter/R/updatePortf.R
   pkg/blotter/R/updatePosPL.R
Log:
- initial revision of worknig currency support for updatePortf 
  TODO integrate into updateAcct

Modified: pkg/blotter/R/initAcct.R
===================================================================
--- pkg/blotter/R/initAcct.R	2010-03-28 15:40:03 UTC (rev 310)
+++ pkg/blotter/R/initAcct.R	2010-03-28 17:05:22 UTC (rev 311)
@@ -45,8 +45,9 @@
 #' @param portfolios 
 #' @param initDate 
 #' @param initEq 
+#' @param currency
 #' @export
-initAcct <- function(name='default', portfolios, initDate="1950-01-01", initEq=0)
+initAcct <- function(name='default', portfolios, initDate="1950-01-01", initEq=0, currency='USD')
 { # @author Peter Carl
 
     if(exists(paste("account",name,sep='.'), envir=.blotter,inherits=TRUE)) 
@@ -63,6 +64,7 @@
     }
     # return(account)
     class(account)<-c("portfolio_account","account")
+    attr(account,'currency')<-currency
     assign(paste("account",as.character(name),sep='.'),account,envir=.blotter)  
     return(name) # not sure this is a good idea
 }

Modified: pkg/blotter/R/initPortf.R
===================================================================
--- pkg/blotter/R/initPortf.R	2010-03-28 15:40:03 UTC (rev 310)
+++ pkg/blotter/R/initPortf.R	2010-03-28 17:05:22 UTC (rev 311)
@@ -18,7 +18,7 @@
 #' @param initDate date prior to the first close price given, used to contain initial account equity and initial position
 #' @author Peter Carl
 #' @export
-initPortf <- function(name="default", symbols, initPosQty = 0, initDate = '1950-01-01')
+initPortf <- function(name="default", symbols, initPosQty = 0, initDate = '1950-01-01', currency='USD')
 { # @author Peter Carl
     if(exists(paste("portfolio",name,sep='.'), envir=.blotter,inherits=TRUE))
         stop(paste("Portfolio",name,"already exists, use updatePortf() or addPortfInstr() to update it."))
@@ -36,6 +36,7 @@
         portfolio[[instrument]]$posPL = initPosPL(initDate = initDate, initPosQty = initPosQty[i])
     }
     class(portfolio)<-c("blotter_portfolio", "portfolio")
+    attr(portfolio,'currency')<-currency
     #return(portfolio)
     assign(paste("portfolio",as.character(name),sep='.'),portfolio,envir=.blotter)
     return(name) # not sure this is a good idea

Modified: pkg/blotter/R/updatePortf.R
===================================================================
--- pkg/blotter/R/updatePortf.R	2010-03-28 15:40:03 UTC (rev 310)
+++ pkg/blotter/R/updatePortf.R	2010-03-28 17:05:22 UTC (rev 311)
@@ -25,7 +25,7 @@
     } 
     for(symbol in Symbols){
         tmp_instr<-try(getInstrument(symbol))
-        updatePosPL(pname, symbol, Dates, Prices=Prices)            
+        updatePosPL(Portfolio=pname, Symbol=as.character(symbol), Dates=Dates, Prices=Prices)            
     }
     return(pname) #not sure this is a good idea
 }

Modified: pkg/blotter/R/updatePosPL.R
===================================================================
--- pkg/blotter/R/updatePosPL.R	2010-03-28 15:40:03 UTC (rev 310)
+++ pkg/blotter/R/updatePosPL.R	2010-03-28 17:05:22 UTC (rev 311)
@@ -41,15 +41,41 @@
     if(is.null(ConMult) | !hasArg(ConMult)){
         tmp_instr<-try(getInstrument(Symbol))
         if(inherits(tmp_instr,"try-error") | !is.instrument(tmp_instr)){
-            warning(paste("Instrument",Symbol," not found, using contract multiplier of 1"))
+            warning("Instrument",Symbol," not found, using contract multiplier of 1")
             ConMult<-1
         } else {
-            ConMult<-tmp_instr$multiplier
+            ConMult<-tmp_instr$multiplier # TODO ultimately this needs to be time series aware
         }  
     }
-    PrevConMult = 1 ## @TODO: Change this to look up the value from instrument?
-    CcyMult =1 ## @TODO: Change this to look up the value from instrument?
-    PrevCcyMult =1 ## @TODO: Change this to look up the value from instrument?
+    CcyMult = NULL 
+    FXrate = NULL
+    if(!is.null(attr(Portfolio,'currency'))) {
+        p.ccy.str<-attr(Portfolio,'currency')
+        if (tmp_instr$currency==p.ccy.str) {
+            CcyMult<-1
+        } else {
+            port_currency<-try(getInstrument(p.ccy.str))
+            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(tmp_instr$currency,p.ccy.str,sep='')
+                FXrate<-try(get(FXrate.str))
+                if(inherits(FXrate,"try-error")){
+                    FXrate.str<-paste(p.ccy.str,tmp_instr$currency,sep='')
+                    FXrate<-try(get(FXrate.str))
+                    if(inherits(FXrate,"try-error")){ 
+                        warning("Exchange Rate",FXrate.str," not found for symbol,',Symbol,' using currency multiplier of 1")
+                        CcyMult<-1
+                    }
+                }
+            }
+        }
+    } else {
+        message("no currency multiplier set on portfolio, using currency multiplier of 1")
+        CcyMult =1
+    }
+    #PrevCcyMult =1 ## @TODO: Change this to look up the value from instrument?
     
     # For each date, calculate realized and unrealized P&L
     for(i in 1:length(Dates)){ ##
@@ -68,13 +94,25 @@
         PrevSpan = paste(PriorPrevDateLast, PrevDate, sep="::")
         if(length(PrevDate)==0)
              PrevDate = NA
-        
+
+        if(is.null(CcyMult) & !is.null(FXrate)) {
+            if(inherits(FXrate,'xts')){
+                CcyMult<-as.numeric(last(FXrate[paste('::',as.character(CurrentDate),sep='')]))
+                PrevCcyMult<-as.numeric(last(FXrate[paste('::',as.character(PrevDateLast),sep='')]))
+            } else {
+                CcyMult<-as.numeric(FXrate)
+                PrevCcyMult<-CcyMult
+            }
+        } else {
+            CcyMult<-1
+            PrevCcyMult<-CcyMult
+        }
         #TODO write a single getTxn and use the values instead of these lines
-        TxnValue = getTxnValue(pname, Symbol, CurrentSpan)
-        TxnFees = getTxnFees(pname, Symbol, CurrentSpan)
+        TxnValue = getTxnValue(pname, Symbol, CurrentSpan)*CcyMult
+        TxnFees = getTxnFees(pname, Symbol, CurrentSpan)*CcyMult
         PosQty = getPosQty(pname, Symbol, as.character(CurrentDate))
         
-        ClosePrice = as.numeric(last(Prices[CurrentDate, grep("Close", colnames(Prices))])) #not necessary
+        ClosePrice = as.numeric(last(Prices[CurrentDate, grep("Close", colnames(Prices))]))*CcyMult
         PosValue = calcPosValue(PosQty, ClosePrice, ConMult)
 
         if(is.na(PrevDate))
@@ -85,16 +123,14 @@
         if(PrevPosQty==0)
             PrevClosePrice = 0
         else
-            PrevClosePrice = as.numeric(Cl(Prices)[as.character(PrevDate)])
+            PrevClosePrice = as.numeric(Cl(Prices)[as.character(PrevDate)])*PrevCcyMult
 
         PrevPosValue = calcPosValue(PrevPosQty, PrevClosePrice, ConMult) ### @TODO: PrevConMult?
         GrossTradingPL = PosValue - PrevPosValue - TxnValue
         NetTradingPL = GrossTradingPL + TxnFees # Fees are assumed to have negative values
-
-        UnrealizedPL = PosQty*(ClosePrice-getPosAvgCost(Portfolio=pname, Symbol, CurrentDate))*ConMult
-
+        #$unrealized_gl    = $end_return['calc_position'] * ($end_return['last_price'] - $end_return['average_cost']);
+        UnrealizedPL = PosQty*(ClosePrice-(getPosAvgCost(Portfolio=pname, Symbol, CurrentDate)*CcyMult))*ConMult
         RealizedPL = round(GrossTradingPL - UnrealizedPL,2)
-        #$unrealized_gl    = $end_return['calc_position'] * ($end_return['last_price'] - $end_return['average_cost']);
 
         NewPeriod = as.xts(t(c(PosQty, ConMult, CcyMult, PosValue, TxnValue, RealizedPL, UnrealizedPL, GrossTradingPL, TxnFees, NetTradingPL)), order.by=as.POSIXct(CurrentDate)) #, format=tformat
         colnames(NewPeriod) = c('Pos.Qty', 'Con.Mult', 'Ccy.Mult', 'Pos.Value', 'Txn.Value',  'Realized.PL', 'Unrealized.PL','Gross.Trading.PL', 'Txn.Fees', 'Net.Trading.PL')



More information about the Blotter-commits mailing list