[Returnanalytics-commits] r3646 - pkg/PApages/R

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri May 22 03:54:36 CEST 2015


Author: peter_carl
Date: 2015-05-22 03:54:35 +0200 (Fri, 22 May 2015)
New Revision: 3646

Added:
   pkg/PApages/R/xlsx.CAPM.R
   pkg/PApages/R/xlsx.Calendar.R
   pkg/PApages/R/xlsx.RiskStats.R
   pkg/PApages/R/xlsx.tables.R
Log:
- this is probably where these should be


Added: pkg/PApages/R/xlsx.CAPM.R
===================================================================
--- pkg/PApages/R/xlsx.CAPM.R	                        (rev 0)
+++ pkg/PApages/R/xlsx.CAPM.R	2015-05-22 01:54:35 UTC (rev 3646)
@@ -0,0 +1,60 @@
+# Add the formatted output of table.CAPM to an Excel workbook
+
+xlsx.CAPM <- function(Ra, Rb, p=(1-(1/12)), Rf=.03/12, wb, sheetname="SFM", title="Regression Statistics", subtitle="Since inception") {
+  # Peter Carl
+  require(xlsx)
+  
+  # Calculate the table
+  # @TODO: Do this from manager inception instead
+  # @TODO: Get rid of leading 'X' in column names
+  x.capm = t(table.CAPM(R, as.perc=FALSE, digits=4))
+  x.dim = dim(x.capm)
+  
+  ## Set style attributes for the spreadsheet
+  # Create a named cell style to be used for columns of ratios or percentages
+  csSheetTitle <- CellStyle(wb) + Font(wb, heightInPoints=14, isBold=TRUE)
+  csSheetSubTitle <- CellStyle(wb) + Font(wb, heightInPoints=12, isItalic=TRUE, isBold=FALSE)
+  csTableRowNames <- CellStyle(wb) + Font(wb, isBold=TRUE)
+  csTableColNames <- CellStyle(wb) + Font(wb, isBold=TRUE) + Alignment(wrapText=TRUE, h="ALIGN_CENTER") + Border(color="black", position=c("TOP", "BOTTOM"), pen=c("BORDER_THIN", "BORDER_THICK")) 
+  csRatioColumn <- CellStyle(wb, dataFormat=DataFormat("0.0")) # ... for ratio results
+  csPercColumn <- CellStyle(wb, dataFormat=DataFormat("0.0%")) # ... for percentage results
+  
+  CAPM.colRatio = list(
+    '3'=csRatioColumn,
+    '5'=csRatioColumn,
+    '8'=csRatioColumn,
+    '15'=csRatioColumn)
+  CAPM.colPerc =list(
+    '1'=csPercColumn,
+    '2'=csPercColumn,
+    '4'=csPercColumn,
+    '6'=csPercColumn,
+    '7'=csPercColumn,
+    '9'=csPercColumn,
+    '10'=csPercColumn,
+    '13'=csPercColumn,
+    '14'=csPercColumn)
+  
+  # Create a sheet in the workbook, add the table, and format it
+  sheet <- createSheet(wb, sheetName = sheetname)
+  addDataFrame(x.RiskStats, sheet, startRow=3, startColumn=1, 
+               colStyle=c(CAPM.colPerc,CAPM.colRatio), 
+               colnamesStyle = csTableColNames, rownamesStyle=csTableRowNames)
+
+  setColumnWidth(sheet,colIndex=c(2:x.dim[1]),colWidth=8)
+  setColumnWidth(sheet,colIndex=1,colWidth=max(nchar(rownames(x.calendar))))
+  
+  # Create the Sheet title ...
+  rows <- createRow(sheet,rowIndex=1)
+  sheetTitle <- createCell(rows, colIndex=1)
+  setCellValue(sheetTitle[[1,1]], title)
+  setCellStyle(sheetTitle[[1,1]], csSheetTitle)
+  # ... and subtitle
+  rows <- createRow(sheet,rowIndex=2)
+  sheetSubTitle <- createCell(rows,colIndex=1)
+  setCellValue(sheetSubTitle[[1,1]], subtitle)
+  setCellStyle(sheetSubTitle[[1,1]], csSheetSubTitle)
+  
+  # Return the whole (now modified) workbook object
+  return(wb)
+}
\ No newline at end of file

