Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Endterm Submission for GSoC2016 - plot.xts for Performance and Risk Project

plot.xts for Performance and Risk

ChuuHsiang Hung - National Chiao Tung University, Institute of Business and management - Taiwan

naturalsmen@gmail.com

Joshua M. Ulrich - DV Trading, US

josh.m.ulrich@gmail.com

Introduction

The goal of this document is to be a reference for people who want to understand the changes made to quantmod and PerformanceAnalytics fork (not commits page). Base R charting functions used in both packages were replaced by xts::plot.xts in Josh's xts package, developed version 0.10-0, to improve financial data visualization and make future maintenance easier. The charting functionality in blotter and quantstrat packages are based on chart_Series function in quantmod. New quantmod and PerformanceAnalytics are backwards compatible with interfaces of other functions in the respective package and no error or warning found in tests by R CMD Check.

Implementation

quantmod

We retain the charting functionality in quantmod but replace the inner base R charting functions by xts::plot.xts. Thus the charts created by users should be looked like the same as in the old version. chartSeries is refactored to be a wrapper for chart_Series which is now a wrapper for xts::plot.xts as well. So S4 method used by chartSeries and its relevant functions are deprecate and S4 objects chob and chobTA are removed. Instead, lchob object of replot_xts class will be returned. Except for refactoring, a chartTheme demonstration for managing chart colors is added. Here we provide a quick example:

# Install developed version
library(devtools)
install_github('naturalsmen/quantmod', ref = 'design')
install_github('joshuaulrich/xts')
library(quantmod)
# see demo for chartSeries and chartTheme
demo(chartSeries)
demo(chartTheme)
# Load example data from TTR
data(ttrc, package="TTR")
# ttrc is from 1985 to 2006
# we only use 5 years here for demonstration
x <- xts(ttrc[,-1], ttrc[,1])["1997:2001"]
# create an object of replot_xts class
cs <- chartSeries(x, TA=c("addVo();addSMA();addMACD();addRSI()"), TAsep=";")
# create candlesticks chart
cs
zoomChart("2002")
zoomChart("")
zooom() # choose desired periods interactively
dropTA("RSI") # remove RSI

Please see vignette quantmod-charts for more information.

Link to commits

https://github.com/naturalsmen/quantmod/commits/design?author=naturalsmen

TO DO

  1. Since log.scale is not supported in xts::plot.xts, special handling should be used to implement it.
  2. setDefaults, setTA and unsetTA still use S4 method and they may be refactored or deprecated in the future.
  3. chartTA functions outside of the addTA functions are not removed, should they?
  4. y-axis value only changes in the frames of even numbers when zoomChart or zooom are called.
  5. We implement the subset functionality by replacing plot_object$Env$xsubset directly but an error occurrs in xts:::chart.lines function. Now a new chart.lines is used in reChart and zoom_Chart. See joshuaulrich/xts#146 for more information.

PerformanceAnalytics

Since all the time series charting functions in PerformanceAnalytics are based on chart.TimeSeries, changes we made to the orther charting functions aim to make them backwards compatible with blotter and quantstrat and coordinate with the refactored chart.TimeSeries. Main changes here are making functions such as charts.PerformaceSummary and charts.BarVaR to be layout-free. Panel functionality of xts::plot.xts is used to create multiple series charts. In addition, those functions are extended to use invisible so that a well-documented data object with well-formatted calculated results could be passed to other graphics packages or frameworks (e.g., dygraphs, ggviz, etc.), including interactive frameworks. Users can also access the plot object by xts:::current.xts_chob().

library(devtools)
install_github('naturalsmen/PerformanceAnalytics', ref = 'fix_time_series_chart_bug')
library(PerformanceAnalytics)
# use example data in PerformanceAnalytics
data(managers)
chart.TimeSeries(managers[,1])
# return value at risk values and create charts
VaR.list <- charts.BarVaR(managers[,1:3], methods = c("ModifiedVaR", "GaussianVaR", "StdDev"))
VaR.list
# show cumulative return, multiple-day return and drawdown value and charts
performance <- charts.PerformanceSummary(managers[,1))
performance
# users can access plot object by xts:::current.xts_chob()
p <- xts:::current.xts_chob()
names(p)
p

Link to commits

https://github.com/naturalsmen/PerformanceAnalytics/commits/fix_time_series_chart_bug?author=naturalsmen

  • Some early commits have been merged to braverock/PerformanceAnalytics. This branch includes them and new commits of fixes for the charting functions. Latest commit returns an object with well-calculated results.

TO DO

  1. Dynamic graph feature planned to be added at the beginning has not been implemented. It should start with full discussion on which package will be used.
  2. Since the plot object of replot_xts class needs to be returned or printed or otherwise the chart can't be viewed, we use environmentName(parent.frame()) to determine if the plot object should be printed. For example, charts.BarVaR creates multiple bar charts and adds different VaR values to the charts by calling chart.BarVaR in the loop. The chart should be printed after the loop ends. There may be a better way to handle this issue.

Conclusion

xts::plot.xts of xts_0.10-0 povides a very convenient charting engine for users to manage time series chart through panels. It is also more flexible for users to create their own custom functions for data exploration and should make future maintenance easier since those packages have been integrated. In PerformanceAnalytics, some functions are extended to use invisible to return an object with well-formatted calculated results that could be passed to other graphics packages or frameworks (e.g., dygraphs, ggviz, etc.), including interactive frameworks.

Acknowledgement

I'd like to thank all the community members in the GSoC-xtsExtra mailing list for their help and advice when I ran into problems during the development. Especially for Josh, thank you so much for spending so much time checking up on me and direct me when I spent time working on things outside of the project. Also very appreciate your quick reply to my questions in real-time via hangouts even if you are very busy on your full-time job. Thank Brian and Ross for your instructions to the workflow of making commits and pull requests and helpful comments to my work. Finally, thank Google Summer of Code faculty for funding this program and answering our questions quickly, giving me great opportunity and experience to make contribution to my favorite language.

References

Jeffrey A. Ryan and Joshua M. Ulrich. xts: eXtensible Time Series. [ONLINE] Available at: http://joshuaulrich.github.io/xts/index.html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment