2018 Volatility Recap

January 6, 2019
By

(This article was first published on R – Quintuitive, and kindly contributed to R-bloggers)

2018 brought more volatility to the markets, which so far has spilled into 2019. Let’s take a look at the long term volatility history picture using the Dow Jones Industrial Average:

Indeed, 2018 was the most volatile year since 2011. Relatively speaking however, the volatility is on the low end for a bear market, which I believe started in late December.

The above chart was produced using the following R code:

library(quantmod)
library(ggplot2)
library(ggthemes)
library(grid)

dji.annual.volatility = function(dji, year) {
   dates = paste("/", as.character(year), sep="")
   dji = na.exclude(dji[dates])
   djiVol = aggregate(dji, as.numeric(format(index(dji), "%Y")),
               function(ss) coredata(tail(TTR:::volatility(
               ss,
               n=NROW(ss),
               calc="close"), 1)))
   
   xx = ecdf(as.vector(djiVol))(as.numeric(tail(djiVol,1)))
   print(xx)
   absRets = na.exclude(abs(ROC(dji[dates], type="discrete")))
   yy = as.numeric(format(index(absRets), "%Y"))
   zz = aggregate(absRets, yy, function(ss) tail(cumprod(1+ss),1))
   print(as.vector(tail(zz,1)))
   
   df = cbind(as.data.frame(index(djiVol)), coredata(djiVol))
   colnames(df) = c("Year", "Volatility")
   
   gg = qplot(x=Year, y=Volatility, data=df, geom="line", colour=Volatility, xlab="Year", ylab="Volatility")
   gg = gg + theme_solarized(base_size=16, base_family="verdana", light=TRUE)

   return(list(plot=gg, dji=dji, dji.vol=djiVol, crystal.ball=zz, df=df))
}

The post 2018 Volatility Recap appeared first on Quintuitive.

To leave a comment for the author, please follow the link and comment on their blog: R – Quintuitive.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)