You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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...
... but it seems we need the cumulative P&L, as such:
Also, line 107 calculates the returns as:
But the return on the first day is NA, so we need to fill it in, as such:
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)
The text was updated successfully, but these errors were encountered: