Working with xts and quantmod



Similar documents
Chapter 1. Return Calculations. 1.1 The Time Value of Money Future value, present value and simple interest.

AT&T Global Network Client for Windows Product Support Matrix January 29, 2015

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

COMPARISON OF FIXED & VARIABLE RATES (25 YEARS) CHARTERED BANK ADMINISTERED INTEREST RATES - PRIME BUSINESS*

Working with Financial Time Series Data in R

Analysis One Code Desc. Transaction Amount. Fiscal Period

Case 2:08-cv ABC-E Document 1-4 Filed 04/15/2008 Page 1 of 138. Exhibit 8

Are High Frequency Traders Prudent and Temperate?

Enhanced Vessel Traffic Management System Booking Slots Available and Vessels Booked per Day From 12-JAN-2016 To 30-JUN-2017

Comparing share-price performance of a stock

Financial Data Access with SQL, Excel & VBA

High-Frequency Data Modelling with R software

Ashley Institute of Training Schedule of VET Tuition Fees 2015

Package FinancialInstrument

Human Resources Management System Pay Entry Calendar

CALL VOLUME FORECASTING FOR SERVICE DESKS

CAFIS REPORT

1. Introduction. 2. User Instructions. 2.1 Set-up

Based on Chapter 11, Excel 2007 Dashboards & Reports (Alexander) and Create Dynamic Charts in Microsoft Office Excel 2007 and Beyond (Scheck)

CENTERPOINT ENERGY TEXARKANA SERVICE AREA GAS SUPPLY RATE (GSR) JULY Small Commercial Service (SCS-1) GSR

Time Series Database Interface: R MySQL (TSMySQL)

R-evolution in Time Series Analysis Software Applied on R-omanian Capital Market

Detailed guidance for employers

Assignment 4 CPSC 217 L02 Purpose. Important Note. Data visualization

Department of Public Welfare (DPW)

Using INZight for Time series analysis. A step-by-step guide.

Computing & Telecommunications Services Monthly Report March 2015

BCOE Payroll Calendar. Monday Tuesday Wednesday Thursday Friday Jun Jul Full Force Calc

Predicting Credit Score Calibrations through Economic Events

Ways We Use Integers. Negative Numbers in Bar Graphs

Infographics in the Classroom: Using Data Visualization to Engage in Scientific Practices

Accident & Emergency Department Clinical Quality Indicators

Package quantmod. R topics documented: July 24, Type Package Title Quantitative Financial Modelling Framework Version Date

Consumer ID Theft Total Costs

Resource Management Spreadsheet Capabilities. Stuart Dixon Resource Manager

NATIONAL CREDIT UNION SHARE INSURANCE FUND

Need to know finance

Package TimeWarp. R topics documented: April 1, 2015

Industry Environment and Concepts for Forecasting 1

The magic of trading money management OR How to covert a small account (or a large one) into a money spinner!

OMBU ENTERPRISES, LLC. Process Metrics. 3 Forest Ave. Swanzey, NH Phone: Fax: OmbuEnterprises@msn.

2015 Examination dates

USING TIME SERIES CHARTS TO ANALYZE FINANCIAL DATA (Presented at 2002 Annual Quality Conference)

Business Idea Development Product production Services. Development Project. Software project management

Basic Project Management & Planning

JetBlue Airways Stock Price Analysis and Prediction

Russell Rhoads, CFA Instructor The Options Institute Chicago Board Options Exchange, Incorporated. All rights reserved.

NewEuroMTS Overview. October 2005

US Health and Life Insurance Company Overview

Seven Tips for Building a Great Dashboard

Package fimport. February 19, 2015

Deep Security/Intrusion Defense Firewall - IDS/IPS Coverage Statistics and Comparison

2015 Settlement Calendar for ASX Cash Market Products ¹ Published by ASX Settlement Pty Limited A.B.N

Economic indicators dashboard

Interest Rates. Countrywide Building Society. Savings Growth Data Sheet. Gross (% per annum)

In-Memory Data Management for Enterprise Applications

Deep Security Intrusion Detection & Prevention (IDS/IPS) Coverage Statistics and Comparison

July 22, 2010 Page 1 of 5

WEATHERHEAD EXECUTIVE EDUCATION COURSE CATALOG

Business Plan Example. 31 July 2020

Volatility Tracker: No Surprises in Gold

Choosing a Cell Phone Plan-Verizon

P/T 2B: 2 nd Half of Term (8 weeks) Start: 25-AUG-2014 End: 19-OCT-2014 Start: 20-OCT-2014 End: 14-DEC-2014

P/T 2B: 2 nd Half of Term (8 weeks) Start: 26-AUG-2013 End: 20-OCT-2013 Start: 21-OCT-2013 End: 15-DEC-2013

Domain Name Abuse Detection. Liming Wang

