The Quantmod Package
The Quantmod Package
mm 40 60 80 100 120
September, 2011
60
Guy Yollin
Principal Consultant, r-programming.org Visiting Lecturer, University of Washington
80
2011)
R for Finance
quantmod
1 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
2 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
3 / 64
R is based on the S language originally developed by John Chambers and 40 colleagues at AT&T Bell Labs in the late 1970s and 1980s R (sometimes called GNU S ) is free open source software licensed under the GNU general public license (GPL 2)
60
R development was initiated by Robert Gentleman and Ross Ihaka at the University of Auckland, New Zealand in the early 1990s R is 80 formally known as The R Project for Statistical Computing
www.r-project.org
Guy Yollin (Copyright
2011)
R for Finance
quantmod
4 / 64
R timeline
mm 40
1991 Statistical Models in S (white book) S3 methods 1984 S: An Interactive Envirnoment for Data Analysis and Graphics 1988 (Brown Book) The New S Language Written in C (Blue Book) Work on S Version 1
60
1999 John Chambers 1998 ACM Software Award
80
2001 R 1.4.0 (S4)
100
120
40
2002 Modern Applied Statistics with S 4th Edition (S+ 6.x, R 1.5.0)
2004 R 2.0.0
1976
60
S-PLULS developed by Statistical Sciences, Inc. 1988 StatSci Licenses S 1993 Modern Applied Statistics with S-PLUS 1994 Modern Applied Statistics with S-PLUS 2nd Edition 1997 Modern Applied Statistics with S-PLUS Programming with Data 3rd Edition (Green Book) (S+ 2000, 5.x) (S+ 5.x) (R complements) 1998 1999
2011
80
S era
S-PLUS era
R era
2011)
R for Finance
quantmod
5 / 64
40
60
80
http://cran.r-project.org/web/views/Finance.html
Guy Yollin (Copyright
2011)
R for Finance
quantmod
6 / 64
R-SIG-FINANCE
mm 40 60 80 100 120
https://stat.ethz.ch/mailman/ listinfo/r-sig-finance Nerve center of the R nance community Daily must read Exclusively for Finance-specic 60 questions, not general R questions
80 40
2011)
R for Finance
quantmod
7 / 64
40
60
80
100
120
quantstrat: quantitative strategy model framework blotter: tools for transaction-oriented trading systems development
40
Quantitative trading rules and trading accouting
60
80
2011)
R for Finance
quantmod
8 / 64
Lecture references
quantmod package mm 40
60 80 100 Jerey Ryans quantmod website: http://www.quantmod.com/ quantmod project page on R-forge: http://r-forge.r-project.org/projects/quantmod 40 Joshua Ulrichs Foss Trading blog: http://blog.fosstrading.com/ TTR project page on R-forge: 60 http://r-forge.r-project.org/projects/ttr/ 120
TTR package
xts package
xts project page on R-forge: http://r-forge.r-project.org/projects/xts/ 80
2011)
R for Finance
quantmod
9 / 64
R-forge
R-forge is a hosting platform for R package development which mm 40 60 80 100 120 includes SVN, daily build and check, mailing lists, bug tracking, message boards etc. More then 1000 R packages are hosted on R-forge including all of the 40 trading system development packages mentioned earlier It is common for new packages to be developed on R-forge and for mature packages to be maintained on R-forge even after being hosted on CRAN
60
2011)
R for Finance
quantmod
10 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
11 / 64
2011)
R for Finance
quantmod
12 / 64
2011)
R for Finance
quantmod
13 / 64
mm
40
60
80
100
120
[1] "GSPC" > ls() [1] "GSPC" > showSymbols() 40 GSPC "yahoo" > first(GSPC) GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted 60 2007-01-03 > last(GSPC) 2011-09-06 GSPC.Open GSPC.High GSPC.Low GSPC.Close GSPC.Volume GSPC.Adjusted 1173.97 1173.97 1140.13 1165.24 5103980000 1165.24 1418.03 1429.42 1407.86 1416.6 3429160000 1416.6
80
2011)
R for Finance
quantmod
14 / 64
80
100
120
60 Main arguments: x an OHLC object type style of chart to draw theme a chart.theme object 80 subset xts style date subsetting argument TA a vector of technical indicators and params
Guy Yollin (Copyright
2011)
R for Finance
quantmod
15 / 64
mm > chartSeries(GSPC,subset="2011",theme="white") 40 60
GSPC
Last 1165.24
80
100
120
[20110103/20110906]
1350
40
1300
1250
1200
60
6000 5000 4000 3000 2000 1000 Volume (millions): 5,103,980,000
1150
1100
80
Jan 03 2011
Feb 01 2011
Mar 01 2011
Apr 01 2011
May 02 2011
Jun 01 2011
Jul 01 2011
Aug 01 2011
Sep 01 2011
2011)
R for Finance
quantmod
16 / 64
60
"grid.col" "dn.col" "up.border" "up.dn.border" "Expiry"
80
"border" "dn.up.col" "dn.border" "main.col" "theme.name"
100
120
whiteTheme$bg.col <- "white" whiteTheme$dn.col <- "pink" whiteTheme$up.col <- "lightgreen" whiteTheme$border <- "lightgray" 60 x <- chartSeries(GSPC,subset="2011",theme=whiteTheme,TA=NULL) class(x)
80
2011)
R for Finance
quantmod
17 / 64
A chartSeries plot
GSPC [20110103/20110906]
mm
40
60
80
100
120
1350
1300
40
1250
1200
60
1150
80
Jan 03 2011
Guy Yollin (Copyright
Apr 01 2011
Jun 01 2011
R for Finance
Jul 01 2011
Sep 01 2011
quantmod 18 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
19 / 64
P&L
80
2011)
R for Finance
quantmod
20 / 64
overview regularly spaced time series multiple regularly spaced time series default for Rmetrics packages reg/irreg and arbitrary time stamp classes an extension of the zoo class
R for Finance quantmod 21 / 64
start return start of time series end return end of time series frequency return frequency of time series window Extract subset of time series index return time index of time series time return time index of time series
60 coredata return data of time series 40
diff dierence of the time series lag lag of the time series aggregate aggregate to lower resolution time series 80 cbind merge 2 or more time series together
Guy Yollin (Copyright
2011)
R for Finance
quantmod
22 / 64
create a zoo time series object merges time series (automatically handles of time alignment)
aggregate create coarser resolution time series with summary statistics rollapply calculate rolling window statistics readzoo 60read a text le into a zoo time series object Authors: Achim Zeileis
80 Gabor Grothendieck
2011)
R for Finance
quantmod
23 / 64
create an xts time series object convert time series data to an OHLC series
align.time align time series to a coarser resolution to.period [.xts Authors: Jerey Ryan Josh80 Ulrich
60 subset time series
2011)
R for Finance
quantmod
24 / 64
Index
40
a Data class or Time-Date class used for the time-stamp of observations the time series observations (univariate or multivariate) can be numeric, character, logical, etc. but must be homogeneous
60
Matrix
Attr
80
hidden attributes and user attributes class of the index format of the index time zone
2011)
R for Finance
quantmod
25 / 64
Date class
Date Dates are represented as the number of days since 1970-01-01
mm 40 60 80 100 120
80 > as.Date("110704",format="%y%m%d")
[1] "2011-07-04"
Guy Yollin (Copyright
2011)
R for Finance
quantmod
26 / 64
Date-time classes
POSIXct
mm
represents time and date as the number of seconds since 40 60 80 100 120 1970-01-01 represents time and date as 9 calendar and time components
POSIXlt
60 > unclass(d)
[1] 1315440935 > sapply(unclass(as.POSIXlt(d)), function(x) x) sec 34.94161 isdst 0.00000 min
80 15.00000
hour 0.00000
mday 8.00000
2011)
R for Finance
quantmod
27 / 64
xts subsetting
mm 40 60 80 100 120
xts time series objects can be easily subset: Date and time organized from most signicant to least signicant
CCYY-MM-DD HH:MM:SS[.s] 40
2011)
R for Finance
quantmod
28 / 64
xts subsetting
R Code: Subset xts object
100
120
[20110103/20110906]
1350 1300 1250 1200
40
60
6000 5000 4000 3000 2000 1000 Volume (millions): 5,103,980,000
1150 1100
80
Jan 03 2011
Mar 01 2011
Apr 01 2011
Jun 01 2011
Jul 01 2011
Sep 01 2011
2011)
R for Finance
quantmod
29 / 64
xts subsetting
R Code: Subset xts object
120
[20100104/20110906]
1350 1300 1250 1200 1150
40
60
10000 8000 6000 4000 2000 Volume (millions): 5,103,980,000
80
Jan 04 2010
Apr 01 2010
Jul 01 2010
Oct 01 2010
Jan 03 2011
Apr 01 2011
Jul 01 2011
2011)
R for Finance
quantmod
30 / 64
xts subsetting
R Code: Subset xts object
100
120
[20110701/20110729]
40
1340
1320
60
5000 4500 4000 Volume (millions): 5,061,190,000
1300
1280
80
3500 Jul 01 2011 Jul 07 2011 Jul 12 2011 Jul 15 2011 Jul 20 2011 Jul 25 2011 Jul 28 2011
2011)
R for Finance
quantmod
31 / 64
xts subsetting
R Code: Subset xts object
120
[20110401/20110729]
40
60
5000 4500 4000 3500 3000 2500 Volume (millions): 5,061,190,000
1280 1260
80
Apr 01 2011 Apr 18 2011 May 09 2011 May 31 2011 Jun 20 2011 Jul 05 2011 Jul 25 2011
2011)
R for Finance
quantmod
32 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
33 / 64
Arguments: return.class time series class of returned object 60 index.class class of date/time index from starting date of time series to ending date of time series
80 adjust
2011)
R for Finance
quantmod
34 / 64
100
120
40 SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted 2000-01-03 148.25 148.25 143.88 145.44 8164300 119.60 2000-01-04 143.53 144.06 139.64 139.75 8089800 114.92 2000-01-05 139.94 141.53 137.25 140.00 12177900 115.13 2000-01-06 139.63 141.50 137.75 137.75 6227200 113.28 2000-01-07 140.31 145.75 140.06 145.75 8066500 119.85 2000-01-1060 146.25 146.91 145.03 146.25 5741700 120.27
> head(index(SPY)) [1] "2000-01-03" "2000-01-04" "2000-01-05" "2000-01-06" "2000-01-07" "2000-01-10" > class(index(SPY)) [1] "Date"
80
2011)
R for Finance
quantmod
35 / 64
mm 40 60 80 > getSymbols("SBUX",index.class="POSIXct",from="2000-01-01")
[1] "SBUX" > head(SBUX)
100
120
SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume SBUX.Adjusted 2000-01-0340 23.87 24.69 23.25 24.66 12136400 6.01 2000-01-04 24.06 24.87 23.75 23.87 10795200 5.82 2000-01-05 23.94 24.62 23.69 24.19 14116400 5.90 2000-01-06 24.00 25.62 24.00 25.06 15420000 6.11 2000-01-07 24.75 25.00 24.25 24.94 13035200 6.08 2000-01-10 25.87 26.75 25.81 26.00 14535600 6.34 > head(index(SBUX)) [1] "2000-01-03 UTC" "2000-01-04 UTC" "2000-01-05 UTC" "2000-01-06 UTC" [5] "2000-01-07 UTC" "2000-01-10 UTC" > class(index(SBUX)) [1] "POSIXct" "POSIXt" > chartSeries(SBUX,theme=whiteTheme)
Guy Yollin (Copyright
60
80
2011)
R for Finance
quantmod
36 / 64
mm Last 39.18
40
60
80
100
120
60 50 40 30 20
40
60
150 100 50 0 Jan 03 2000 Jul 02 Jul 01 2001 2002 Volume (millions): 8,398,900
10
80
Jul 01 2003
Jul 01 2004
Jul 01 2005
Jul 01 2008
Jul 01 2009
Jul 01 2010
Jul 01 2011
37 / 64
2011)
R for Finance
quantmod
mm
40
60
80
100
120
SBUX.spl 1993-09-30 0.5 1995-12-04 0.5 1999-03-22 0.5 2001-04-30 0.5 2005-10-2440 0.5 > class(spl) [1] "xts" "zoo" > (div <- getDividends("SBUX"))
60 [,1] 2010-04-05 0.10 2010-08-02 0.13 2010-11-16 0.13 2011-02-07 0.13 2011-05-09 0.13 2011-08-0880 0.13
> class(div) [1] "xts" "zoo"
Guy Yollin (Copyright
2011)
R for Finance
quantmod
38 / 64
Arguments:
60 x
an OHLC object
use.Adjusted calculated from dividends and splits (F), or used Adjusted price column (T) Note from package author:
80
Using use.Adjusted = TRUE will be less precise than the method that employs actual split and dividend information.
Guy Yollin (Copyright
2011)
R for Finance
quantmod
39 / 64
80
100
120
60
SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume SBUX.Adjusted 2000-01-03 5.821792 6.021786 5.670577 6.014470 12136400 6.01 2000-01-04 5.868132 6.065688 5.792524 5.821792 10795200 5.82 2000-01-05 5.838865 6.004714 5.777891 5.899839 14116400 5.90 2000-01-06 5.853498 6.248610 5.853498 6.112028 15420000 6.11 2000-01-07806.036420 6.097394 5.914472 6.082760 13035200 6.08 2000-01-10 6.309584 6.524212 6.294950 6.341290 14535600 6.34
2011)
R for Finance
quantmod
40 / 64
mm > head(adj1)
40
60
80
100
120
SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume SBUX.Adjusted 2000-01-03 5.821792 6.021786 5.670577 6.014470 12136400 6.01 2000-01-04 5.868132 6.065688 5.792524 5.821792 10795200 5.82 2000-01-05 5.838865 6.004714 5.777891 5.899839 14116400 5.90 2000-01-06405.853498 6.248610 5.853498 6.112028 15420000 6.11 2000-01-07 6.036420 6.097394 5.914472 6.082760 13035200 6.08 2000-01-10 6.309584 6.524212 6.294950 6.341290 14535600 6.34 > adj2 <- adjustOHLC(SBUX, use.Adjusted=TRUE) > head(adj2)
60 SBUX.Open SBUX.High SBUX.Low SBUX.Close SBUX.Volume SBUX.Adjusted 2000-01-03 5.817466 6.017311 5.666363 6.01 12136400 6.01 2000-01-04 5.866326 6.063821 5.790742 5.82 10795200 5.82 2000-01-05 5.839024 6.004878 5.778049 5.90 14116400 5.90 2000-01-06 5.851556 6.246536 5.851556 6.11 15420000 6.11 2000-01-07 6.033681 6.094627 5.911788 6.08 13035200 6.08 2000-01-10806.308300 6.522885 6.293669 6.34 14535600 6.34
> chartSeries(adj1,theme=whiteTheme,name="SBUX")
Guy Yollin (Copyright
2011)
R for Finance
quantmod
41 / 64
mm Last 39.18
40
60
80
100
120 40
35 30
40
25 20 15
60
150 100 50 0 Jan 03 2000 Jul 02 Jul 01 2001 2002 Volume (millions): 8,398,900
10 5
80
Jul 01 2003
Jul 01 2004
Jul 01 2005
Jul 01 2008
Jul 01 2009
Jul 01 2010
Jul 01 2011
42 / 64
2011)
R for Finance
quantmod
120
60
2011)
R for Finance
quantmod
43 / 64
40
60
80
2011)
R for Finance
quantmod
44 / 64
100
120
40
DTB3 1954-01-04 1.33 > last(DTB3,'1 week') DTB3 2011-08-2960 0.02 2011-08-30 0.01 2011-08-31 0.02 2011-09-01 0.02 2011-09-02 0.02 > chartSeries(DTB3,theme="white") 80
2011)
R for Finance
quantmod
45 / 64
mm
Last 0.02
40
60
80
100
120
15
40
10
60
80
Jan 04 1954 Jan 04 1960 Jan 03 1966
2011)
0 Jan 03 1972 Jan 03 1978 Jan 03 1984 Jan 02 1990 Jan 02 1996 Jan 02 2002 Jan 02 2008
quantmod 46 / 64
R for Finance
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
47 / 64
The TTR package is a comprehensive collection of technical analysis indicators for R Key features: moving averages oscillators price channels trend indicators 60 Author: Joshua Ulrich
80 40
2011)
R for Finance
quantmod
48 / 64
mm
Description
40
60
80
Description
100
120
stochastic oscillator Aroon indicator Bollinger bands Chaikin Acc/Dist rate of change Close Location Value Chande Momentum Oscillator exponential moving average volume weighted MA Donchian Channel Ease of Movement Value MA converge/diverge Relative Strength Index Trend Detection Index Vertical Horizontal Filter
Directional Movement Index Average True Range Commodity Channel Index Chaikin Volatility momentum indicator Chaikin Money Flow simple moving average double exp mov avg volume weighed avg price Detrended Price Oscillator volatility estimators Money Flow Index Parabolic Stop-and-Reverse Triple Smoothed Exponential Osc Williams Acc/Dist Zig Zag trend line
chaikinVolatility momentum CMF SMA DEMA VWAP DPO volatility MFI SAR TRIX williamsAD ZigZag
40
60
80
Williams % R
2011)
R for Finance
quantmod
49 / 64
80
2011)
R for Finance
quantmod
50 / 64
Bollinger bands
SBUX
Last 39.18 Bollinger Bands (20,2) [Upper/Lower]: 39.703/34.747
[20110103/20110907]
mm
40
60
80
100
40 120 38
36
40
34
32
60
Volume (millions): 8,398,900
80
Jan 03 2011 Feb 01 2011
Mar 01 2011
Apr 01 2011
May 02 2011
Jun 01 2011
Jul 01 2011
Aug 01 2011
Sep 01 2011
2011)
R for Finance
quantmod
51 / 64
> macd <- MACD( Cl(SBUX), 12, 26, 9, maType="EMA" ) > last(macd,'1 week') macd signal 2011-09-06 0.01005927 -0.4979056 2011-09-0740 0.34641082 -0.3290423 > priceDPO <- DPO(Cl(SBUX)) > tail(na.omit(priceDPO)) SBUX.Close 2011-08-22 -1.900 60 -0.052 2011-08-23 2011-08-24 0.244 2011-08-25 -0.945 2011-08-26 -0.261 2011-08-29 0.154
2011)
R for Finance
quantmod
52 / 64
[20110103/20110907]
mm
40
60
80
100
120 40
38
36
40
34
32
3 2 1 0 1 2 3 4 2 0 2 4
60
80
Jan 03 2011 Feb 01 2011
Mar 01 2011
Apr 01 2011
May 02 2011
Jun 01 2011
Jul 01 2011
Aug 01 2011
Sep 01 2011
2011)
R for Finance
quantmod
53 / 64
The chartSeries functions are in the process of being updated; quantmod functions incorporating an underscore in their name are experimental version 2 functions: chart Series add TA chart Theme
60
> > > > > myTheme<-chart_theme() myTheme$col$up.col<-'lightgreen' myTheme$col$dn.col<-'pink' chart_Series(SBUX["2011"],theme=myTheme,name="SBUX") plot(add_BBands(on=1,sd=2,n=20,lwd=2,col=4))
40
80
2011)
R for Finance
quantmod
54 / 64
20110103 / 20110907
mm
40
60
80
100
120
42 41 40
41
40
39
39
38
40
38
37
37
36
36
35
35
60
34 34 33 33
32
32
31
80
Jan Feb Feb 01 2011 Mar 01 2011 Mar Apr 01 2011 Apr May 02 2011 May Jun 01 2011 Jun Jul 01 2011 Jul Aug 01 2011 Aug Sep Sep 01 2011
31
Jan 03 2011
2011)
R for Finance
quantmod
55 / 64
Outline
mm
1
40
60
80
100
120
2011)
R for Finance
quantmod
56 / 64
120
High-frequency time series require formal time-based classes for 40 indexing Recommended classes for high-frequency time series:
xts (extensible time series) class 60 POSIXct/POSIXlt date-time class for indexing
Class time series class Low frequency zoo Date High Frequency xts POSIXlt
80
2011)
R for Finance
quantmod
57 / 64
Arguments:
60
format date-time format specication tz timezone to use for conversion Return value: 80 POSIXlt object(s)
Guy Yollin (Copyright
2011)
R for Finance
quantmod
58 / 64
2011)
R for Finance
quantmod
59 / 64
> fn1 <- "GBPUSD.txt" > dat <- read.table(file=fn1,sep=",",header=T,as.is=T) > head(dat) 1 2 3 4 5 6 Date Time Open High Low Close Up Down 10/20/2002 2330 1.5501 1.5501 1.5481 1.5482 0 0 40 10/21/2002 0 1.5481 1.5483 1.5472 1.5472 0 0 10/21/2002 30 1.5471 1.5480 1.5470 1.5478 0 0 10/21/2002 100 1.5477 1.5481 1.5471 1.5480 0 0 10/21/2002 130 1.5480 1.5501 1.5479 1.5493 0 0 10/21/2002 200 1.5492 1.5497 1.5487 1.5492 0 0
mm
40
60
80
100
120
> tm <- strptime(paste(dat[,"Date"], 60 sprintf("%04d",dat[,"Time"])),format="%m/%d/%Y %H%M") > class(tm) [1] "POSIXlt" "POSIXt" > head(tm) [1] "2002-10-20 23:30:00" "2002-10-21 00:00:00" "2002-10-21 00:30:00" [4] "2002-10-21 01:00:00" "2002-10-21 01:30:00" "2002-10-21 02:00:00"
Guy Yollin (Copyright
80
2011)
R for Finance
quantmod
60 / 64
40
2007-01-01 17:30:00 2007-01-01 18:00:00 2007-01-01 18:30:00 2007-01-01 19:00:00 2007-01-01 19:30:00 60 2007-01-01 20:00:00 2007-01-01 20:30:00
80
2011)
R for Finance
quantmod
61 / 64
mm
Last 1.98542
40
60
80
100
120
1.988
1.986
40
1.984
1.982
60
1.980
1.978
1.976
80
Dec 24 00:00 Dec 24 04:00 Dec 24 08:00
2011)
Dec 24 12:00
Dec 26 02:00
Dec 26 06:00
Dec 26 10:00
Dec 26 14:30
Dec 26 20:00
quantmod 62 / 64
R for Finance
1.988
mm
40
60
80
100
120
1.988 1.987
1.987
1.986
1.986
1.985
1.985
1.984
40
1.984
1.983
1.983
1.982
1.982
1.981
1.981
1.980
60
Crossover bar
1.980
1.979
1.979
1.978
1.978
1.977
1.977
1.976
80
Dec 24 04:00
1.976
Dec 24 00:00
Dec 24 08:00
Dec 24 12:00
Dec 26 00:00
Dec 26 04:00
Dec 26 08:00
Dec 26 12:00
Dec 26 16:00
Dec 26 20:00
Dec 26 23:30
2011)
R for Finance
quantmod
63 / 64
80
2011)
R for Finance
quantmod
64 / 64