Added: pkg/PApages/R/xlsx.Calendar.R
===================================================================
--- pkg/PApages/R/xlsx.Calendar.R	                        (rev 0)
+++ pkg/PApages/R/xlsx.Calendar.R	2015-05-22 01:54:35 UTC (rev 3646)
@@ -0,0 +1,45 @@
+# Add the formatted output of table.CalendarReturns to an Excel workbook
+
+xlsx.Calendar <- function(R, p=(1-(1/12)), Rf=.03/12, wb, sheetname="Risk Stats", title="Risk Statistics", subtitle="Since inception") {
+  # Peter Carl
+  require(xlsx)
+  
+  # Calculate the table
+  # @TODO: Do this from manager inception instead
+  # @TODO: Get rid of leading 'X' in column names
+  x.calendar = t(table.CalendarReturns(R, as.perc=FALSE, digits=4))
+  x.dim = dim(x.calendar)
+  
+  ## Set style attributes for the spreadsheet
+  # Create a named cell style to be used for columns of ratios or percentages
+  csSheetTitle <- CellStyle(wb) + Font(wb, heightInPoints=14, isBold=TRUE)
+  csSheetSubTitle <- CellStyle(wb) + Font(wb, heightInPoints=12, isItalic=TRUE, isBold=FALSE)
+  csTableRowNames <- CellStyle(wb) + Font(wb, isBold=TRUE)
+  csTableColNames <- CellStyle(wb) + Font(wb, isBold=TRUE) + Alignment(wrapText=TRUE, h="ALIGN_CENTER") + Border(color="black", position=c("TOP", "BOTTOM"), pen=c("BORDER_THIN", "BORDER_THICK")) 
+  csPercColumn <- CellStyle(wb, dataFormat=DataFormat("0.0%")) # ... for percentage results
+  
+  # Create a sheet in the workbook, add the table, and format it
+  # @TODO: Fix table cell formatting
+  sheet <- createSheet(wb, sheetName = sheetname)
+  addDataFrame(x.calendar, sheet, startRow=3, startColumn=1, 
+               colStyle=list(csPercColumn), 
+               colnamesStyle = csTableColNames, 
+               rownamesStyle=csTableRowNames)
+
+  setColumnWidth(sheet,colIndex=c(2:x.dim[1]),colWidth=8)
+  setColumnWidth(sheet,colIndex=1,colWidth=max(nchar(rownames(x.calendar))))
+  
+  # Create the Sheet title ...
+  rows <- createRow(sheet,rowIndex=1)
+  sheetTitle <- createCell(rows, colIndex=1)
+  setCellValue(sheetTitle[[1,1]], title)
+  setCellStyle(sheetTitle[[1,1]], csSheetTitle)
+  # ... and subtitle
+  rows <- createRow(sheet,rowIndex=2)
+  sheetSubTitle <- createCell(rows,colIndex=1)
+  setCellValue(sheetSubTitle[[1,1]], subtitle)
+  setCellStyle(sheetSubTitle[[1,1]], csSheetSubTitle)
+  
+  # Return the whole (now modified) workbook object
+  return(wb)
+}
\ No newline at end of file

Added: pkg/PApages/R/xlsx.RiskStats.R
===================================================================
--- pkg/PApages/R/xlsx.RiskStats.R	                        (rev 0)
+++ pkg/PApages/R/xlsx.RiskStats.R	2015-05-22 01:54:35 UTC (rev 3646)
@@ -0,0 +1,65 @@
+# Add the formatted output of table.RiskStats to an Excel workbook
+# 
+# wb = xlsx.RiskStats.R(R=last(x.R,60), wb=wb)
+
+# @TODO: Wrap each table in a similar function: Calendar, etc.
+
+xlsx.RiskStats <- function(R, p=(1-(1/12)), Rf=.03/12, wb, sheetname="Risk Stats", title="Risk Statistics", subtitle="Since inception") {
+  # Peter Carl
+  require(xlsx)
+  
+  # Calculate the table
+  x.RiskStats = as.data.frame(t(table.RiskStats(R=R, p=p, Rf=Rf)))
+  
+  ## Set style attributes for the spreadsheet
+  # Create a named cell style to be used for columns of ratios or percentages
+  csSheetTitle <- CellStyle(wb) + Font(wb, heightInPoints=14, isBold=TRUE)
+  csSheetSubTitle <- CellStyle(wb) + Font(wb, heightInPoints=12, isItalic=TRUE, isBold=FALSE)
+  csTableRowNames <- CellStyle(wb) + Font(wb, isBold=TRUE)
+  csTableColNames <- CellStyle(wb) + Font(wb, isBold=TRUE) + Alignment(wrapText=TRUE, h="ALIGN_CENTER") + Border(color="black", position=c("TOP", "BOTTOM"), pen=c("BORDER_THIN", "BORDER_THICK")) 
+  csRatioColumn <- CellStyle(wb, dataFormat=DataFormat("0.0")) # ... for ratio results
+  csPercColumn <- CellStyle(wb, dataFormat=DataFormat("0.0%")) # ... for percentage results
+  
+  # Which columns in the table should be formatted which way?
+  RiskStats.colRatio = list(
+    '3'=csRatioColumn,
+    '5'=csRatioColumn,
+    '8'=csRatioColumn,
+    '15'=csRatioColumn)
+  RiskStats.colPerc =list(
+    '1'=csPercColumn,
+    '2'=csPercColumn,
+    '4'=csPercColumn,
+    '6'=csPercColumn,
+    '7'=csPercColumn,
+    '9'=csPercColumn,
+    '10'=csPercColumn,
+    '13'=csPercColumn,
+    '14'=csPercColumn)
+  
+  # Create a sheet in the workbook, add the table, and format it
+#   wb = set.xlsxWBStyles(wb) # Establish formats in the wb in case it hasn't happened before
+  sheet <- createSheet(wb, sheetName = sheetname)
+  addDataFrame(x.RiskStats, sheet, startRow=3, startColumn=1, 
+               colStyle=c(RiskStats.colPerc,RiskStats.colRatio), 
+               colnamesStyle = csTableColNames, rownamesStyle=csTableRowNames)
+  setColumnWidth(sheet,colIndex=c(2:15),colWidth=11)
+  setColumnWidth(sheet,colIndex=16,colWidth=13)
+  setColumnWidth(sheet,colIndex=17,colWidth=6)
+  setColumnWidth(sheet,colIndex=1,colWidth=max(nchar(rownames(x.RiskStats))))
+  
+  # Create the Sheet title ...
+  rows <- createRow(sheet,rowIndex=1)
+  sheetTitle <- createCell(rows, colIndex=1)
+  setCellValue(sheetTitle[[1,1]], title)
+  setCellStyle(sheetTitle[[1,1]], csSheetTitle)
+  # ... and subtitle
+  rows <- createRow(sheet,rowIndex=2)
+  sheetSubTitle <- createCell(rows,colIndex=1)
+  setCellValue(sheetSubTitle[[1,1]], subtitle)
+  setCellStyle(sheetSubTitle[[1,1]], csSheetSubTitle)
+  
+  # Return the whole (now modified) workbook object
+  return(wb)
+}
+

