[R-SIG-Finance] Problem with portfolio/blotter and base currencies

Worik Stanton worik.stanton at gmail.com
Fri Oct 12 05:15:06 CEST 2012


Friends

Running the attached code gives the following output:

> go(baseCur="AAA")
[1] "1970-01-02 00:00:00 AAABBB 100 @ 1.075"
[1] "1970-01-03 00:00:00 AAABBB -100 @ 1.4802"
Return:  -1.421085e-16
getEndEq: 100
curEq 140.52
> go(baseCur="BBB")
[1] "1970-01-02 00:00:00 AAABBB 100 @ 1.075"
[1] "1970-01-03 00:00:00 AAABBB -100 @ 1.4802"
Return:  0.4052
getEndEq: 140.52
curEq 140.52

The initial equity is set to 100.  When baseCur is "AAA" the endEq
should have moved, surly?

Switching the base currency of the Account and Portfolio objects has a
huge effect. 

I need to clear my head and re-examine how I am defining my curEq, not
sure I have that one corrrect in the case of baseCur == "BBB", but when
baseCur=="AAA" given the transactions, the equity should move.

Am I loosing my mind or is there a bug somewhere in blotter?  (I suspect
in .updatePosPL(..))

cheers
Worik

-- 
it does not matter                      I think that I shall never see
how much I dig and dig                    A billboard lovely as a tree
this hole just                      Indeed, unless the billboards fall
keeps getting deeper                      I'll never see a tree at all

-------------- next part --------------
## required libraries
require(quantmod)
require(TTR)
require(blotter)

go(baseCur="AAA")
go(baseCur="BBB")

go <- function(baseCur="BBB"){

  suppressWarnings(try(rm("account.testy","portfolio.testy",pos=.blotter),silent=TRUE))
  suppressWarnings(rm_exchange_rates("AAABBB"))
  suppressWarnings(rm_currencies("AAA"))
  suppressWarnings(rm_currencies("BBB"))
  suppressWarnings(try(rm("portfolio","account","N","symbol",
                          "ClosePrice", AAABBB, 
                          "CurrentDate","equity","Units",
                          "maxUnits","size","Stop",
                          "equity","TxnPrice","initDate",
                          "initEq","Posn","verbose"),silent=TRUE)
                   )

  idx <- as.Date(1:2)
  AAABBB <- xts(c(1.075, 1.4802), order.by=idx)
  names(AAABBB) <- "Close"
  assign("AAABBB", AAABBB, pos=1)

  currency("AAA")
  currency("BBB")
  exchange_rate("AAABBB", "BBB", "AAA", 0.00001)  
  ## Set up a portfolio object and an account object

  initEq <- 100
  curEq <- initEq
  initDate <- start(AAABBB)-1
  portfolio = "testy" 
  assign("portfolio", portfolio, pos=1)
  initPortf(name=portfolio, "AAABBB", initDate=initDate, currency=baseCur)
  account = "testy"
  assign("account", account, pos=1)
  initAcct(name=account,portfolios="testy", initDate=initDate,
           initEq=initEq, currency=baseCur)

  TxnDate <- idx[1]
  TxnPrice <- as.numeric(Cl(AAABBB[TxnDate,]))
  TxnQty <- 100
  curEq <- curEq - (TxnQty * TxnPrice)
  addTxn(Portfolio=portfolio,
         TxnDate=TxnDate,
         TxnQty = TxnQty,
         Symbol="AAABBB",
         TxnPrice=TxnPrice,
         TxnFees=0, verbose=TRUE)

  updatePortf(Portfolio = portfolio, Dates = TxnDate)
  updateAcct(account, Dates = TxnDate)
  updateEndEq(account, Dates = TxnDate)
  
  TxnDate <- idx[2]
  TxnPrice <- as.numeric(Cl(AAABBB[TxnDate,]))
  TxnQty <- -100
  curEq <- curEq - (TxnQty * TxnPrice)
  addTxn(Portfolio=portfolio,
         TxnDate=TxnDate,
         TxnQty = TxnQty,
         Symbol="AAABBB",
         TxnPrice=TxnPrice,
         TxnFees=0, verbose=TRUE)

  updatePortf(Portfolio = portfolio, Dates = TxnDate)
  updateAcct(account, Dates = TxnDate)
  updateEndEq(account, Dates = TxnDate)

  endDate <- end(AAABBB)
  cat('Return: ',
      (getEndEq(Account=account, Date=endDate)-initEq)/initEq,'\n')

  cat("getEndEq:", getEndEq(account, endDate), "\n")
  cat("curEq", curEq, "\n")
  
}#go()



More information about the R-SIG-Finance mailing list