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

AcctReturns appears wrong; doesn't match PortfReturns, possible patch #50

Open
rogerjbos opened this issue May 12, 2017 · 0 comments
Open

Comments

@rogerjbos
Copy link

In the example below I get a 6.9% portfolio return and a slightly negative account return and the problem is with the account return. I took a look at the function and found one way to fix it, but I might be way off base, please let me know.

In AcctReturns line 102 adds the daily P&L to initEq...

V = initEq + reclass(rowSums(table), table)                  # Account values

... but it seems we need the cumulative P&L, as such:

V = initEq + reclass(cumsum(rowSums(table)), table)                  # Account values

Also, line 107 calculates the returns as:

  returns =  V  / (lag(V) + CF) - 1

But the return on the first day is NA, so we need to fill it in, as such:

  returns =  V  / (lag(V) + CF) - 1
  returns[1] = V[1] / initEq - 1

This produces an account return which matches closely to the portfolio return. I am really interested in hearing feedback on this patch.

Start Example Code

require(blotter)
require(FinancialInstrument)
require(quantmod)
rm(.blotter)
if(!exists(".instrument")) .instrument <<- new.env()
if(!exists(".blotter")) .blotter <<- new.env()

currency("USD")
symbols = c("IBM")
for(symbol in symbols){ # establish tradable instruments
stock(symbol, currency="USD", multiplier=1)
}

getSymbols(symbols, from='2017-01-01', to='2017-01-31', src='yahoo', index.class=c("POSIXt","POSIXct"))

initPortf("p", symbols=symbols, currency="USD")

addTxn(Portfolio = "p", Symbol = "IBM", TxnDate = '2017-01-03', TxnQty = 1, TxnPrice = 166, TxnFees = 00)
addTxn("p", "IBM", '2017-01-27', -1, 177.30, TxnFees = 0)

updatePortf(Portfolio="p",Dates='2017-01')

initAcct(name="a", portfolios="p", initEq=166, currency="USD")
updateAcct("a",'2017-01')
updateEndEq("a",'2017-01')

portfolio_rets <- PortfReturns(Account="a", Dates="2017", Portfolios="p")
Return.cumulative(portfolio_rets)

account_rets <- AcctReturns(Account="a", Dates="2017", Portfolios="p")
Return.cumulative(account_rets)

p = getPortfolio("p")
a = getAccount("a")

account_test <- CalculateReturns(a$summary$End.Eq)
cbind(portfolio_rets, account_rets, account_test)

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

1 participant