Let’s do an easy experiment. Lets caluclate the 25-day rolling volatility of the S&P 500 from 2007 onwards.
1-Get the data:
2-Run the volatility function from the package TTR (comes along with quantmod):
#n=25 means we want 25 day rolling volatility. N=252 means we are taking a year as 252 days. calc=’close’ indicates that we want to calculate Close to Close volatility. If you look at the help page for the volatility function, there are several different calc=” parameters available.
3-We can now plot this using chartSeries(vol). Notice the huge spike in volatility in 2008.
1-chart.Histogram(returnVector, methods=c(“add.centered”, “add.rug”, “add.risk”)) #This gives the returns distributed in a Histogram,compared to the normal distribution and with the VaR provided.
2-charts.PerformanceSummary(returnVector) #Very useful plot showing cumulative return, daily (or periodic) return and drawdown. Takes as input artihmetic returns under default settings. Change to log returns by setting geometric=F in the function call.
3-chartSeries(priceVector) #I use this more for charting price movements than returns. This is a versatile and powerful function and can be used for all sorts of technical analysis. You can add a various technical indicators using the ‘TA=…’ parameter entry. Read up on it by entering ‘?chartSeries’