P/T 2B: 2 nd Half of Term (8 weeks) Start: 24-AUG-2015 End: 18-OCT-2015 Start: 19-OCT-2015 End: 13-DEC-2015

ITD Help Desk Traffic Report May 2002

College Tuition: Data mining and analysis

Time Management II. June 5, Copyright 2008, Jason Paul Kazarian. All rights reserved.

Using Data Mining for Mobile Communication Clustering and Characterization

BETTER INFORMATION LEADS TO BETTER PRIVATE HEALTH INSURANCE DECISIONS

2016 Examina on dates

Jon Buschke 3059 Austin Ave Simi Valley, CA (805)

Financial Operating Procedure: Budget Monitoring

Monthly Report for Last Atlantis Partners LLC Share Classes November, 2008

Stephen Few, Perceptual Edge Visual Business Intelligence Newsletter November/December 2008

10/27/14. Consumer Credit Risk Management. Tackling the Challenges of Big Data Big Data Analytics. Andrew W. Lo

SWIFT. Page:1. Openstack Swift. Object Store Cloud built from the grounds up. David Hadas Swift ATC. HRL 2012 IBM Corporation

Are you prepared to make the decisions that matter most? Decision making in healthcare

WEB TRADER USER MANUAL

Quantitative Software Management

FY 2015 Schedule at a Glance

Office of Emergency Management Santa Monica, CA October 2014 Page 1. Public Safety Consolidation Communications Implementation Plan

Are you prepared to make the decisions that matter most? Decision making in retail

Reading and Writing Small Numbers

New York dairy manure management greenhouse gas emissions and mitigation costs ( ) Jenifer L. Wightman and Peter B.

Discussion Outline. A. KPIs Defined. B. Why KPIs Matter. C. KPI Menu. D. Implementation. E. Example KPIs. F. Pitfalls

Working Capital and the Financing Decision C H A P T E R S I X

MGEX Agricultural Index Futures and Options

Package TSfame. February 15, 2013

PATIENT JOURNEY BOARDS

Introduction to the data.table package in R

Are you prepared to make the decisions that matter most? Decision making in manufacturing

Performance of pairs trading on the S&P 500 index

Transcription:

Working with xts and quantmod Leveraging R with xts and quantmod for quantitative trading Jeffrey A. Ryan jeffrey.ryan@insightalgo.com R/Finance Workshop Presented on April 24, R/Finance : Applied Finance with R University of Illinois at Chicago

xts: extensible time series Most everything in trading involves a time series Regular data (positions data, P&L) Irregular data (market data, book data, trades) R has many ways to manage this...

xts: extensible time series Data Classes

xts: extensible time series fts Data Classes matrix mts tframe data.table data.frame zoo its timeseries ts irts vectors zooreg

xts: extensible time series fts Data Classes matrix mts tframe data.table data.frame zoo its timeseries ts irts vectors zooreg Time Classes

xts: extensible time series fts Data Classes matrix mts tframe data.table data.frame zoo its timeseries ts irts vectors zooreg Time Classes chron character POSIXct Date POSIXlt numeric yearmon yearqtr timedate

xts: extensible time series fts Data Classes matrix mts tframe data.table data.frame zoo its ts Sometimes, choice is timeseries irts vectors bad for package zooreg developers and interoperability Time Classes chron POSIXct character Date POSIXlt numeric yearmon yearqtr timedate

xts: extensible time series The solution?

xts: extensible time series add one more class of course...

xts: extensible time series Motivation (c. 2007) Avid user of zoo Natural R-like interface Flexible and complete methods S3! I still wanted a few features for trading... Additional series metadata Require time-based indexing Conversion/reconversion tools

xts: extensible time series Significant design requirements for xts: Preserve zoo behavior Utilize time-based indexing Allow for arbitrary attributes to be cleanly attached ISO 8601 subsetting by time strings Lossless conversion utilities to hide messy details

xts: extensible time series What s inside an xts object? Index + Matrix + Attr Internally the storage is always a numeric vector! Can be of any class supported by matrix Hidden attributes AND user attributes.indexclass,.indexformat,.indextz,.class, index

xts: extensible time series What s inside an xts object? Index + Matrix + Attr Internally the storage is always a numeric vector! Can be of any class supported by matrix Hidden attributes AND user attributes.indexclass,.indexformat,.indextz,.class, index

xts: extensible time series What s inside an xts object? Index + Matrix + Attr Internally the storage is always a numeric vector! Can be of any class supported by matrix Hidden attributes AND user attributes.indexclass,.indexformat,.indextz,.class, index

xts: extensible time series What s inside an xts object? Index + Matrix + Attr Internally the storage is always a numeric vector! Can be of any class supported by matrix Hidden attributes AND user attributes.indexclass,.indexformat,.indextz,.class, index