Added: pkg/PApages/R/xlsx.tables.R
===================================================================
--- pkg/PApages/R/xlsx.tables.R	                        (rev 0)
+++ pkg/PApages/R/xlsx.tables.R	2015-05-22 01:54:35 UTC (rev 3646)
@@ -0,0 +1,57 @@
+xlsx.Performance <- function (R, title, outputdir, timestamp) {
+  # Create a spreadsheet with performance and risk metrics
+  try(rm(wb)) # delete old or outdated objects in the workspace
+  
+  # @TODO: code after this could be functionalized as xlsx.Performance()
+  
+  # Create an Excel workbook using xlsx
+  wb <- createWorkbook() # Create a workbook object
+  
+  # Get attributes of the data used
+  x.lastDate = tail(index(x.R), n=1)
+  
+  ## Calculate Ex Post Risk and Returns Statistics table
+  # Create a sheet for Since Inception
+  sheetname = paste(manager," Stats", sep="")
+  title = paste(manager, "Ex-Post Returns and Risk")
+  subtitle = paste("Since inception, updated through ", x.lastDate, sep="")
+  wb = xlsx.RiskStats(R=x.R, wb=wb, sheetname=sheetname, title=title, subtitle=subtitle)
+  
+  # Trailing n-month view of the same table
+  periods = c(60,36,24,12)
+  for(period in periods){
+    sheetname=paste(manager," Stats ", period,"m", sep="")
+    title = paste(manager, "Ex-Post Returns and Risk")
+    subtitle = paste("Trailing ", period, "-month period through ", x.lastDate, sep="")
+    wb = xlsx.RiskStats(R=last(x.R, period), wb=wb, sheetname=sheetname, title=title, subtitle=subtitle)
+  }
+  
+  ## Calculate Calendar Returns table
+  sheetname = paste(manager," Returns", sep="")
+  title = paste(manager, "Calendar Returns")
+  subtitle = paste("Since inception, updated through ", x.lastDate, sep="")
+  wb = xlsx.Calendar(R=x.R, wb=wb, sheetname=sheetname, title=title, subtitle=subtitle)
+  
+  ## Drawdowns table
+  
+  ## SFM table
+  # Create a sheet for Since Inception
+  sheetname = paste(manager," SFM", sep="")
+  title = paste(manager, "SFM Regression")
+  subtitle = paste("Since inception, updated through ", x.lastDate, sep="")
+  wb = xlsx.CAPM.R(R=x.R, wb=wb, sheetname=sheetname, title=title, subtitle=subtitle)
+  
+  # Trailing n-month view of the same table
+  periods = c(60,36,12)
+  for(period in periods){
+    sheetname=paste(manager," SFM ", period,"m", sep="")
+    title = paste(manager, "SFM Regression")
+    subtitle = paste("Trailing ", period, "-month period through ", x.lastDate, sep="")
+    wb = xlsx.CAPM(R=last(x.R, period), wb=wb, sheetname=sheetname, title=title, subtitle=subtitle)
+  }
+  
+  ## AC table
+  
+  ## 
+  saveWorkbook(wb, file=paste(outputdir,"/",manager," Performance ",timestamp,".xlsx", sep=""))
+}
\ No newline at end of file



More information about the Returnanalytics-commits mailing list