[R-SIG-Finance] PerformanceAnalytics::table.CalendarReturns

Amarjit Chandhial @@ch@ndh|@| @end|ng |rom bt|nternet@com
Wed Sep 4 23:17:44 CEST 2024


 
Thanks Ilya - including helper functions: Creating a Table of Monthly 
Returns With R and a Volatility Trading Interview | QuantStrat TradeR 
<https://quantstrattrader.com/2018/02/20/creating-a-table-of-monthly-returns-with-r-and-a-volatility-trading-interview/>
 
Cheers,
Amarjit
 
 
 
------ Original Message ------
From: ilya.kipnis using gmail.com
To: a.chandhial using btinternet.com Cc: r-sig-finance using r-project.org
Sent: Wednesday, September 4th 2024, 21:20
Subject: Re: [R-SIG-Finance] PerformanceAnalytics::table.CalendarReturns
 
Amarjit,
Maybe this is something you'd find useful?
 
require(data.table)
calendarReturnTable <- function(rets, digits = 3, percent = FALSE) {
  
  # get maximum drawdown using daily returns
  dds <- apply.yearly(rets, maxDrawdown)
  
  # get monthly returns
  rets <- apply.monthly(rets, Return.cumulative)
  
  # convert to data frame with year, month, and monthly return value
  dfRets <- cbind(year(index(rets)), month(index(rets)), coredata(rets))
  
  # convert to data table and reshape into year x month table
  dfRets <- data.frame(dfRets)
  colnames(dfRets) <- c("Year", "Month", 
"Value")
  monthNames <- c("Jan", "Feb", "Mar", 
"Apr", "May", "Jun", "Jul", 
"Aug", "Sep", "Oct", "Nov", 
"Dec")
  for(i in 1:length(monthNames)) {
    dfRets$Month[dfRets$Month==i] <- monthNames[i]
  }
  dfRets <- data.table(dfRets)
  dfRets <- data.table::dcast(dfRets, Year~Month)
  
  # create row names and rearrange table in month order
  dfRets <- data.frame(dfRets)
  yearNames <- dfRets$Year
  rownames(dfRets) <- yearNames; dfRets$Year <- NULL
  dfRets <- dfRets[,monthNames]
  
  # append yearly returns and drawdowns
  yearlyRets <- apply.yearly(rets, Return.cumulative)
  dfRets$Annual <- yearlyRets
  dfRets$DD <- dds
  
  # convert to percentage
  if(percent) {
    dfRets <- dfRets * 100
  }
  
  # round for formatting
  dfRets <- apply(dfRets, 2, round, digits)
  
  # paste the percentage sign
  if(percent) {
    dfRets <- apply(dfRets, 2, pastePerc)
    dfRets <- apply(dfRets, 2, rowGsub)
    dfRets <- data.frame(dfRets)
    rownames(dfRets) <- yearNames
  }
  return(dfRets)
}
 

 
On Wed, Sep 4, 2024 at 3:45 PM Amarjit Chandhial via R-SIG-Finance 
<r-sig-finance using r-project.org <mailto:r-sig-finance using r-project.org> > 
wrote:
 

Hi,
 
Are there any plans for 
https://timelyportfolio.github.io/PerformanceAnalytics/reference/table.CalendarReturns.html 
<https://timelyportfolio.github.io/PerformanceAnalytics/reference/table.CalendarReturns.html> 
 
function to handle daily returns aggregated to monthly and year?
 
It would be useful to have a table displaying Monthly Returns and Total 
Return (rows), by year's (columns), for daily returns.
 
Amarjit
        [[alternative HTML version deleted]]
_______________________________________________
R-SIG-Finance using r-project.org <mailto:R-SIG-Finance using r-project.org> 
mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance 
<https://stat.ethz.ch/mailman/listinfo/r-sig-finance>
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions 
should go.
 

	[[alternative HTML version deleted]]



More information about the R-SIG-Finance mailing list