Quantmod Tutorial
Quantmod Tutorial
Guy Yollin
Applied Mathematics
University of Washington
Quantmod
1 / 74
Outline
Introduction
Time series in R
Quantmod
2 / 74
Outline
Introduction
Time series in R
Quantmod
3 / 74
Lecture references
quantmod package
Jeffrey Ryans quantmod website:
http://www.quantmod.com/
quantmod project page on R-forge:
http://r-forge.r-project.org/projects/quantmod
TTR package
Joshua Ulrichs Foss Trading blog:
http://blog.fosstrading.com/
TTR project page on R-forge:
http://r-forge.r-project.org/projects/ttr/
xts package
xts project page on R-forge:
http://r-forge.r-project.org/projects/xts/
Quantmod
4 / 74
R Package
PerformanceAnalytics - Tools for performance and risk analysis
Quantmod
5 / 74
merge
aggregate
rollapply
read.zoo
Authors:
Achim Zeileis
Gabor Grothendieck
Quantmod
6 / 74
align.time
to.period
[.xts
Authors:
Jeffrey Ryan
Josh Ulrich
Quantmod
7 / 74
R-forge
Quantmod
8 / 74
#
# install these packages from CRAN (or r-forge)
#
install.packages("xts", dependencies=TRUE)
install.packages("PerformanceAnalytics", dependencies=TRUE)
#
# Install these package from r-forge
#
install.packages("quantmod", repos = "http://R-Forge.R-project.org")
install.packages("TTR", repos = "http://R-Forge.R-project.org")
install.packages("FinancialInstrument", repos = "http://R-Forge.R-project.org")
install.packages("blotter", repos = "http://R-Forge.R-project.org")
install.packages("quantstrat", repos = "http://R-Forge.R-project.org")
Quantmod
9 / 74
Outline
Introduction
Time series in R
Quantmod
10 / 74
chartSeries
Author:
Jeffrey Ryan
Guy Yollin (Copyright 2014)
Quantmod
11 / 74
Main arguments:
Symbols
env
src
auto.assign
...
additional parameters
Return value:
an object of type return.class depending on env and auto.assign
Guy Yollin (Copyright 2014)
Quantmod
12 / 74
= 'Date',
Main arguments:
return.class
index.class
...
additional parameters
Return value:
an object of type return.class depending on env and auto.assign
Guy Yollin (Copyright 2014)
Quantmod
13 / 74
Quantmod
14 / 74
2014-09-02
2014-09-03
2014-09-04
2014-09-05
tail(Cl(GSPC),4)
##
##
##
##
##
2014-09-02
2014-09-03
2014-09-04
2014-09-05
GSPC.Close
2002.28
2000.72
1997.65
2007.71
tail(Ad(GSPC),4)
##
##
##
##
##
2014-09-02
2014-09-03
2014-09-04
2014-09-05
GSPC.Adjusted
2002.28
2000.72
1997.65
2007.71
Quantmod
15 / 74
Description
Op(x)
Get Open
Hi(x)
Get High
Lo(x)
Get Low
Cl(x)
Get Close
Vo(x)
Get Volume
Ad(x)
HLC(x)
OHLC(x)
Quantmod
16 / 74
Main arguments:
x
an OHLC object
type
theme
a chart.theme object
subset
TA
Return value:
a chob object
Guy Yollin (Copyright 2014)
Quantmod
17 / 74
[20140102/20140905]
Last 2007.71
2000
1950
1900
1850
1800
1750
5000
4500
4000
3500
3000
2500
2000
Volume (millions):
2,818,300,000
Jan 02 2014
Mar 03 2014
May 01 2014
Jul 01 2014
Sep 02 2014
Quantmod
18 / 74
"grid.col"
"dn.col"
"up.border"
"up.dn.border"
"Expiry"
"border"
"dn.up.col"
"dn.border"
"main.col"
"theme.name"
"minor.tick"
"up.up.col"
"dn.up.border"
"sub.col"
Quantmod
19 / 74
A chartSeries plot
GSPC
[20140701/20140905]
2000
1980
1960
1940
1920
Jul 01 2014
Guy Yollin (Copyright 2014)
Jul 21 2014
Aug 04 2014
Aug 18 2014
Sep 02 2014
Quantmod
20 / 74
Outline
Introduction
Time series in R
Quantmod
21 / 74
Quantmod
22 / 74
class
ts
mts
zoo
xts
package
stats
stats
zoo
xts
overview
regularly spaced time series
multiple regularly spaced time series
reg/irreg and arbitrary time stamp classes
an extension of the zoo class
Introduction to Trading Systems
Quantmod
23 / 74
end
frequency
window
index
time
coredata
diff
lag
aggregate
cbind
merge
Quantmod
24 / 74
Matrix
Attr
Quantmod
25 / 74
Date class
A Date object is stored internally as the days since 1970-01-01
myStr <- "2013-07-04"
class(myStr)
## [1] "character"
myDate <- as.Date(myStr)
class(myDate)
## [1] "Date"
as.numeric(myDate)
## [1] 15890
format(myDate,"%m/%d/%y")
## [1] "07/04/13"
as.Date("110704",format="%y%m%d")
## [1] "2011-07-04"
Guy Yollin (Copyright 2014)
Quantmod
26 / 74
Date-Time classes
A POSIXct object is a Date-Time object internally stored as the
number of seconds since 1970-01-01
A POSIXlt object is a Date-Time object internally stored as 9
calendar and time components
d <- Sys.time()
class(d)
## [1] "POSIXct" "POSIXt"
unclass(d)
## [1] 1410038854
sapply(unclass(as.POSIXlt(d)), function(x) x)
##
sec
## "33.5228459835052"
##
mon
##
"8"
##
isdst
##
"1"
Guy Yollin (Copyright 2014)
min
"27"
year
"114"
zone
"PDT"
hour
"14"
wday
"6"
gmtoff
"-25200"
Introduction to Trading Systems
mday
"6"
yday
"248"
Quantmod
27 / 74
xts subsetting
Quantmod
28 / 74
xts subsetting
chartSeries(GSPC["2014"],theme=whiteTheme,name="S&P 500")
S&P 500
[20140102/20140905]
Last 2007.71
2000
1950
1900
1850
1800
1750
5000
4500
4000
3500
3000
2500
2000
Volume (millions):
2,818,300,000
Jan 02 2014
Mar 03 2014
May 01 2014
Jul 01 2014
Sep 02 2014
Quantmod
29 / 74
xts subsetting
chartSeries(GSPC["2013/2014"],theme=whiteTheme,name="S&P 500")
S&P 500
[20130102/20140905]
2000
Last 2007.71
1900
1800
1700
1600
1500
5000
4000
3000
2000
Volume (millions):
2,818,300,000
Jan 02 2013
Jul 01 2014
Quantmod
30 / 74
xts subsetting
chartSeries(GSPC["2014-06::2014-07"],theme=whiteTheme,name="S&P 500")
S&P 500
[20140602/20140731]
Last 1930.67
1980
1960
1940
1920
4000
3500
3000
2500
2000
Volume (millions):
4.193e+09
Jun 02 2014
Jun 16 2014
Jun 30 2014
Jul 14 2014
Jul 28 2014
Quantmod
31 / 74
xts subsetting
chartSeries(GSPC["201406::"],theme=whiteTheme,name="S&P 500")
S&P 500
[20140602/20140905]
Last 2007.71
2000
1980
1960
1940
1920
4000
3500
3000
2500
2000
Volume (millions):
2,818,300,000
Jun 02 2014
Jun 23 2014
Jul 14 2014
Aug 04 2014
Aug 25 2014
Quantmod
32 / 74
Outline
Introduction
Time series in R
Quantmod
33 / 74
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
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"
Quantmod
34 / 74
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
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,minor.ticks=FALSE)
Guy Yollin (Copyright 2014)
Quantmod
35 / 74
[20000103/20140905]
80
Last 77.95
60
40
20
150
100
Volume (millions):
4,002,500
50
0
Jan 03 2000 Jan 02 2003
Guy Yollin (Copyright 2014)
Jan 03 2012
Quantmod
36 / 74
1993-09-30
1995-12-04
1999-03-22
2001-04-30
2005-10-24
SBUX.spl
0.5
0.5
0.5
0.5
0.5
class(spl)
## [1] "xts" "zoo"
Quantmod
37 / 74
2010-04-05
2010-08-02
2010-11-16
2011-02-07
2011-05-09
2011-08-08
2011-11-15
2012-02-06
2012-05-07
2012-08-06
2012-11-13
2013-02-05
2013-05-07
2013-08-06
2013-11-12
2014-02-04
2014-05-06
2014-08-05
[,1]
0.10
0.13
0.13
0.13
0.13
0.13
0.17
0.17
0.17
0.17
0.21
0.21
0.21
0.21
0.26
0.26
0.26
0.26
class(div)
## [1] "xts" "zoo"
Quantmod
38 / 74
Main arguments:
x
an OHLC object
use.Adjusted
Return value:
An object of the original class, with prices adjusted for splits and
dividends
Using use.Adjusted = TRUE will be less precise than the method that employs
actual split and dividend information
Guy Yollin (Copyright 2014)
Quantmod
39 / 74
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
SBUX.Open
5.5818405
5.6239146
5.5958652
5.6098899
5.7851990
6.0493313
SBUX.High
5.7711743
5.8155859
5.7548121
5.9885575
5.8436353
6.2526898
An article that describes how Yahoo calculates the adjusted close can
be found here:
http://help.yahoo.com/kb/index?locale=en_US&y=PROD_ACCT&page=content&id=SLN2311
Guy Yollin (Copyright 2014)
Quantmod
40 / 74
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
SBUX.Open
5.5818405
5.6239146
5.5958652
5.6098899
5.7851990
6.0493313
SBUX.High
5.7711743
5.8155859
5.7548121
5.9885575
5.8436353
6.2526898
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
SBUX.Open
5.5778102
5.6220603
5.5916081
5.6121309
5.7855854
6.0519385
SBUX.High
5.7670073
5.8136683
5.7504341
5.9909497
5.8440257
6.2553846
chartSeries(adj.exact,theme=whiteTheme,name="SBUX",minor.ticks=FALSE)
Guy Yollin (Copyright 2014)
Quantmod
41 / 74
[20000103/20140905]
80
Last 77.95
60
40
20
150
100
Volume (millions):
4,002,500
50
0
Jan 03 2000 Jan 02 2003
Guy Yollin (Copyright 2014)
Jan 03 2012
Quantmod
42 / 74
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
SBUX.Open
5.5818405
5.6239146
5.5958652
5.6098899
5.7851990
6.0493313
SBUX.High
5.7711743
5.8155859
5.7548121
5.9885575
5.8436353
6.2526898
SBUX.High
5.7711743
5.8155859
5.7548121
5.9885575
5.8436353
6.2526898
head(adj.exact)
##
##
##
##
##
##
##
2000-01-03
2000-01-04
2000-01-05
2000-01-06
2000-01-07
2000-01-10
SBUX.Open
5.5818405
5.6239146
5.5958652
5.6098899
5.7851990
6.0493313
Quantmod
43 / 74
http://research.stlouisfed.org/fred2/
Guy Yollin (Copyright 2014)
Quantmod
44 / 74
getSymbols('DTB3',src='FRED')
first(DTB3,'1 week')
##
DTB3
## 1954-01-04 1.33
last(DTB3,'1 week')
##
##
##
##
##
DTB3
2014-09-01
NA
2014-09-02 0.03
2014-09-03 0.03
2014-09-04 0.03
chartSeries(DTB3,theme="white",minor.ticks=FALSE)
Quantmod
45 / 74
[19540104/20140904]
Last 0.03
15
10
Jan 04 1954
Jan 02 1970
Jan 02 1985
Jan 03 2000
Sep 04 2014
Quantmod
46 / 74
Quandl
Tammer Kamel the founder of www.quandl.com wants to do to
Bloomberg what Wikipedia did to Britannica.
http://www.quandl.com/
Guy Yollin (Copyright 2014)
Quantmod
47 / 74
Quandl futures
Quantmod
48 / 74
Quandl.auth
Quandl.search
Authors:
Raymond McTaggart
Anonymous API calls are limited to 50 requests per day; signed up users receive an
authorization token that allows them to get 500 API calls per day; see
http://www.quandl.com/help/r
Guy Yollin (Copyright 2014)
Quantmod
49 / 74
Main arguments:
code
Dataset code on Quandl specified as a string
type
Type of data returned (raw, ts, zoo or xts)
start_date Start date
end_date
End date
collapse
Frequency of data
Return value:
time series data in the specified format
Guy Yollin (Copyright 2014)
Quantmod
50 / 74
Quantmod
51 / 74
Required data cleaning: replace zero values with NA and then use the
function na.approx to interpolate
Quantmod
52 / 74
[19830330/20140905]
Last 93.29
140
120
100
80
60
40
20
60
50
40
30
20
10
0
Volume (10,000s):
261,386
Mar 30 1983
Jan 02 1990
Jan 02 1998
Jan 03 2006
Jan 02 2014
Quantmod
53 / 74
Outline
Introduction
Time series in R
Quantmod
54 / 74
Quantmod
55 / 74
Description
Function
Description
stoch
stochastic oscillator
ADX
aroon
Aroon indicator
ATR
BBands
Bollinger bands
CCI
chaikinAD
Chaikin Acc/Dist
chaikinVolatility
Chaikin Volatility
ROC
rate of change
momentum
momentum indicator
CLV
CMF
CMO
SMA
EMA
DEMA
VWMA
volume weighted MA
VWAP
DonchianChannel
Donchian Channel
DPO
EMV
volatility
volatility estimators
MACD
MA converge/diverge
MFI
RSI
SAR
Parabolic Stop-and-Reverse
TDI
TRIX
VHF
williamsAD
Williams Acc/Dist
WPR
Williams % R
ZigZag
Quantmod
56 / 74
2014-08-22
2014-08-25
2014-08-26
2014-08-27
2014-08-28
2014-08-29
2014-09-02
2014-09-03
2014-09-04
2014-09-05
dn
76.411021
76.428539
76.476472
76.554083
76.552982
76.625643
76.656180
76.698147
76.724373
76.812251
mavg
77.528909
77.511574
77.485948
77.453339
77.457245
77.503182
77.521167
77.533500
77.541000
77.574667
up
78.646797
78.594608
78.495424
78.352595
78.361509
78.380720
78.386153
78.368853
78.357627
78.337082
pctB
0.40208802
0.67932318
0.75131112
0.77615115
0.68030579
0.67481749
0.51088649
0.26447097
0.22794613
0.50131162
chartSeries(SBUX,TA='addBBands();addBBands(draw="p");addVo()',
subset='2014',theme="white")
pctB =
Close LowerBand
UpperBand LowerBand
Quantmod
57 / 74
Bollinger bands
SBUX
[20140102/20140905]
Last 77.95
Bollinger Bands (20,2) [Upper/Lower]: 78.337/76.812
80
78
76
74
72
70
68
1.0
Bollinger %b (20,2):
0.501
0.5
0.0
15
Volume (millions):
4,002,500
10
5
Jan 02 2014
Mar 03 2014
May 01 2014
Jul 01 2014
Aug 01 2014
Sep 05 2014
Quantmod
58 / 74
macd
signal
2014-08-28 0.1152221277 0.079458718
2014-08-29 0.1195390995 0.087474794
2014-09-02 0.0876697703 0.087513789
2014-09-03 -0.0092470283 0.068161626
2014-09-04 -0.0470796190 0.045113377
2014-09-05 0.0051187401 0.037114450
chartSeries(SBUX, TA = "addMACD()",subset="2014",theme=whiteTheme)
Quantmod
59 / 74
[20140102/20140905]
80
Last 77.95
78
76
74
72
70
68
2
1
0
1
2
Jan 02 2014
Guy Yollin (Copyright 2014)
Mar 03 2014
May 01 2014
Jul 01 2014
Sep 02 2014
Quantmod
60 / 74
2014-08-28
2014-08-29
2014-09-02
2014-09-03
2014-09-04
2014-09-05
SBUX.Close.EMA.14
52.010352
52.010352
48.936808
43.189696
46.797814
53.577264
chartSeries(SBUX, TA = "addRSI()",subset="2013",theme=whiteTheme)
RSI = 100
RS =
100
1 + RS
average of up changes
average of down changes
Quantmod
61 / 74
[20130102/20131231]
Last 77.5472535857189
80
75
70
65
60
55
80
70
60
50
40
30
Jan 02 2013
Guy Yollin (Copyright 2014)
Apr 01 2013
Jul 01 2013
Oct 01 2013
Dec 31 2013
Quantmod
62 / 74
Quantmod
63 / 74
20130102 / 20131231
80
80
75
75
70
70
65
65
60
60
55
55
Jan
Jan 02 2013
Feb
Mar
Mar 01 2013
Apr
May
Jun
Jul
Aug
Sep
Oct
Nov
Nov 01 2013
Dec
Dec 31 2013
Quantmod
64 / 74
Outline
Introduction
Time series in R
Quantmod
65 / 74
Daily data
Intra-day data
xts
xts
Date
POSIXlt
Quantmod
66 / 74
Main arguments:
x
format
tz
Return value:
a POSIXlt object
Quantmod
67 / 74
Main arguments:
x
order.by
Return value:
an xts object
Quantmod
68 / 74
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
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
tm <- strptime(
paste(dat[,"Date"], sprintf("%04d",dat[,"Time"])),
format="%m/%d/%Y %H%M")
class(tm)
## [1] "POSIXlt" "POSIXt"
head(tm)
## [1] "2002-10-20 23:30:00 PDT" "2002-10-21 00:00:00 PDT" "2002-10-21 00:30:00 PDT"
## [4] "2002-10-21 01:00:00 PDT" "2002-10-21 01:30:00 PDT" "2002-10-21 02:00:00 PDT"
Quantmod
69 / 74
2007-01-01
2007-01-01
2007-01-01
2007-01-01
2007-01-01
2007-01-01
2007-01-01
17:30:00
18:00:00
18:30:00
19:00:00
19:30:00
20:00:00
20:30:00
Open
1.9649
1.9646
1.9645
1.9651
1.9651
1.9655
1.9653
High
1.9650
1.9648
1.9653
1.9652
1.9658
1.9657
1.9656
Low
1.9644
1.9641
1.9645
1.9647
1.9651
1.9650
1.9651
Close
1.9645
1.9644
1.9650
1.9650
1.9654
1.9654
1.9655
Quantmod
70 / 74
[20071224/20071226 23:30:00]
Last 1.98542
1.988
1.986
1.984
1.982
1.980
1.978
1.976
Dec 24 00:00
Dec 24 10:00
Dec 26 04:00
Dec 26 14:30
Dec 26 23:30
Quantmod
71 / 74
Quantmod
72 / 74
1.988
1.988
1.987
1.987
1.986
1.986
1.985
1.985
1.984
1.984
1.983
1.983
1.982
1.982
1.981
1.981
1.980
1.980
1.979
1.979
Crossover
bar
1.978
1.978
1.977
1.977
1.976
1.976
Dec 24 00:00
Dec 24 04:00
Dec 24 08:00
Dec 24 12:00
Dec 26 02:00
Dec 26 06:00
Dec 26 10:00
Dec 26 22:00
Quantmod
73 / 74
http://depts.washington.edu/compfin
Quantmod
74 / 74