<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/visualizing-portfolio-volatility/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/07/21/visualizing-portfolio-volatility/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <br>
<br>


<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>



</p>
<p>This is the third post in our series on portfolio volatility, variance and standard deviation. If you want to start at the beginning with calculating portfolio volatility, have a look at the first post <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/12/introduction-to-volatility/" target="_blank">here – Intro to Volatility</a>. The second post on calculating rolling standard deviations is <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/21/visualizing-portfolio-volatility/url" target="_blank">here: Intro to Rolling Volatility</a>.</p>
<p>Today we will visualize rolling standard deviations with <code>highcharter</code> using two objects from that second post.</p>
<p>The charts, which are the fun payoff after all of the equations and functions that we have ground out in the previous posts, should highlight any unusual occurrences or volatility spikes/dips that we might want to investigate.</p>
<p>First, load the <code>.RDat</code> file saved from our previous Notebook (or you can run the scripts from the previous posts).</p>
<pre><code>load('rolling-sd.RDat')</code></pre>
<p>We now have 2 objects in our Global Environment – <code>spy_rolling_sd</code> – an <code>xts</code> object of rolling SPY standard deviations – <code>roll_portfolio_result</code> – an <code>xts</code> object of rolling portfolio standard deviations. Because both of those are <code>xts</code> objects, we can pass them straight to <code>highcharter</code> with the <code>hc_add_series()</code> function and set a name and a color with the <code>name</code> and <code>color</code> arguments. Nothing too complicated here – we did the hard work in our previous Notebooks.</p>
<pre><code>highchart(type = &quot;stock&quot;) %>%
  hc_title(text = &quot;SPY v. Portfolio Rolling Volatility&quot;) %>%
  hc_add_series(spy_rolling_sd, name = &quot;SPY Volatility&quot;, color = &quot;blue&quot;) %>%
  hc_add_series(roll_portfolio_result, name = &quot;Port Volatility&quot;, color = &quot;green&quot;) %>%
  hc_navigator(enabled = FALSE) %>% 
  hc_scrollbar(enabled = FALSE)</code></pre>
<div></div>
<p></p>
<p>It is interesting to note that from late April 2016 to late October 2016, SPY’s rolling standard deviation dipped below that of the diversified portfolio. The portfolio volatility was plunging at the same time, but SPY’s was falling faster. What happened over the 6 preceding months to explain this?</p>
<p>Maybe we should add a flag to highlight this event. We can also add flags for the maximum SPY volatility, maximum and minimum portfolio rolling volatility and might as well include a line for the mean rolling volatility of SPY to practice adding horizontal lines.</p>
<p>We will use two methods for adding flags. First, we’ll hard code the date for the flag as “2016-04-29” using the date when rolling SPY volatility dipped below the portfolio. Second, we’ll set a flag with the date<br>
<code>as.Date(index(roll_portfolio_result[which.max(roll_portfolio_result)]),format = &quot;%Y-%m-%d&quot;)</code> which looks like a convoluted mess but is adding a date for whenever the rolling portfolio standard deviation hit its maximum.</p>
<p>This is a bit more ‘dynamic’ because we can change our assets but keep this code the same and it will find the date with the maximum rolling standard deviation. Our first flag is not dynamic in the sense that it is specific to the comparison between SPY and this exact portfolio.</p>
<pre><code>spy_important_date &lt;- as.Date(c(&quot;2016-04-29&quot;), format = &quot;%Y-%m-%d&quot;)

port_max_date &lt;- as.Date(index(roll_portfolio_result[which.max(roll_portfolio_result)]),
                         format = &quot;%Y-%m-%d&quot;)
port_min_date &lt;- as.Date(index(roll_portfolio_result[which.min(roll_portfolio_result)]),
                         format = &quot;%Y-%m-%d&quot;)
spy_max_date &lt;- as.Date(index(spy_rolling_sd[which.max(spy_rolling_sd)]),
                         format = &quot;%Y-%m-%d&quot;)


highchart(type = &quot;stock&quot;) %>%
  hc_title(text = &quot;SPY v. Portfolio Rolling Volatility&quot;) %>%
  hc_add_series(spy_rolling_sd, name = &quot;SPY Volatility&quot;, color = &quot;blue&quot;, id = &quot;SPY&quot;) %>%
  hc_add_series(roll_portfolio_result, name = &quot;Portf Volatility&quot;, color = &quot;green&quot;, id = &quot;Port&quot;) %>%
  hc_add_series_flags(spy_important_date,
                      title = c(&quot;SPY Vol Dips&quot;), 
                      text = c(&quot;SPY rolling sd dips below portfolio.&quot;),
                      id = &quot;SPY&quot;) %>%
  hc_add_series_flags(spy_max_date,
                      title = c(&quot;SPY Max &quot;), 
                      text = c(&quot;SPY max rolling volatility.&quot;),
                      id = &quot;SPY&quot;) %>%
   hc_add_series_flags(port_max_date,
                      title = c(&quot;Portf Max&quot;), 
                      text = c(&quot;Portfolio maximum rolling volatility.&quot;),
                      id = &quot;Port&quot;) %>%
  hc_add_series_flags(port_min_date,
                      title = c(&quot;Portf Min&quot;), 
                      text = c(&quot;Portfolio min rolling volatility.&quot;),
                      id = &quot;Port&quot;) %>%
  hc_yAxis(title = list(text = &quot;Mean SPY rolling Vol&quot;),
           showFirstLabel = FALSE,
           showLastLabel = FALSE,
           plotLines = list(
             list(value = mean(spy_rolling_sd), color = &quot;#2b908f&quot;, width = 2)))  %>% 
  hc_navigator(enabled = FALSE) %>% 
  hc_scrollbar(enabled = FALSE)</code></pre>
<div></div>
<p></p>
<p>Hover on the flags and you can see the text we added for explanation.</p>
<p>It’s remarkable how rolling volatility has absolutely plunged since early-to-mid 2016. Since August of 2016, both the portfolio and SPY rolling standard deviations have been well below the SPY mean.</p>
<p>Thanks for sticking with this three-part introduction to volatility. Next time, we’ll port our work to Shiny and play with different assets and allocations.</p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rviews.rstudio.com/2017/07/21/visualizing-portfolio-volatility/"> R Views</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/qPmUn8JMcZI" height="1"></a></div>