Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in updatePortf() when using multiple currencies #117

Open
evgeniavolkova opened this issue Feb 20, 2022 · 4 comments
Open

Error in updatePortf() when using multiple currencies #117

evgeniavolkova opened this issue Feb 20, 2022 · 4 comments

Comments

@evgeniavolkova
Copy link

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.

@joshuaulrich
Copy link
Collaborator

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

@evgeniavolkova
Copy link
Author

evgeniavolkova commented Feb 20, 2022

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')

@fmoudoute
Copy link

hi @evgeniavolkova did you find a way around this please ? I am facing the same issue on intraday trades

@braverock
Copy link
Owner

braverock commented Nov 18, 2024

I can't replicate the issue, so I suspect maybe a timezone problem creating duplicate entries on different timestamps or dates. I always run R in 'UTC' timezone, and all of our local data is 'UTC', and 'Date' indexed data is UTC.

The Currency multiplier from the last observation should be applied until there is a new observation.

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')
[1] "USD"
[1] "RUB"
NULL
[1] "AAPL"
[1] "RUB=X"
[1] "p"
[1] "2022-01-04 00:00:00 AAPL 1 @ 179.7"
[1] "2022-01-04 00:00:00 AAPL -1 @ 179.7"

Also, we routinely use intraday trade and FX quote data with currency quotes that are not aligned to transaction timestamps, so at least at that level, we don't experience this problem either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants