Problem calculating account returns (AcctReturns) in Blotter

22 views Asked by At

I've spent the last few days on this, but I can't figure out how to calculate account returns using AcctReturns and PortfReturns in blotter in a way that the results makes sense.

This is the code:

library(xts)
library(blotter)
library(quantmod)
library(quantstrat)
library(PerformanceAnalytics)

rm(list = ls())

.blotter <- new.env()
.strategy <- new.env()

inicio_account <- "2021-02-10"
inicio_portfolio <- "2021-02-10"
nome_account <- "Igor_Account"
nome_portfolio <- "Igor_Portfolio"

tickers <- "NVDA"

getSymbols("NVDA", from = inicio_account, to = "2024-03-26", src = "yahoo", adjust = TRUE)
currency("USD")
stock("NVDA", currency = "USD", multiplier = 1)

###############################################################

initPortf(nome_portfolio, symbols = tickers, initPosQty = 0, initDate = inicio_portfolio)
initAcct(nome_account, portfolios = nome_portfolio, initDate = inicio_account, initEq=152.1574)

###############################################################

addTxn(nome_portfolio, "NVDA", "2021-02-11", TxnQty = 1, TxnPrice = 152.1574, verbose = TRUE)
#addAcctTxn(nome_account, as.Date("2023-08-07"), TxnType = "Additions", Amount = 100)

#addAcctTxn(nome_account, as.Date("2023-08-08"), TxnType = "Withdrawals", Amount = -100)

#addTxn(nome_portfolio, "NVDA", "2024-03-25", TxnQty = -1, TxnPrice = 950.0200, verbose = TRUE)

###############################################################

# Update everything

updatePortf(nome_portfolio)
updateAcct(nome_account)
updateEndEq(nome_account)

# Get the returns

p <- PortfReturns(Account = nome_account, Portfolio = nome_portfolio)
a <- AcctReturns(Account = nome_account)

# Calculate regular and log returns from stock data

t <- NVDA[-1]
ta <- t[,6]

ativo <- Return.calculate(ta, method = "discrete")
ativoret <- Return.cumulative(ativo, geometric = FALSE)
ativorets <- ativoret[1,1]

ativolog <- Return.calculate(ta, method = "discrete")
ativologret <- Return.cumulative(ativolog, geometric = TRUE)
ativologrets <- ativologret[1,1]

# Calculate cumulative portfolio and account returns

a <- a[-1]

pret <- Return.cumulative(p, geometric = FALSE)
prets <- pret[1,1]
pret_log <- Return.cumulative(p, geometric = TRUE)
prets_log <- pret_log[1,1]
aret <- Return.cumulative(a, geometric = FALSE)
arets <- aret[1,1]
aret_log <- Return.cumulative(a, geometric = TRUE)
arets_log <- aret_log[1,1]

# Print the results that we have

#print(paste0("Stock Returns: ", round(ativorets,5)*100,"%"))
print(paste0("Stock Log Returns: ", round(ativologrets,5)*100,"% <- Buy at 152.1574, Sell at 950.02, 524.367% is OK!"))
print(paste0("Portfolio Returns: ", round(prets,5)*100,"% <- Also OK!"))
#print(paste0("Portfolio Log Returns: ", round(prets_log,5)*100,"% <- ??"))
print(paste0("Account Returns: ", round(arets,5)*100,"% <- 586.069%? Why is not 524.366%?"))
print(paste0("Account Log Returns: ", round(arets_log,5)*100,"% <- 4.686%? What?"))

This is the output I get (which is wrong)

I've been working on developing a portfolio tracking tool to monitor the returns for my clients' portfolios/accounts. I started with a simple model focusing on a single-stock portfolio to verify if the results align with my manual calculations. This initial step is crucial for me as I plan to gradually refine the tool to accurately track returns for both stock and futures trading within the same account.

However, I've encountered an issue where the calculated portfolio returns accurately reflect the single-stock returns, but the account returns deviate significantly. In theory, for a single-stock portfolio, the stock returns, account returns, and portfolio returns should be identical, yet they aren't aligning as expected. If the account returns are supposed to be combined with PortfReturns to achieve the correct results, I'm unsure how to proceed.

Despite numerous attempts, including adjusting dates, conducting manual recalculations, experimenting with different stocks, and exploring various types of returns, the issue persists.

If anyone could share a light, that would be very helpful.

0

There are 0 answers