blotter icon indicating copy to clipboard operation
blotter copied to clipboard

Error in updatePortf() when using multiple currencies

Open evgeniavolkova opened this issue 3 years ago • 2 comments

Sometimes updatePortf() throws

Error in NextMethod(.Generic) : 
  number of items to replace is not a multiple of replacement length

After searching for the source of the error, found that it's caused by the line CcyMult <- CcyMult[index(TmpPeriods)].

It produces duplicate redundant dates in CcyMult when dates (and a number of rows in CcyMult) are already aligned with TmpPeriods. After executing this line the number of rows in CcyMult becomes greater than in TmpPeriods, which causes the error mentioned above.

A possible and ugly fix:

if ((nrow(CcyMult) != nrow(TmpPeriods))){
	CcyMult <- CcyMult[index(TmpPeriods)]
}

It helps to avoid the error, but there should be a better solution.

evgeniavolkova avatar Feb 20 '22 20:02 evgeniavolkova

Thanks for the report! Can you give a small, reproducible example that triggers the issue?

joshuaulrich avatar Feb 20 '22 20:02 joshuaulrich

It looks like the problem is in intraday deals.

devtools::install_github("braverock/blotter")
library('blotter')
library('quantmod')

options("getSymbols.warning4.0"=FALSE)
from ="2022-01-01"
to ="2022-01-15"
symbols = c("AAPL")
currency("USD")
currency("RUB")
getSymbols(symbols, from=from, to=to, 
           adjust=TRUE)
stock(symbols, currency="USD", multiplier=1)
getSymbols("RUB=X",src="yahoo",from="2022-01-01")
RUBUSD = `RUB=X`

initPortf('p', symbols = symbols, currency = 'RUB')

addTxn(Portfolio = 'p', Symbol = 'AAPL', TxnDate = as.Date("2022-01-04"),
       TxnQty = 1, TxnPrice = 179.70)
addTxn(Portfolio = 'p', Symbol = 'AAPL', TxnDate = as.Date("2022-01-04"),
       TxnQty = -1, TxnPrice = 179.70)

updatePortf('p')

evgeniavolkova avatar Feb 20 '22 20:02 evgeniavolkova