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: [email protected]
To: [email protected] Cc: [email protected]
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(&quot;Year&quot;, &quot;Month&quot;, 
&quot;Value&quot;)
  monthNames <- c(&quot;Jan&quot;, &quot;Feb&quot;, &quot;Mar&quot;, 
&quot;Apr&quot;, &quot;May&quot;, &quot;Jun&quot;, &quot;Jul&quot;, 
&quot;Aug&quot;, &quot;Sep&quot;, &quot;Oct&quot;, &quot;Nov&quot;, 
&quot;Dec&quot;)
  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 
<[email protected] <mailto:[email protected]> > 
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]]
_______________________________________________
[email protected] <mailto:[email protected]> 
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]]

_______________________________________________
[email protected] mailing list
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.

Reply via email to