xts: extensible time series What s inside an xts object? Index + Matrix + Attr Internally the storage is always a numeric vector! Can be of any class supported by matrix Hidden attributes AND user attributes.indexclass,.indexformat,.indextz,.class, index Important! index must be a time-based class

xts: extensible time series Index as numeric? That isn t time-based!! Internally all index values are represented in POSIX time (seconds since the epoch) Coercion happens at object creation or upon index replacement index() converts back to user-level class.index() access the raw seconds in the index.indexclass,.indexformat and.indextz attributes Rationale? Simplicity for C level code, removal of multiple conversion in most instances, more consistent behavior All details are hidden from the user

xts: extensible time series Time-based indexing in xts (ISO 8601) Date and time organized from most significant to least significant: CCYY-MM-DD HH:MM:SS[.s] Fixed number of digits Separators can be omitted e.g. CCYYMMDDHHMMSS Reduced accuracy forms are valid: e.g. CCYY-MM Fractional decimal time is supported Intervals can be expressed e.g. 2000-05/2001-04 source: http://en.wikipedia.org/iso_8601

xts: extensible time series Create an xts object Load xts package > library(xts) Loading required package: zoo xts now requires a valid TZ variable to be set your current TZ:America/Chicago > x <- xts(rnorm(10), Sys.Date()+1:10) > x [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493-04-02 1.4152439

xts: extensible time series Create an xts object > library(xts) Loading required package: zoo xts now requires a valid TZ variable to be set your current TZ:America/Chicago Create an xts object > x <- xts(rnorm(10), Sys.Date()+1:10) > x [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493-04-02 1.4152439

xts: extensible time series Indexing by time Index using standard tools (still works) > x[ index(x) >= as.date("-03-28") & index(x) <= + as.date("-04-01") ] [,1] -03-28 -0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493 > x["0328/0401"] [,1] -03-28 -0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493

xts: extensible time series Indexing by time > x[ index(x) >= as.date("-03-28") & index(x) <= + as.date("-04-01") ] [,1] -03-28 -0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493 Index via ISO-style with xts > x["0328/0401"] [,1] -03-28 -0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493

xts: extensible time series Indexing by time > x[""] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493-04-02 1.4152439 > x["04"] [,1] -04-01 0.8100493-04-02 1.4152439 > x["0301/03"] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293 All of

xts: extensible time series Indexing by time > x[""] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493-04-02 1.4152439 > x["04"] [,1] -04-01 0.8100493-04-02 1.4152439 > x["0301/03"] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293 All of April

xts: extensible time series Indexing by time > x[""] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293-04-01 0.8100493-04-02 1.4152439 > x["04"] [,1] -04-01 0.8100493-04-02 1.4152439 > x["0301/03"] [,1] -03-24 0.3554788-03-25 1.2812633-03-26 0.1268833-03-27-0.6945146-03-28-0.3936148-03-29-0.1938840-03-30 0.2368576-03-31-1.2152293 From the first March to the end of March

xts: extensible time series All subsetting is via a binary search algorithm. F-A-S-T! > str(x10m) # 10 million observations An 'xts' object from -03-23 16:19:00 to -07-17 10:05:39 containing: Data: int [1:10000000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > str(x100k) # 100 thousand observations An 'xts' object from -03-23 16:19:00 to -03-24 20:05:39 containing: Data: int [1:100000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > system.time(x10m['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x10m[index(x10m) >= as.posixct('-03-23 16:19:00') & index(x10m) <= as.posixct('-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832

xts: extensible time series All subsetting is via a binary search algorithm. F-A-S-T! > str(x10m) # 10 million observations An 'xts' object from -03-23 16:19:00 to -07-17 10:05:39 containing: Data: int [1:10000000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > str(x100k) # 100 thousand observations An 'xts' object from -03-23 16:19:00 to -03-24 20:05:39 containing: Data: int [1:100000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > system.time(x10m['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x10m[index(x10m) >= as.posixct('-03-23 16:19:00') & index(x10m) <= as.posixct('-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832

xts: extensible time series All subsetting is via a binary search algorithm. F-A-S-T! > str(x10m) # 10 million observations An 'xts' object from -03-23 16:19:00 to -07-17 10:05:39 containing: Data: int [1:10000000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > str(x100k) # 100 thousand observations An 'xts' object from -03-23 16:19:00 to -03-24 20:05:39 containing: Data: int [1:100000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > system.time(x10m['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['0323']) user system elapsed 0.006 0.001 0.006 Identical speed! > system.time(x10m[index(x10m) >= as.posixct('-03-23 16:19:00') & index(x10m) <= as.posixct('-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832

xts: extensible time series All subsetting is via a binary search algorithm. F-A-S-T! > str(x10m) # 10 million observations An 'xts' object from -03-23 16:19:00 to -07-17 10:05:39 containing: Data: int [1:10000000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > str(x100k) # 100 thousand observations An 'xts' object from -03-23 16:19:00 to -03-24 20:05:39 containing: Data: int [1:100000, 1] 1 2 3 4 5 6 7 8 9 10... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/Chicago xts Attributes: NULL > system.time(x10m['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['0323']) user system elapsed 0.006 0.001 0.006 > system.time(x10m[index(x10m) >= as.posixct('-03-23 16:19:00') & index(x10m) <= as.posixct('-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832

xts: extensible time series xts + C Moving [.xts to C dramatically decreased subsetting costs Highest cost basic operation in R was merge. Prime C candidate Implemented optimized sort-merge-join in C with custom algorithm Additional C based routines followed... xts now has 3000+ lines of C

xts: extensible time series Additional xts tools to.period, period.apply, endpoints, timebasedrange, try.xts, reclass, Reclass

xts: extensible time series...in development

xts: extensible time series Binary.xd files Representation of xts objects on disk Seekable for disk-based subsetting Future time-series database structure XTS (disk) xts (memory) xtsdb

xts: extensible time series Parallel processing period.apply runsum, runcov, runsd, etc. (moving from TTR)

xts: extensible time series Parallel processing period.apply runsum, runcov, runsd, etc. Multiple index support dimensional attributes

xts: extensible time series Parallel processing period.apply runsum, runcov, runsd, etc. Multiple index support dimensional attributes Tighter zoo integration Backport C code into zoo

AAPL [ 12 01/ 03 23] 105 100 95 90 85 80 Dec 01 Dec 15 Dec 29 Jan 12 Jan 26 Feb 09 Feb 23 Mar 09 Mar 23 quantmod

quantmod was envisioned to be a rapid prototyping environment in R to facilitate quantitative modeling, testing, and trading

Data. Visualization. Modeling.

Data. Visualization. Modeling. Trading requires lots of different types of data, from many different sources. quantmod aims to hide the details of the data source, to make using data a priority

Data. Visualization. Modeling. Trading requires lots of different types of data, from many different sources. quantmod aims to hide the details of the data source, to make using data a priority getsymbols

Data. Visualization. Modeling. getsymbols csv Rdata MySQL SQLite google yahoo Interactive Brokers FRED oanda

Data. Visualization. Modeling. getsymbols getsymbols is the top level function that dispatches to custom methods based on user direction setsymbollookup getsymbollookup savesymbollookup loadsymbollookup

Data. Visualization. Modeling. getsymbols getsymbols behave like base::load by assigning objects into the user s workspace (.GlobalEnv)

Data. Visualization. Modeling. getsymbols getsymbols behave like base::load by assigning objects into the user s workspace (.GlobalEnv) Rationale: when dealing with potentially dozens of symbols interactively, it is redundant to have to manually assign each. Also facilitates multiple requests.

Data. Visualization. Modeling. getsymbols getsymbols behave like base::load by assigning objects into the user s workspace (.GlobalEnv) getsymbols (devel) now returns all symbols in an environment! loadsymbols will be available to directly replace the previous getsymbols behavior UPDATED

Data. Visualization. Modeling. getsymbols getsymbols( AAPL ) getsymbols( AAPL;SBUX ) getsymbols( USD/EUR,src= oanda )

Data. Visualization. Modeling. getsymbols getsymbols( AAPL ) getsymbols( AAPL;SBUX ) getsymbols( USD/EUR,src= oanda ) Additional data wrappers: getdividends, getquote, getsplits, getfx, getmetals, getoptionchain

Data. Visualization. Modeling. Interactive, highly customizable financial charting in R

Data. Visualization. Modeling. ^gspc price 700 750 800 850 02 02 02 14 02 27 03 11 03 23 Time Basic OHLC chart from tseries

Data. Visualization. Modeling. GSPC [ 02 02/ 03 23] 850 800 750 700 Feb 02 Feb 09 Feb 17 Feb 23 Mar 02 Mar 09 Mar 16 Mar 23 candlechart(gspc, subset='02/', theme='white', TA=NULL)

Data. Visualization. Modeling. Requirements Fast rendering (base plotting tools) Interactive and scriptable Work with all timeseries classes Minimal commands Highly customizable Full technical analysis support (via TTR)

Data. Visualization. Modeling. The Basics 130 120 110 100 90 80 70 30 25 20 15 10 5 IBM Last 98.71 Volume (millions): 12,407,200 [2007 01 03/ 03 23] Jan 03 2007 Apr 02 2007 Jul 02 2007 Oct 01 2007 Jan 02 Apr 01 Jul 01 Oct 01 Jan 02 Mar 23 > chartseries(ibm)

Data. Visualization. Modeling. The Basics 130 120 110 100 90 80 70 30 25 20 15 10 5 IBM Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 Volume (millions): 12,407,200 [2007 01 03/ 03 23] Jan 03 2007 Apr 02 2007 Jul 02 2007 Oct 01 2007 Jan 02 Apr 01 Jul 01 Oct 01 Jan 02 Mar 23 > addbbands()

Data. Visualization. Modeling. The Basics IBM [2007 01 03/ 03 23] 130 120 Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 110 100 90 80 70 30 25 20 15 10 5 5 0 Volume (millions): 12,407,200 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 5 Jan 03 2007 Mar 01 2007 May 01 2007 Jul 02 2007 Sep 04 2007 Nov 01 2007 Jan 02 Mar 03 May 01 Jul 01 Sep 02 Nov 03 Jan 02 Mar 02 > addmacd(32,50,12)

Data. Visualization. Modeling. The Basics IBM [ 01 02/ 03 23] Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 95 90 85 25 20 15 10 3 2 1 0 1 2 3 Volume (millions): 12,407,200 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 Jan 12 Jan 20 Jan 26 Feb 02 Feb 09 Feb 17 Feb 23 Mar 02 Mar 09 Mar 16 Mar 23 > rechart(subset=,theme= white, type= candles )

Data. Visualization. Modeling. Inside chartseries 200 180 160 140 AAPL Last 107.66 [2007 01 03/ 03 23] chartseries 120 100 80 120 100 80 60 40 20 Volume (millions): 23,799,900 Jan 03 2007 May 01 2007 Sep 04 2007 Jan 02 May 01 Sep 02 Jan 02 chob (chart object) 95 IBM Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 [ 01 02/ 03 23] addta chobta 85 90 3 2 1 01 2 3 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 Jan 20 Feb 02 Feb 17 Mar 02 Mar 16

Data. Visualization. Modeling. Inside chartseries 200 180 160 140 AAPL Last 107.66 [2007 01 03/ 03 23] chartseries 120 100 80 120 100 80 60 40 20 Volume (millions): 23,799,900 Jan 03 2007 May 01 2007 Sep 04 2007 Jan 02 May 01 Sep 02 Jan 02 chob (chart object) 95 IBM Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 [ 01 02/ 03 23] addta chobta 85 90 3 2 1 01 2 3 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 Jan 20 Feb 02 Feb 17 Mar 02 Mar 16

Data. Visualization. Modeling. Inside chartseries 200 180 160 140 AAPL Last 107.66 [2007 01 03/ 03 23] chartseries 120 100 80 120 100 80 60 40 20 Volume (millions): 23,799,900 Jan 03 2007 May 01 2007 Sep 04 2007 Jan 02 May 01 Sep 02 Jan 02 chob (chart object) 95 IBM Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 [ 01 02/ 03 23] addta chobta 85 90 3 2 1 01 2 3 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 Jan 20 Feb 02 Feb 17 Mar 02 Mar 16

Data. Visualization. Modeling. Inside chartseries 200 180 160 140 AAPL Last 107.66 [2007 01 03/ 03 23] chartseries 120 100 80 120 100 80 60 40 20 Volume (millions): 23,799,900 Jan 03 2007 May 01 2007 Sep 04 2007 Jan 02 May 01 Sep 02 Jan 02 chob (chart object) 95 IBM Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 [ 01 02/ 03 23] addta chobta 85 90 3 2 1 01 2 3 Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 Jan 20 Feb 02 Feb 17 Mar 02 Mar 16 Drawn by chartseries.chob

Data. Visualization. Modeling. Extending chartseries

Data. Visualization. Modeling. GMMA Guppy Multiple Moving Average (with newta)

Data. Visualization. Modeling. > # create a function that returns our GMMA > GMMA <- function(x) { + fastma <- c(3,5,8,10,12,15) + slowma <- c(30,35,40,45,50,60) + x <- sapply(c(fastma,slowma), + function(xx) EMA(x,xx)) + return(x) + } >

Data. Visualization. Modeling. > # create a function that returns our GMMA > GMMA <- function(x) { + fastma <- c(3,5,8,10,12,15) + slowma <- c(30,35,40,45,50,60) + x <- sapply(c(fastma,slowma), + function(xx) EMA(x,xx)) + return(x) + } > > # create an addguppy function with newta > addguppy <- newta(fun=gmma, + prefun=cl, + col=c(rep(3,6), + rep( #333333,6)), + legend= GMMA ) > class(addguppy) [1] function

Data. Visualization. Modeling. > # create a function that returns our GMMA > GMMA <- function(x) { + fastma <- c(3,5,8,10,12,15) + slowma <- c(30,35,40,45,50,60) + x <- sapply(c(fastma,slowma), + function(xx) EMA(x,xx)) + return(x) + } > candlechart(aapl); addguppy() > # create an addguppy function with newta > addguppy <- newta(fun=gmma, + prefun=cl, + col=c(rep(3,6), + rep( #333333,6)), + legend= GMMA ) > class(addguppy) [1] function

Data. Visualization. Modeling. AAPL [ 08 01/ 12 01] 180 Last 88.93 160 140 120 100 80 80 Volume (millions): 32,991,700 60 40 20 180 160 140 120 100 80 GMMA () : 3 : 90.695 5 : 90.625 8 : 90.556 10 : 90.771 12 : 91.117 15 : 91.774 30 : 96.566 35 : 98.593 40 : 100.726 45 : 102.900 50 : 105.066 60 : 109.241 Aug 01 Aug 11 Aug 18 Aug 25 Sep 02 Sep 15 Sep 22 Sep 29 Oct 06 Oct 13 Oct 20 Oct 27 Nov 03 Nov 10 Nov 17 Nov 24

Data. Visualization. Modeling. AAPL [ 08 01/ 12 01] 180 Last 88.93 160 140 120 addguppy(on=-1, 100 80 col=c(rep( blue,6), 80 60 40 Volume (millions): 32,991,700 rep( black,6))) 20 180 160 140 120 100 80 GMMA () : 3 : 90.695 5 : 90.625 8 : 90.556 10 : 90.771 12 : 91.117 15 : 91.774 30 : 96.566 35 : 98.593 40 : 100.726 45 : 102.900 50 : 105.066 60 : 109.241 Aug 01 Aug 11 Aug 18 Aug 25 Sep 02 Sep 15 Sep 22 Sep 29 Oct 06 Oct 13 Oct 20 Oct 27 Nov 03 Nov 10 Nov 17 Nov 24

Data. Visualization. Modeling. AAPL [ 08 01/ 12 01] 180 Last 88.93 160 on arg selects panel 140 120 addguppy(on=-1, 100 80 col=c(rep( blue,6), 80 60 40 Volume (millions): 32,991,700 rep( black,6))) 20 180 160 140 120 100 80 GMMA () : 3 : 90.695 5 : 90.625 8 : 90.556 10 : 90.771 12 : 91.117 15 : 91.774 30 : 96.566 35 : 98.593 40 : 100.726 45 : 102.900 50 : 105.066 60 : 109.241 Aug 01 Aug 11 Aug 18 Aug 25 Sep 02 Sep 15 Sep 22 Sep 29 Oct 06 Oct 13 Oct 20 Oct 27 Nov 03 Nov 10 Nov 17 Nov 24

Data. Visualization. Modeling. AAPL [ 01 02/ 04 23] 120 110 100 Last 125.4 : 123.429 : 122.541 : 121.371 : 120.545 : 119.680 : 118.348 : 112.361 : 110.796 : 109.447 : 108.300 : 107.342 : 105.937 90 80 60 50 40 30 20 Volume (millions): 33,719,600 Jan 02 Jan 20 Feb 02 Feb 17 Mar 02 Mar 16 Mar 30 Apr 13 Apr 23

Data. Visualization. Modeling. chartseries3d Yield Curve Daily 5% 4% 3% 2% 1% Jan 02 Feb 01 Mar 03 Apr 01 May 01 Jun 02 Jul 01 Aug 01 Sep 02 Oct 01 Nov 03 Dec 01 Dec 31 1mo 3mo6mo 1yr 2yr3yr 5yr 7yr10yr 0% 20yr 30yr

Data. Visualization. Modeling. chartseries3d chartseries functionality to 3d/persp style graphics automatic time axis annotation interactive, rechart, rotchart, etc.

Data. Visualization. Modeling. chartseries + chartseries3d T[, "3mo"] [ 01 02/ 12 31] T[, "2yr"] [ 01 02/ 12 31] T[, "5yr"] [ 01 02/ 12 31] T[, "10yr"] [ 01 02/ 12 31] 3.0 2.5 2.0 1.5 1.0 0.5 0.0 Last 0.11 3.0 2.5 2.0 1.5 1.0 Last 0.76 3.5 3.0 2.5 2.0 1.5 Last 1.55 4.0 3.5 3.0 2.5 2.0 Last 2.25 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Yield Curve Daily Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 5% 4% 3% 2% 1% Jan 02 Feb 01 Mar 03 Apr 01 May 01 Jun 02 Jul 01 Aug 01 Sep 02 Oct 01 Nov 03 Dec 01 Dec 31 1mo 3mo 6mo 1yr 2yr 3yr 5yr 7yr 10yr 0% 30yr 20yr

Data. Visualization. Modeling. T[, "3mo"] [ 01 02/ 12 31] T[, "2yr"] [ 01 02/ 12 31] T[, "5yr"] [ 01 02/ 12 31] T[, "10yr"] [ 01 02/ 12 31] 3.0 2.5 2.0 1.5 1.0 0.5 0.0 Last 0.11 3.0 2.5 2.0 1.5 1.0 Last 0.76 3.5 3.0 2.5 2.0 1.5 Last 1.55 4.0 3.5 3.0 2.5 2.0 Last 2.25 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Yield Curve Daily Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 Jan 02 Apr 01 Jun 02 Aug 01 Oct 01 Dec 31 options (risk and volatility) 5% 4% 3% limit order book data 2% 1% Jan 02 Feb 01 Mar 03 Apr 01 May 01 Jun 02 Jul 01 Aug 01 Sep 02 Oct 01 Nov 03 Dec 01 Dec 31 1mo 3mo 6mo 1yr 2yr 3yr 5yr 7yr 10yr 0% 30yr 20yr

attachsymbols New functionality to extend upon getsymbols

attachsymbols New functionality to extend upon getsymbols Create a demand based database system using getsymbols that allows for implicit loading of an entire universe of symbols

attachsymbols Example: All US Equity symbols on demand. > search() [1] ".GlobalEnv" "package:quantmod" "package:defaults" [4] "package:xts" "package:zoo" "package:stats" [7] "package:graphics" "package:grdevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base"

attachsymbols Example: All US Equity symbols on demand. > search() [1] ".GlobalEnv" "package:quantmod" "package:defaults" [4] "package:xts" "package:zoo" "package:stats" [7] "package:graphics" "package:grdevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base" > attachsymbols(db=ddb_yahoo(), pos=2, prefix="e.") Contains symbols and method

attachsymbols Example: All US Equity symbols on demand. > search() [1] ".GlobalEnv" "package:quantmod" "package:defaults" [4] "package:xts" "package:zoo" "package:stats" [7] "package:graphics" "package:grdevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base" > attachsymbols(db=ddb_yahoo(), pos=2, prefix="e.") > search() [1] ".GlobalEnv" "DDB:Yahoo" "package:quantmod" [4] "package:defaults" "package:xts" "package:zoo" [7] "package:stats" "package:graphics" "package:grdevices" [10] "package:utils" "package:datasets" "package:methods" [13] "Autoloads" "package:base"

attachsymbols Example: All US Equity symbols on demand. > search() [1] ".GlobalEnv" "package:quantmod" "package:defaults" [4] "package:xts" "package:zoo" "package:stats" [7] "package:graphics" "package:grdevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base" > attachsymbols(db=ddb_yahoo(), pos=2, prefix="e.") > search() [1] ".GlobalEnv" "DDB:Yahoo" "package:quantmod" [4] "package:defaults" "package:xts" "package:zoo" [7] "package:stats" "package:graphics" "package:grdevices" [10] "package:utils" "package:datasets" "package:methods" [13] "Autoloads" "package:base" > str(ls("ddb:yahoo")) chr [1:7406] "E.A" "E.AA" "E.AAC" "E.AACC" "E.AAI" "E.AAII"...

attachsymbols Example: All US Equity symbols on demand. > search() [1] ".GlobalEnv" "package:quantmod" "package:defaults" [4] "package:xts" "package:zoo" "package:stats" [7] "package:graphics" "package:grdevices" "package:utils" [10] "package:datasets" "package:methods" "Autoloads" [13] "package:base" 7406 symbols are available > attachsymbols(db=ddb_yahoo(), pos=2, prefix="e.") > search() [1] ".GlobalEnv" "DDB:Yahoo" "package:quantmod" [4] "package:defaults" "package:xts" "package:zoo" [7] "package:stats" "package:graphics" "package:grdevices" [10] "package:utils" "package:datasets" "package:methods" [13] "Autoloads" "package:base" > str(ls("ddb:yahoo")) chr [1:7406] "E.A" "E.AA" "E.AAC" "E.AACC" "E.AAI" "E.AAII"...

attachsymbols Example: All US Equity symbols on demand. > str(e.a) An 'xts' object from 2007-01-03 to -03-23 containing: Data: num [1:559, 1:6] 35 34.3 34.3 34 34.1... - attr(*, "dimnames")=list of 2..$ : NULL..$ : chr [1:6] "A.Open" "A.High" "A.Low" "A.Close"... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/ Chicago xts Attributes: List of 2 $ src : chr "yahoo" $ updated: POSIXct[1:1], format: "-03-24 10:59:14"

attachsymbols Example: All US Equity symbols on demand. > str(e.a) An 'xts' object from 2007-01-03 to -03-23 containing: Data: num [1:559, 1:6] 35 34.3 34.3 34 34.1... - attr(*, "dimnames")=list of 2..$ : NULL..$ : chr [1:6] "A.Open" "A.High" "A.Low" "A.Close"... Indexed by objects of class: [POSIXt,POSIXct] TZ: America/ Chicago xts Attributes: List of 2 $ src : chr "yahoo" $ updated: POSIXct[1:1], format: "-03-24 10:59:14" First access loads data

attachsymbols Example: All US Equity symbols on demand. > system.time(e.akam) user system elapsed 0.032 0.004 0.267 download from Yahoo

attachsymbols Example: All US Equity symbols on demand. > system.time(e.akam) user system elapsed 0.032 0.004 0.267 > system.time(e.akam) user system elapsed 0 0 0 subsequent calls from cache

attachsymbols Two Methods to Cache Disk Memory after first access, objects are cached to disk. makeactivebinding after first access objects remain in memory delayedassign

attachsymbols Custom DDB methods DDB object Binding Function Attach symbols

attachsymbols Custom DDB methods example: DDB:Yahoo > DDB_Yahoo() > # creates DDB object of all US Equity Symbols

attachsymbols Custom DDB methods example: DDB:Yahoo > DDB_Yahoo() > # creates DDB object of all US Equity Symbols > str(quantmod:::ddb_yahoo()) List of 3 $ name: chr "DDB:Yahoo" $ src : chr "yahoo" $ db : chr [1:7358] "AACC" "AAME" "AANB" "AAON"... - attr(*, "class")= chr "DDB"

attachsymbols Custom DDB methods example: DDB:Yahoo > attachsymbols() > # binds symbols to functions to load/reload

attachsymbols Custom DDB methods example: DDB:Yahoo > attachsymbols() > # binds symbols to functions to load/reload A few details...

attachsymbols attachsymbols attachsymbols.yahoo create.binding

attachsymbols attachsymbols attachsymbols.yahoo create.binding > attachsymbols function (DB = DDB_Yahoo(), pos = 2, prefix = NULL, postfix = NULL, mem.cache = TRUE, file.cache = FALSE, cache.dir = tempdir()) { if (!inherits(db, "DDB")) stop("db must be of class 'DDB'") do.call(paste("attachsymbols", DB$src, sep = "."), list(db = DB, pos = pos, prefix = prefix, postfix = postfix, mem.cache = mem.cache, file.cache = file.cache, cache.dir = cache.dir)) } <environment: namespace:quantmod>

attachsymbols attachsymbols attachsymbols.yahoo create.binding > attachsymbols function (DB = DDB_Yahoo(), pos = 2, prefix = NULL, postfix = NULL, mem.cache = TRUE, file.cache = FALSE, cache.dir = tempdir()) { if (!inherits(db, "DDB")) stop("db must be of class 'DDB'") do.call(paste("attachsymbols", DB$src, sep = "."), list(db = DB, pos = pos, prefix = prefix, postfix = postfix, mem.cache = mem.cache, file.cache = file.cache, cache.dir = cache.dir)) } <environment: namespace:quantmod>

attachsymbols attachsymbols attachsymbols.yahoo create.binding > quantmod:::attachsymbols.yahoo function (DB, pos, prefix, postfix, mem.cache, file.cache, cache.dir,...) { attach(null, pos = pos, name = DB$name) rsym <- function(x) gsub("_", "-", x, perl = TRUE) lsym <- function(x) paste(prefix, as.character(x), postfix, sep = "") invisible(sapply(db$db, create.binding, lsym = lsym, rsym = rsym, mem.cache = mem.cache, file.cache = file.cache, cache.dir = cache.dir, envir = DB$name)) } <environment: namespace:quantmod>

attachsymbols attachsymbols attachsymbols.yahoo create.binding > quantmod:::create.binding function (s, lsym, rsym, mem.cache = TRUE, file.cache = FALSE, cache.dir = tempdir(), envir) {...... if(file.cache) {... makeactivebinding(lsym(s), f, as.environment(envir)) } if (mem.cache) { envir <- as.environment(envir) delayedassign(lsym(s), { assign(lsym(s), getsymbols(rsym(s), auto.assign = FALSE), env = envir) get(lsym(s), env = envir) }, assign.env = envir) } } <environment: namespace:quantmod>

attachsymbols attachsymbols attachsymbols.yahoo create.binding > quantmod:::create.binding function (s, lsym, rsym, mem.cache = TRUE, file.cache = FALSE, cache.dir = tempdir(), envir) {...... if(file.cache) {... makeactivebinding(lsym(s), f, as.environment(envir)) } if (mem.cache) { envir <- as.environment(envir) delayedassign(lsym(s), { assign(lsym(s), getsymbols(rsym(s), auto.assign = FALSE), env = envir) get(lsym(s), env = envir) }, assign.env = envir) } } <environment: namespace:quantmod>

attachsymbols Custom DDB uses Auto-loading data based on source Multiple sources in unique environments - in one session Simple mechanisms to create and manage - leverages getsymbols infrastructure attachsymbols(dtn_ddb()) attachsymbols(bloomberg_bonds_ddb()) attachsymbols(opra_ddb())

Future Work Integration of trading/testing with blotter & PerformanceAnalytics package More data methods, easier to extend specifymodel - buildmodel - trademodel work

More Information www.insightalgo.com www.quantmod.com Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com www.quantmod.com/rfinance