<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/reproducible-finance-the-book-and-a-discount-for-our-readers/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/10/29/reproducible-finance-the-book/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>I’m thrilled to announce the release of my new book <a rel="nofollow" href="https://www.crcpress.com/Reproducible-Finance-with-R-Code-Flows-and-Shiny-Apps-for-Portfolio-Analysis/Regenstein-Jr/p/book/9781138484030" target="_blank">Reproducible Finance with R: Code Flows and Shiny Apps for Portfolio Analysis</a>, which originated as a series of R Views posts in this space. The <a rel="nofollow" href="https://rviews.rstudio.com/2016/11/09/reproducible-finance-with-r-the-sharpe-ratio/" target="_blank">first post</a> was written way back in November of 2016 – thanks to all the readers who have supported us along the way!</p>
<p>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.</p>
<p>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.</p>
<p>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 <em>or</em> in finance, but the most important thing is a desire to learn about the landscape of R code and finance packages.</p>
<p>An overarching goal of the book is to introduce the three major R paradigms for portfolio analysis: <code>xts</code>, the <code>tidyverse</code>, and <code>tidyquant</code>. As a result, we will frequently run the same analysis using three different code flows.</p>
<p>If that ‘three-universe’ structure seems a bit unclear, have a quick look back at <a rel="nofollow" href="https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/" target="_blank">this post on skewness</a> and <a rel="nofollow" href="https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/" target="_blank">this post on kurtosis</a>, and you’ll notice that we solve the same task and get the same result with different code paths.</p>
<p>For example, if we had portfolio returns saved in a tibble object called <code>portfolio_returns_tq_rebalanced_monthly</code>, and an <code>xts</code> object called <code>portfolio_returns_xts_rebalanced_monthly</code>, and our goal was to find the Sharpe Ratio of portfolio returns, we would start in the <code>xts</code> world with <code>SharpeRatio()</code> from the <code>PerformanceAnalytics</code> package.</p>
<pre><code># define risk free rate variable
rfr &lt;- .0003

sharpe_xts &lt;- 
  SharpeRatio(portfolio_returns_xts_rebalanced_monthly, 
              Rf = rfr,
              FUN = &quot;StdDev&quot;) %>% 
  `colnames&lt;-`(&quot;sharpe_xts&quot;)

sharpe_xts</code></pre>
<pre><code>##                               sharpe_xts
## StdDev Sharpe (Rf=0%, p=95%):  0.2748752</code></pre>
<p>We next would use the tidyverse and run our calculations in a piped flow:</p>
<pre><code>sharpe_tidyverse_byhand &lt;- 
  portfolio_returns_tq_rebalanced_monthly %>% 
  summarise(sharpe_dplyr = mean(returns - rfr)/
              sd(returns - rfr))

sharpe_tidyverse_byhand</code></pre>
<pre><code>## # A tibble: 1 x 1
##   sharpe_dplyr
##          </code></pre>