Showing posts with label advent. Show all posts
Showing posts with label advent. Show all posts

Thursday, July 19, 2012

Best of Axys, R, d3.js, and HTML5

Axys, R, d3.js, and HTML5 all offer incredibly powerful tools for investment management and reporting, but they are not set up to synergistically interact to fill each other’s gaps and leverage each other’s strengths.  In my ideal scenario, Axys serves as the accounting system and performance calculator, R serves as the advanced financial/statistical engine, d3.js serves as the interactive reporting component, and HTML5 offers the user interface and ties everything together through websockets (nicely demoed here).  After working and suffering with Axys for 12 years, I am amazed that it all seems to be coming together.  I provided a bare proof of concept for Axys to d3.js in my post Axys to d3.js Error Catching and Formatting.  Now let’s extend that to R and websockets through the generously contributed R websockets package.  I have borrowed very heavily from the author's Youtube example presented in

In this proof of concept, Axys will calculate performance and send to Excel through a graph macro which creates JSON and an html page (thanks Bruce http://excelramblings.blogspot.com/).  The html page contains javascript and d3.js to produce a simple bar chart.  Now we add a button to take the JSON created by Excel and embedded in our html and send it to R.  R will produce a charts.PerformanceSummary chart and send it back as jpeg to the html page.  The html page will receive the image and replace the d3.js bar chart with the image.

Going forward I will only use R as the statistical engine and continue to rely on d3.js for the interactive reporting.  How far I go with this depends heavily on user response.  Please let me know if you would like me to continue down this path.

The list of acknowlegements is starting to get long.  I really appreciate all the fine work done by Mike Bostock on d3.js https://github.com/mbostock/d3/wiki, the dedicated authors of the R package PerformanceAnalytics http://cran.r-project.org/web/packages/PerformanceAnalytics/index.html, Brian the author of http://illposed.net/websockets.html and the example, the author of RJSONIO http://cran.r-project.org/web/packages/RJSONIO/index.html, and Bruce McPherson at http://excelramblings.blogspot.com/ for the inspirational idea.

To work through on your own, you will need the Excel file cdataset.xlsm, the Axys report perhstsp.rep, and the R code from GIST.

Wednesday, July 18, 2012

Axys to d3.js Error Catching and Formatting

I got so excited in d3.js Chart of Axys Performance that I did not test adequately.  When I added a y-axis, I realized that the axis was reversed.  All the links should now be fixed with the better code, and I also added some labels.  This is a work in progress, so please stay tuned.

To try it for yourself, please download the perhstsp.rep Axys report and a stripped down version of cDataSet.xlsm (see the full version for more http://ramblings.mcpher.com/Home/excelquirks/downloadlist) . If you would just like to see the final html output then go to http://bl.ocks.org/3106445.

Thanks to Mike Bostock http://bost.ocks.org/mike/for all his incredible work on everything, but especially d3.js. Also, thanks again to Bruce for his stimulating work with Excel.

Friday, July 13, 2012

d3.js Chart of Axys Performance

Please see Axys to d3.js Error Catching and Formatting for a much better version.

Way back in January I made some amazing (is it sad that I consider this amazing?) discoveries as I tried to push the limits with Advent Axys reporting and even integrated R with Axys in R in Axys (Impossible Dream).  Now I have accomplished what I believe to be even more exciting and potentially useful by  combining the nice Excel and d3.js work done at http://excelramblings.blogspot.com/ with 20 year old Axys technology (nicely improved by http://viabinary.com/ViaBinary/Home.html).  For those of you who know reporting in Axys, I’m sure you share my excitement.  Please let me know your thoughts.

To prove that I’m not dreaming, here is the screencast.

To try it for yourself, please download the perhstsp.rep Axys report and a stripped down version of cDataSet.xlsm (see the full version for more http://ramblings.mcpher.com/Home/excelquirks/downloadlist) .  If you would just like to see the final html output then go to http://bl.ocks.org/3106445.

Thanks to Mike Bostock http://bost.ocks.org/mike/for all his incredible work on everything, but especially d3.js.  Also, thanks again to Bruce for his stimulating work with Excel.

Tuesday, January 10, 2012

Production Quality Bullets?

Incrementalism can lead to good (in design) or bad (in politics) outcomes.  Subtle changes to reports can often yield significant benefits.  I hope I was able to achieve a positive experience with just a couple modifications to Better Bullets. To achieve this result, please use the newest excelgrf_macros.xlsm.

From TimelyPortfolio

I think the next version might take our formatting abilities a little too far, but I will show it just as another alternative.

From TimelyPortfolio

So far I have not heard any comments or suggestions.  Please send them my way.  I am aware that the instructions are fairly sparse. I hope to do a screencast very soon.

Monday, January 9, 2012

R in Axys (Impossible Dream)

It has always been a dream of mine to incorporate R graphs and analysis in an Advent Axys report.  The unbelievable work from the guys at Statconn http://rcom.univie.ac.at/ make this dream possible.  If we use the same perhstsp.rep created for Better Bullets, and then apply a little Excel macro magic excelgrf_macros.xlsm with help from RExcel and PerformanceAnalytics, we can go from

From TimelyPortfolio
From TimelyPortfolio

to

From TimelyPortfolio

I never really expected to be able to accomplish this.  Roll Tide!

Friday, January 6, 2012

Few’s Bullets in Axys Reports

Just a couple of days ago, I would have said what I am about to show was impossible in Axys reports.  However, with the very fine work from Fabrice Rimlinger http://sparklines-excel.blogspot.com/ and a little stretching of the boundaries, I have been able to get bullet performance graphs in an Axys performance report.  For more on Stephen Few’s innovative bullet charts, see http://www.perceptualedge.com/blog/?p=217 or for his books see

Just like in Axys Combo Incremental Improvement, we will run a custom report (in this case an amended Axys perhist perhstsp.rep) and use excelgrf_macros.xlsm to get our new macro sparkbullet.

From TimelyPortfolio
From TimelyPortfolio

And we get something amazing but not all that pretty.  I’ll take it though.  Please see the updated Better Bullets for an even better version of this chart.

From TimelyPortfolio

Axys and Graphic Design

I am sure most of my normal readers discovered a long time ago that I have no degree in graphic design, but that does not mean that I cannot try.  To further the proof of concept and hopefully inspire some thought, here is an initial pass at graphic design on Axys Combo Incremental Improvement.

From TimelyPortfolio

Thursday, January 5, 2012

Axys Combo Incremental Improvement

As I experiment more with Power of Sparklines in Advent Axys, I thought my first real challenge would be to get two charts with one report.  In my mind, client reporting generally contains four major sections:

    1. Performance
    2. Activity Summary
    3. Historical Value
    4. Allocation/Current Holdings

For this incremental improvement, I will work on 2 (Activity Summary) and 3 (Historical Value).  The new Excel file can be downloaded at excelgrf_macros.xlsm and our fancy new Axys report at actimval.rep.  I accept no credit for the code in the report since most comes from the standard Axys activity.rep and mval.rep.

The steps will be very similar to those taken in Power of Sparklines in Advent Axys.

First run actimval.rep in Axys reports.

From TimelyPortfolio

Then click graph

From TimelyPortfolio

which brings up the second graph dialog box where we can input the location and name of the Excel file and our new macro.

From TimelyPortfolio

And hopefully you will get something similar to this. Not pretty but certainly a step in the right direction.

From TimelyPortfolio

Currently, most of this is more proof of concept and experimentation, but by the end of all this, I foresee some mighty clean and impressive report packages.  Please comment and contribute.  I know this is pretty domain specific, so let me know if you are out there.  As far as I know the only folks professionally pursuing Axys custom reporting besides Advent are http://cssi.org/ and http://www.isitc.com/.

Tuesday, January 3, 2012

Power of Sparklines in Advent Axys

I recently discovered the amazing work of Fabrice Rimlinger at http://sparklines-excel.blogspot.com and thought of what I believe is a novel way of using the Microsoft Excel integration with Advent Axys reports to potentially achieve a dashboard style report.  This would be far more visually compelling and informative than anything else I have seen in Axys.  For this report, we will just use the standard Axys Portfolio Summary Report and then an Excel macro http://www.box.com/s/9jenb341qmv4iaoeh5uy.  Please also install the appropriate Excel add-in from http://sparklines-excel.blogspot.com.

I know the audience for this will be more limited than my general audience.  I might move these Axys report discussions to my incomplete and defunct http://axysreporting.com site.

For the R lover, I am hoping to also incorporate the unbelievable RExcel from http://rcom.univie.ac.at/.

Thursday, September 15, 2011

Reporting Good Enough to Share

Sorry to all my faithful readers for my absence recently. I started a new job at a new firm, so my blogging has moved down the priority list but only temporarily. I am still committed to documenting my thoughts, especially finance and R thoughts as discussed in Why Talk My Book? .

So far, I have used R primarily for my own use without the intention of sharing with clients <> R stats finance geeks. However, R is so powerful that I would like to leverage it for more general communication.

Our shop uses Advent Axys, which I believe is still the industry standard despite its extremely poor and inflexible reporting capabilities. At one point in a previous life, I believed some of the inflexibility could be overcome through its replang scripting, and I endured a very painful journey documenting and experimenting http://www.axysreporting.com. I now believe the best solution is to use Axys only to export its information to better, more capable reporting engines.

In this example, I amended the Axys 631 report to export performance to a .csv file. I'm concerned that sharing my amended report might violate the very aggressive IP attorneys at Advent. I will try to write from scratch at some point to share while still avoiding this ire.  Please let me know if you have already done this to save me some time and effort.  Click on the screenshot to get the .csv file from Google Docs.

image

For now, I will share a sample performance file generated from my 631 exporter, and change the fine PerformanceAnalytics charts.PerformanceSummary report.  In later posts I will add some ggplot2 charts.  I think the chart is now good enough to share with clients, but unfortunately, my code is nowhere near as clean or robust as I would like.  I combined all the code into one R script, and here is the result.  I would love input and thoughts.

The original charts.PerformanceSummary with no modifications.

From TimelyPortfolio

My new version

From TimelyPortfolio

R code (click to download from Google Docs):

#this report uses the PerformanceAnalytics charts.PerformanceSummary
#as the base for a new one-pager
#99% of the code is from the PerformanceAnalytics package
#all credit should be given to the PerformanceAnalytics team
#all errors should be assigned to me
 
 
#this is almost entirely from the PerformanceAnalytics
#chart.CumReturns function
#I only change to label the endpoints
na.skip <- function (x, FUN=NULL, ...) # maybe add a trim capability?
{ # @author Brian Peterson
 
# DESCRIPTION:
 
# Time series data often contains NA's, either due to missing days,
# noncontiguous series, or merging multiple series,
#
# Some Calulcations, such as return calculations, require data that
# looks like a vector, and needs the output of na.omit
#
# It is often convenient to apply these vector-like functions, but
# you still need to keep track of the structure of the oridginal data.
 
# Inputs
# x the time series to apply FUN too
# FUN function to apply
# ... any additonal parameters to FUN
 
# Outputs:
# An xts time series that has the same index and NA's as the data
# passed in, after applying FUN
 
nx <- na.omit(x)
fx <- FUN(nx, ... = ...)
if (is.vector(fx)) {
result <- .xts(fx, .index(x), .indexCLASS = indexClass(x))
}
else {
result <- merge(fx, .xts(, .index(x)))
}
return(result)
}
 
 
 
chart.CumReturnsX <-
function (R, wealth.index = FALSE, geometric = TRUE, legend.loc = NULL, colorset = (1:12), begin = c("first","axis"), ...)
{ # @author Peter Carl
 
# DESCRIPTION:
# Cumulates the returns given and draws a line graph of the results as
# a cumulative return or a "wealth index".
 
# Inputs:
# R: a matrix, data frame, or timeSeries of returns
# wealth.index: if true, shows the "value of $1", starting the cumulation
# of returns at 1 rather than zero
# legend.loc: use this to locate the legend, e.g., "topright"
# colorset: use the name of any of the palattes above
# method: "none"
 
# Outputs:
# A timeseries line chart of the cumulative return series
 
# FUNCTION:
 
# Transform input data to a matrix
begin = begin[1]
x = checkData(R)
 
# Get dimensions and labels
columns = ncol(x)
columnnames = colnames(x)
 
# Calculate the cumulative return
one = 0
if(!wealth.index)
one = 1
 
##find the longest column, calc cum returns and use it for starting values
 
if(begin == "first") {
length.column.one = length(x[,1])
# find the row number of the last NA in the first column
start.row = 1
start.index = 0
while(is.na(x[start.row,1])){
start.row = start.row + 1
}
x = x[start.row:length.column.one,]
if(geometric)
reference.index = na.skip(x[,1],FUN=function(x) {cumprod(1+x)})
else
reference.index = na.skip(x[,1],FUN=function(x) {cumsum(x)})
}
for(column in 1:columns) {
if(begin == "axis") {
start.index = FALSE
} else {
# find the row number of the last NA in the target column
start.row = 1
while(is.na(x[start.row,column])){
start.row = start.row + 1
}
start.index=ifelse(start.row > 1,TRUE,FALSE)
}
if(start.index){
# we need to "pin" the beginning of the shorter series to the (start date - 1 period)
# value of the reference index while preserving NA's in the shorter series
if(geometric)
z = na.skip(x[,column],FUN = function(x,index=reference.index[(start.row - 1)]) {rbind(index,1+x)})
else
z = na.skip(x[,column],FUN = function(x,index=reference.index[(start.row - 1)]) {rbind(1+index,1+x)})
} else {
z = 1+x[,column]
}
column.Return.cumulative = na.skip(z,FUN = function(x, one, geometric) {if(geometric) cumprod(x)-one else (1-one) + cumsum(x-1)},one=one, geometric=geometric)
if(column == 1)
Return.cumulative = column.Return.cumulative
else
Return.cumulative = merge(Return.cumulative,column.Return.cumulative)
}
if(columns == 1)
Return.cumulative = as.xts(Return.cumulative)
colnames(Return.cumulative) = columnnames
 
# Chart the cumulative returns series
chart.TimeSeries(Return.cumulative, col = colorset, legend.loc = legend.loc, ...)
for (i in 1:NCOL(Return.cumulative)) {
text(x=NROW(Return.cumulative),
y=coredata(Return.cumulative)[NROW(Return.cumulative),i],
sprintf("%1.0f%%",round(coredata(Return.cumulative)[NROW(Return.cumulative),i],2)*100),
adj = c(0, 0.5),lwd=0.5,col=colorset[i])
}
}
 
#this function shows bar plot side-by-side comparisons for rolling annualized
#returns
#please proceed with caution as function is not robust
#and does not perform adequate error checking
chart.SideBar <- function (w, auto.grid = TRUE, xaxis = TRUE, yaxis = TRUE, yaxis.right = FALSE,
type = "l", lty = 1, lwd = 2, main = NULL, ylab = "Annualized Returns", xlab = NULL,
xlim = NULL, ylim = NULL, element.color = "darkgray", event.lines = NULL,
event.labels = NULL, period.areas = NULL, event.color = "darkgray",
period.color = "aliceblue", colorset = (1:12), pch = (1:12),
legend.loc = NULL, cex.axis = 0.8, cex.legend = 0.8,
cex.lab = 1, cex.labels = 0.8, cex.main = 1, major.ticks = "auto",
minor.ticks = TRUE, grid.color = "lightgray", grid.lty = "dotted",
xaxis.labels = NULL, ...)
{
barplot(w, beside=TRUE, col = colorset[1:NROW(w)],
xlab = xlab, ylab = ylab, axes = FALSE,
ylim = c(min(0,min(w)),max(w)+0.05),...)
 
if (auto.grid) {
abline(v=0, col = element.color, lty = grid.lty)
grid(NA, NULL, col = grid.color)
}
abline(h = 0, col = grid.color)
 
axis(2, cex.axis = cex.axis, col = element.color)
title(ylab = ylab, cex = cex.lab)
 
 
 
if (!is.null(legend.loc)) {
legend(legend.loc, inset = 0.02, text.col = colorset,
col = colorset, cex = cex.legend, border.col = grid.color,
lty = lty, lwd = 2, bg = "white", legend = rownames(w))
}
 
box(col = element.color)
}
 
 
charts.PerformanceSummaryX <-
function (R, Rf = 0, main = NULL, submain=NULL, geometric=TRUE, methods = "none", width = 0, event.labels = NULL, ylog = FALSE, wealth.index = FALSE, gap = 12, begin=c("first","axis"), legend.loc="bottomright", p=0.95, maxdraw = TRUE,...)
{ # @author Peter Carl
 
# DESCRIPTION:
# A wrapper to create a wealth index chart, bars for monthly peRformance,
# and underwater chart for drawdown.
 
# Inputs:
# R: a matrix, data frame, or timeSeries, usually a set of monthly returns.
# The first column is assumed to be the returns of interest, the next
# columns are assumed to be relevant benchmarks for comparison.
# Rf: this is the risk free rate. Remember to set this to the same
# periodicity as the data being passed in.
# method: Used to select the risk parameter to use in the chart.BarVaR. May
# be any of:
# modVaR - uses CF modified VaR
# VaR - uses traditional Value at Risk
# StdDev - monthly standard deviation of trailing 12 month returns
#
 
# Outputs:
# A stack of three related timeseries line charts
 
# FUNCTION:
begin = begin[1]
x = checkData(R)
colnames = colnames(x)
ncols = ncol(x)
 
# This repeats a bit of code from chart.CumReturns, but it's intended
# to align the start dates of all three charts. Basically, it assumes
# that the first column in the list is the column of interest, and
# starts everything from that start date
 
length.column.one = length(x[,1])
# find the row number of the last NA in the first column
start.row = 1
start.index = 0
while(is.na(x[start.row,1])){
start.row = start.row + 1
}
x = x[start.row:length.column.one,]
 
if(ncols > 1)
legend.loc = legend.loc
else
legend.loc = NULL
 
if(is.null(main))
main = paste(colnames[1],"Performance", sep=" ")
 
if(ylog)
wealth.index = TRUE
 
op <- par(no.readonly=TRUE)
 
# First, we lay out the graphic as a three row, one column format
# plot.new()
layout(matrix(c(1,2,3)),height=c(2,1,1.3),width=1)
# to see the resulting layout, use layout.show(3)
 
# mar: a numerical vector of the form c(bottom, left, top, right) which
# gives the number of lines of margin to be specified on the four sides
# of the plot. The default is c(5, 4, 4, 2) + 0.1
 
# The first row is the cumulative returns line plot
par(oma=c(2,2,4,2))
par(mar=c(1,6,8,4))
chart.CumReturnsX(x, main = "", xaxis = FALSE, legend.loc = legend.loc, cex.legend = 1, event.labels = event.labels, ylog = ylog, wealth.index = wealth.index, begin = begin, geometric = geometric, ylab="Cumulative Return",...)
 
# title(main=main, sub=submain, cex.main=2, cex.sub=1.5, adj=0, outer =FALSE)
mtext(text=main, line = -2, outer = TRUE, adj = 0.1, cex=2)
mtext(text=submain, line = -4, outer = TRUE, adj = 0.075, cex=1.5)
 
 
 
# The second row is the monthly returns bar plot
# par(mar=c(1,4,0,2))
 
freq = periodicity(x)
 
switch(freq$scale,
seconds = { date.label = "Second"},
minute = { date.label = "Minute"},
hourly = {date.label = "Hourly"},
daily = {date.label = "Daily"},
weekly = {date.label = "Weekly"},
monthly = {date.label = "Monthly"},
quarterly = {date.label = "Quarterly"},
yearly = {date.label = "Annual"}
)
 
# chart.BarVaR(x, main = "", xaxis = FALSE, width = width, ylab = paste(date.label,"Return"), methods = methods, event.labels = NULL, ylog=FALSE, gap = gap, p=p, ...)
 
# The third row is the underwater plot
par(mar=c(3,6,0,4))
chart.Drawdown(x, geometric = geometric, main = "", xlab=NA, ylab = "Drawdown", event.labels = NULL, ylog=FALSE, ...)
if (maxdraw) {
abline(h=-0.1,col="tomato3",lwd=3,lty="dashed")
text(x=2,y=-0.1,"maximum acceptable drawdown",adj = c(0, 0.5))
}
 
# If we wanted to add a fourth row with the table of monthly returns
# par(mar=c(0,0,0,0))
# textplot(table.Returns(as.matrix(R)),cex=.7,cmar=1.5,rmar=0.5,halign="center", valign="center")
 
par(mar=c(6,6,0,4))
 
returnTable <- returnTable <- table.TrailingPeriods(x,
periods=c(12,24,36,NROW(x)),
FUNCS="Return.annualized")
rownames(returnTable) <- c(paste(c(1:3)," Year",sep=""),"Since Inception")[1:NROW(returnTable)]
 
chart.SideBar(t(as.matrix(returnTable)),...)
 
par(op)
 
}
 
 
 
 
#now let's use the amended report to look at performance
require(quantmod)
require(PerformanceAnalytics)
 
clientPerf <- read.csv("clientperf.csv",stringsAsFactors=FALSE)
clientPerf[,2:NCOL(clientPerf)] <- lapply(clientPerf[,2:NCOL(clientPerf)],as.numeric)
clientPerf <- as.xts(clientPerf[,2:NCOL(clientPerf)]/100,
order.by = as.Date(clientPerf[,1],format="%m-%d-%y"))
colnames(clientPerf) <- c("Client","BarclaysAgg","SP500")
 
jpeg(filename="performance summary.jpg",quality=100,width=6, height = 7, units="in",res=96)
charts.PerformanceSummary(clientPerf)
dev.off()
 
jpeg(filename="performance one-pager.jpg",quality=100,width=6, height = 7, units="in",res=96)
charts.PerformanceSummaryX(clientPerf,main="Client Performance",
submain=paste("Since Inception - ",format(seq(index(clientPerf)[1], length=2, by="-1 months")[2],"%B %Y"),sep=""),legend.loc="bottomright",maxdraw=FALSE,
colorset=c("cadetblue4","darkgray","bisque3"),lwd=c(3,2,2))
dev.off()

Created by Pretty R at inside-R.org