I’m thrilled to announce the release of my new book Reproducible Finance with R: Code Flows and Shiny Apps for Portfolio Analysis, which originated as a series of R Views posts in this space. The first post was written way back in November of 2016 - thanks to all the readers who have supported us along the way!
If you are familiar with the R Views posts, then you probably have a pretty good sense for the book’s style, prose, and code approach, but I’d like to add a few quick words of background.
The book’s practical motivations are: (1) to introduce R to finance professionals, or aspiring finance professionals, who wish to move beyond Excel for their quantitative work, and (2) to introduce various finance coding paradigms to R coders.
The softer motivation is to demonstrate and emphasize readable, reusable, and reproducible R code, data visualizations, and Shiny dashboards. It will be very helpful to have some background in the R programming language or in finance, but the most important thing is a desire to learn about the landscape of R code and finance packages.
An overarching goal of the book is to introduce the three major R paradigms for portfolio analysis: xts, the tidyverse, and tidyquant. As a result, we will frequently run the same analysis using three different code flows.
If that ‘three-universe’ structure seems a bit unclear, have a quick look back at this post on skewness and this post on kurtosis, and you’ll notice that we solve the same task and get the same result with different code paths.
For example, if we had portfolio returns saved in a tibble object called portfolio_returns_tq_rebalanced_monthly, and an xts object called portfolio_returns_xts_rebalanced_monthly, and our goal was to find the Sharpe Ratio of portfolio returns, we would start in the xts world with SharpeRatio() from the PerformanceAnalytics package.
# define risk free rate variable
rfr %
na.omit() %__% summarise(stand_dev = sd(returns),
sharpe = mean(returns - rfr)/
sd(returns - rfr)) %__% add_row(asset = &quot;Portfolio&quot;,
stand_dev =
portfolio_sd_xts_builtin[1],
sharpe =
sharpe_tq$sharpe_tq) %__%
ggplot(aes(x = stand_dev,
y = sharpe,
color = asset)) +
geom_point(size = 2) +
geom_text(
aes(x =
sd(portfolio_returns_tq_rebalanced_monthly$returns),
y =
sharpe_tq$sharpe_tq + .02,
label = &quot;Portfolio&quot;)) +
ylab(&quot;Sharpe Ratio&quot;) +
xlab(&quot;standard deviation&quot;) +
ggtitle(&quot;Sharpe Ratio versus Standard Deviation&quot;) +
# The next line centers the title
theme_update(plot.title = element_text(hjust = 0.5))
Figure 1: Sharpe versus Standard Deviation
Finally, we are ready to calculate and visualize the Sharpe Ratio of a custom portfolio with Shiny and a flexdashboard, like the one found here.
As in the above example, most tasks in the book end with data visualization and then Shiny (a few early readers have commented with happy surprise that all the charts and code are in full color in the book - thanks to CRC press for making that happen!). Data visualization and Shiny are heavily emphasized - more so than in other finance books - and that might seem unusual. After all, every day we hear about how the financial world is becoming more quantitatively driven as firms race towards faster, more powerful algorithms. Why emphasize good ol’ data visualization? I believe, and have observed first-hand, that the ability to communicate and tell the story of data in a compelling way is only going to become more crucial as the financial world becomes more complex. Investors, limited partners, portfolio managers, clients, risk managers - they might not want to read our code or see our data, but we still need to communicate to them the value of our work. Data visualization and Shiny dashboards are a great way to do that. By book’s end, a reader will have built a collection of live, functioning flexdashboards that can be the foundation for more complex apps in the future.
If you’ve read this far, good news! Between now and December 31, 2018, there’s a 20% discount on the book being run at CRC, and if you don’t see it applied, readers can use discount code SS120 on the CRC website. The book is also available on Amazon as Kindle or paperback (but there’s only than 10 copies left as of right now).
Thanks so much for reading, and happy coding!