Xts Quantmod Workshop
Transcript of Xts Quantmod Workshop
![Page 1: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/1.jpg)
Working with xts and quantmodLeveraging R with xts and quantmod for quantitative trading
R/Finance 2009: Applied Finance with RUniversity of Illinois at Chicago
Jeffrey A. Ryan [email protected]
R/Finance 2009 Workshop Presented on April 24, 2009
![Page 2: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/2.jpg)
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...
![Page 3: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/3.jpg)
xts: extensible time series
Data Classes
![Page 4: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/4.jpg)
xts: extensible time series
data.framedata.table
vectors
ts
mts
timeSeriesits irts
matrixfts
zoo
zooreg
tframe
Data Classes
![Page 5: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/5.jpg)
xts: extensible time series
data.framedata.table
vectors
ts
mts
timeSeriesits irts
matrixfts
zoo
zooreg
tframe
Time Classes
Data Classes
![Page 6: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/6.jpg)
xts: extensible time series
data.framedata.table
vectors
ts
mts
timeSeriesits irts
matrixfts
zoo
zooreg
tframe
chron POSIXct
yearmonDate POSIXlt
character
numeric
yearqtr timeDate
Time Classes
Data Classes
![Page 7: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/7.jpg)
xts: extensible time series
data.framedata.table
vectors
ts
mts
timeSeriesits irts
matrixfts
zoo
zooreg
tframe
Data Classes
chron POSIXct
yearmonDate POSIXlt
character
numeric
yearqtr timeDate
Time Classes
Sometimes, choice is bad for package developers and interoperability
![Page 8: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/8.jpg)
xts: extensible time series
The “solution” ?
![Page 9: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/9.jpg)
xts: extensible time series
add one more class of course...
![Page 10: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/10.jpg)
xts: extensible time series
Motivation (c. 2007)
Avid user of zoo
I still wanted a few features for trading...
• Additional series metadata• Require time-based indexing• Conversion/reconversion tools
• Natural R-like interface• Flexible and complete methods• S3!
![Page 11: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/11.jpg)
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
![Page 12: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/12.jpg)
xts: extensible time series
What’s inside an xts object?
Index Matrix+
Internally the storage is always a numeric vector!
Attr+
Can be of any class supported by matrix
Hidden attributes AND user attributes
.indexCLASS, .indexFORMAT,.indexTZ, .CLASS, index
![Page 13: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/13.jpg)
xts: extensible time series
What’s inside an xts object?
Index Matrix+
Internally the storage is always a numeric vector!
Attr+
Can be of any class supported by matrix
Hidden attributes AND user attributes
.indexCLASS, .indexFORMAT,.indexTZ, .CLASS, index
![Page 14: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/14.jpg)
xts: extensible time series
What’s inside an xts object?
Index Matrix+
Internally the storage is always a numeric vector!
Attr+
Can be of any class supported by matrix
Hidden attributes AND user attributes
.indexCLASS, .indexFORMAT,.indexTZ, .CLASS, index
![Page 15: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/15.jpg)
xts: extensible time series
What’s inside an xts object?
Index Matrix+
Internally the storage is always a numeric vector!
Attr+
Can be of any class supported by matrix
Hidden attributes AND user attributes
.indexCLASS, .indexFORMAT,.indexTZ, .CLASS, index
![Page 16: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/16.jpg)
xts: extensible time series
What’s inside an xts object?
Index Matrix+
Internally the storage is always a numeric vector!
Attr+
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
![Page 17: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/17.jpg)
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
![Page 18: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/18.jpg)
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
![Page 19: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/19.jpg)
xts: extensible time series
Create an xts object
> library(xts)Loading required package: zooxts now requires a valid TZ variable to be set your current TZ:America/Chicago
> x <- xts(rnorm(10), Sys.Date()+1:10)> x [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.81004932009-04-02 1.4152439
Load xts package
![Page 20: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/20.jpg)
xts: extensible time series
Create an xts object
> library(xts)Loading required package: zooxts now requires a valid TZ variable to be set your current TZ:America/Chicago
> x <- xts(rnorm(10), Sys.Date()+1:10)> x [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.81004932009-04-02 1.4152439
Create an xts object
![Page 21: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/21.jpg)
xts: extensible time series
Indexing by time
> x[ index(x) >= as.Date("2009-03-28") & index(x) <= + as.Date("2009-04-01") ] [,1]2009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.8100493
> x["20090328/20090401"] [,1]2009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.8100493
Index using standard tools (still works)
![Page 22: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/22.jpg)
xts: extensible time series
Indexing by time
> x[ index(x) >= as.Date("2009-03-28") & index(x) <= + as.Date("2009-04-01") ] [,1]2009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.8100493
> x["20090328/20090401"] [,1]2009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.8100493
Index via ISO-style with xts
![Page 23: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/23.jpg)
xts: extensible time series
Indexing by time
> x["2009"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.81004932009-04-02 1.4152439
> x["200904"] [,1]2009-04-01 0.81004932009-04-02 1.4152439
> x["20090301/200903"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.2152293
All of 2009
![Page 24: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/24.jpg)
xts: extensible time series
Indexing by time
> x["2009"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.81004932009-04-02 1.4152439
> x["200904"] [,1]2009-04-01 0.81004932009-04-02 1.4152439
> x["20090301/200903"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.2152293
All of April 2009
![Page 25: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/25.jpg)
xts: extensible time series
Indexing by time
> x["2009"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.21522932009-04-01 0.81004932009-04-02 1.4152439
> x["200904"] [,1]2009-04-01 0.81004932009-04-02 1.4152439
> x["20090301/200903"] [,1]2009-03-24 0.35547882009-03-25 1.28126332009-03-26 0.12688332009-03-27 -0.69451462009-03-28 -0.39361482009-03-29 -0.19388402009-03-30 0.23685762009-03-31 -1.2152293
From the first March to the end of March
![Page 26: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/26.jpg)
xts: extensible time seriesAll subsetting is via a binary search algorithm. F-A-S-T!
> str(x10m) # 10 million observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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 observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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['20090323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['20090323']) user system elapsed 0.006 0.001 0.006
> system.time(x10m[index(x10m) >= as.POSIXct('2009-03-23 16:19:00') & index(x10m) <= as.POSIXct('2009-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832
![Page 27: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/27.jpg)
xts: extensible time seriesAll subsetting is via a binary search algorithm. F-A-S-T!
> str(x10m) # 10 million observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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 observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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['20090323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['20090323']) user system elapsed 0.006 0.001 0.006
> system.time(x10m[index(x10m) >= as.POSIXct('2009-03-23 16:19:00') & index(x10m) <= as.POSIXct('2009-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832
![Page 28: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/28.jpg)
xts: extensible time seriesAll subsetting is via a binary search algorithm. F-A-S-T!
> str(x10m) # 10 million observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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 observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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['20090323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['20090323']) user system elapsed 0.006 0.001 0.006
> system.time(x10m[index(x10m) >= as.POSIXct('2009-03-23 16:19:00') & index(x10m) <= as.POSIXct('2009-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832
Identical speed!
![Page 29: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/29.jpg)
xts: extensible time seriesAll subsetting is via a binary search algorithm. F-A-S-T!
> str(x10m) # 10 million observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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 observationsAn 'xts' object from 2009-03-23 16:19:00 to 2009-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['20090323']) user system elapsed 0.006 0.001 0.006 > system.time(x100k['20090323']) user system elapsed 0.006 0.001 0.006
> system.time(x10m[index(x10m) >= as.POSIXct('2009-03-23 16:19:00') & index(x10m) <= as.POSIXct('2009-03-23 23:59:58')]) user system elapsed 1.457 1.372 2.832
![Page 30: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/30.jpg)
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
![Page 31: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/31.jpg)
xts: extensible time series
Additional xts tools
to.period, period.apply, endpoints, timeBasedRange, try.xts, reclass, Reclass
![Page 32: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/32.jpg)
xts: extensible time series
...in development
![Page 33: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/33.jpg)
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
![Page 34: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/34.jpg)
xts: extensible time series
Parallel processingperiod.apply
runSum, runCov, runSD, etc. (moving from TTR)
![Page 35: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/35.jpg)
xts: extensible time series
Parallel processingperiod.apply
runSum, runCov, runSD, etc.
Multiple index supportdimensional attributes
![Page 36: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/36.jpg)
xts: extensible time series
Parallel processingperiod.apply
runSum, runCov, runSD, etc.
Multiple index supportdimensional attributes
Tighter zoo integrationBackport C code into zoo
![Page 37: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/37.jpg)
quantmod
80859095
100105
AAPL [2008−12−01/2009−03−23]
Dec 012008
Dec 152008
Dec 292008
Jan 122009
Jan 262009
Feb 092009
Feb 232009
Mar 092009
Mar 232009
![Page 38: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/38.jpg)
quantmod
quantmod was envisioned to be a rapid prototyping environment in R to facilitate quantitative modeling,
testing, and trading
![Page 39: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/39.jpg)
quantmod
Data. Visualization. Modeling.
![Page 40: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/40.jpg)
quantmod
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
![Page 41: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/41.jpg)
quantmod
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
![Page 42: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/42.jpg)
quantmod
Data. Visualization. Modeling.
getSymbols
csv Rdata MySQL
SQLite google yahoo
InteractiveBrokers
FRED oanda
![Page 43: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/43.jpg)
quantmod
Data. Visualization. Modeling.
getSymbols
getSymbols is the top level function that dispatches to custom methods based on user direction
setSymbolLookupgetSymbolLookupsaveSymbolLookuploadSymbolLookup
![Page 44: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/44.jpg)
quantmod
Data. Visualization. Modeling.
getSymbols
getSymbols behave like base::load by assigning objects into the user’s workspace (.GlobalEnv)
![Page 45: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/45.jpg)
quantmod
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.
![Page 46: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/46.jpg)
quantmod
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
![Page 47: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/47.jpg)
quantmod
Data. Visualization. Modeling.
getSymbols
getSymbols(“AAPL”)getSymbols(“AAPL;SBUX”)
getSymbols(“USD/EUR”,src= “oanda”)
![Page 48: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/48.jpg)
quantmod
Data. Visualization. Modeling.
getSymbols
getSymbols(“AAPL”)getSymbols(“AAPL;SBUX”)
getSymbols(“USD/EUR”,src= “oanda”)
Additional data wrappers:
getDividends, getQuote, getSplits,getFX, getMetals, getOptionChain
![Page 49: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/49.jpg)
quantmod
Data. Visualization. Modeling.
Interactive, highly customizable financial charting in R
![Page 50: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/50.jpg)
quantmod
Data. Visualization. Modeling.
^gspc
Time
price
2009−02−02 2009−02−14 2009−02−27 2009−03−11 2009−03−23
700
750
800
850
Basic OHLC chart from tseries
![Page 51: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/51.jpg)
quantmod
Data. Visualization. Modeling.
700
750
800
850
GSPC [2009−02−02/2009−03−23]
Feb 022009
Feb 092009
Feb 172009
Feb 232009
Mar 022009
Mar 092009
Mar 162009
Mar 232009
candleChart(GSPC, subset='200902/', theme='white', TA=NULL)
![Page 52: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/52.jpg)
quantmod
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)
![Page 53: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/53.jpg)
quantmod
Data. Visualization. Modeling.
The Basics
70
80
90
100
110
120
130
IBM [2007−01−03/2009−03−23]
Last 98.71
Volume (millions):12,407,200
51015202530
Jan 032007
Apr 022007
Jul 022007
Oct 012007
Jan 022008
Apr 012008
Jul 012008
Oct 012008
Jan 022009
Mar 232009
> chartSeries(IBM)
![Page 54: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/54.jpg)
quantmod
Data. Visualization. Modeling.
The Basics
> addBBands()
70
80
90
100
110
120
130
IBM [2007−01−03/2009−03−23]
Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Volume (millions):12,407,200
51015202530
Jan 032007
Apr 022007
Jul 022007
Oct 012007
Jan 022008
Apr 012008
Jul 012008
Oct 012008
Jan 022009
Mar 232009
![Page 55: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/55.jpg)
quantmod
Data. Visualization. Modeling.
The Basics
> addMACD(32,50,12)
70
80
90
100
110
120
130
IBM [2007−01−03/2009−03−23]Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Volume (millions):12,407,200
51015202530
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−5
0
5
Jan 032007
Mar 012007
May 012007
Jul 022007
Sep 042007
Nov 012007
Jan 022008
Mar 032008
May 012008
Jul 012008
Sep 022008
Nov 032008
Jan 022009
Mar 022009
![Page 56: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/56.jpg)
quantmod
Data. Visualization. Modeling.
The Basics
> reChart(subset= “2009”,theme= “white”, type= “candles”)
85
90
95
IBM [2009−01−02/2009−03−23]Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Volume (millions):12,407,200
10
15
20
25
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−3−2−1
0123
Jan 022009
Jan 122009
Jan 202009
Jan 262009
Feb 022009
Feb 092009
Feb 172009
Feb 232009
Mar 022009
Mar 092009
Mar 162009
Mar 232009
![Page 57: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/57.jpg)
quantmod
Data. Visualization. Modeling.
Inside chartSeries
chartSeries
chobTA
chob(chart object)
addTA 85
90
95
IBM [2009−01−02/2009−03−23]
Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−3−2−10123
Jan 022009
Jan 202009
Feb 022009
Feb 172009
Mar 022009
Mar 162009
80
100
120
140
160
180
200
AAPL [2007−01−03/2009−03−23]
Last 107.66
Volume (millions):23,799,900
20406080
100120
Jan 032007
May 012007
Sep 042007
Jan 022008
May 012008
Sep 022008
Jan 022009
![Page 58: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/58.jpg)
quantmod
Data. Visualization. Modeling.
Inside chartSeries
chartSeries
chobTA
chob(chart object)
addTA 85
90
95
IBM [2009−01−02/2009−03−23]
Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−3−2−10123
Jan 022009
Jan 202009
Feb 022009
Feb 172009
Mar 022009
Mar 162009
80
100
120
140
160
180
200
AAPL [2007−01−03/2009−03−23]
Last 107.66
Volume (millions):23,799,900
20406080
100120
Jan 032007
May 012007
Sep 042007
Jan 022008
May 012008
Sep 022008
Jan 022009
![Page 59: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/59.jpg)
quantmod
Data. Visualization. Modeling.
Inside chartSeries
chartSeries
chobTA
chob(chart object)
addTA 85
90
95
IBM [2009−01−02/2009−03−23]
Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−3−2−10123
Jan 022009
Jan 202009
Feb 022009
Feb 172009
Mar 022009
Mar 162009
80
100
120
140
160
180
200
AAPL [2007−01−03/2009−03−23]
Last 107.66
Volume (millions):23,799,900
20406080
100120
Jan 032007
May 012007
Sep 042007
Jan 022008
May 012008
Sep 022008
Jan 022009
![Page 60: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/60.jpg)
quantmod
Data. Visualization. Modeling.
Inside chartSeries
chartSeries
chobTA
chob(chart object)
addTA 85
90
95
IBM [2009−01−02/2009−03−23]
Last 98.71Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Moving Average Convergence Divergence (12,26,9):MACD: 1.418Signal: 0.392
−3−2−10123
Jan 022009
Jan 202009
Feb 022009
Feb 172009
Mar 022009
Mar 162009
80
100
120
140
160
180
200
AAPL [2007−01−03/2009−03−23]
Last 107.66
Volume (millions):23,799,900
20406080
100120
Jan 032007
May 012007
Sep 042007
Jan 022008
May 012008
Sep 022008
Jan 022009
Drawn by chartSeries.chob
![Page 61: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/61.jpg)
quantmod
Data. Visualization. Modeling.
Extending chartSeries
![Page 62: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/62.jpg)
GMMAGuppy Multiple Moving Average
(with newTA)
quantmod
Data. Visualization. Modeling.
![Page 63: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/63.jpg)
> # 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)+ }>
quantmod
Data. Visualization. Modeling.
![Page 64: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/64.jpg)
> # 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”
quantmod
Data. Visualization. Modeling.
![Page 65: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/65.jpg)
> # 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”
candleChart(AAPL); addGuppy()
quantmod
Data. Visualization. Modeling.
![Page 66: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/66.jpg)
80
100
120
140
160
180
AAPL [2008−08−01/2008−12−01]Last 88.93
Volume (millions):32,991,700
20
40
60
80
GMMA () :3 : 90.6955 : 90.6258 : 90.55610 : 90.77112 : 91.11715 : 91.77430 : 96.56635 : 98.59340 : 100.72645 : 102.90050 : 105.06660 : 109.241
80
100
120
140
160
180
Aug 012008
Aug 112008
Aug 182008
Aug 252008
Sep 022008
Sep 152008
Sep 222008
Sep 292008
Oct 062008
Oct 132008
Oct 202008
Oct 272008
Nov 032008
Nov 102008
Nov 172008
Nov 242008
quantmod
Data. Visualization. Modeling.
![Page 67: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/67.jpg)
80
100
120
140
160
180
AAPL [2008−08−01/2008−12−01]Last 88.93
Volume (millions):32,991,700
20
40
60
80
GMMA () :3 : 90.6955 : 90.6258 : 90.55610 : 90.77112 : 91.11715 : 91.77430 : 96.56635 : 98.59340 : 100.72645 : 102.90050 : 105.06660 : 109.241
80
100
120
140
160
180
Aug 012008
Aug 112008
Aug 182008
Aug 252008
Sep 022008
Sep 152008
Sep 222008
Sep 292008
Oct 062008
Oct 132008
Oct 202008
Oct 272008
Nov 032008
Nov 102008
Nov 172008
Nov 242008
quantmod
Data. Visualization. Modeling.
addGuppy(on=-1, col=c(rep(“blue”,6), rep(“black”,6)))
![Page 68: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/68.jpg)
80
100
120
140
160
180
AAPL [2008−08−01/2008−12−01]Last 88.93
Volume (millions):32,991,700
20
40
60
80
GMMA () :3 : 90.6955 : 90.6258 : 90.55610 : 90.77112 : 91.11715 : 91.77430 : 96.56635 : 98.59340 : 100.72645 : 102.90050 : 105.06660 : 109.241
80
100
120
140
160
180
Aug 012008
Aug 112008
Aug 182008
Aug 252008
Sep 022008
Sep 152008
Sep 222008
Sep 292008
Oct 062008
Oct 132008
Oct 202008
Oct 272008
Nov 032008
Nov 102008
Nov 172008
Nov 242008
quantmod
Data. Visualization. Modeling.
addGuppy(on=-1, col=c(rep(“blue”,6), rep(“black”,6)))
on arg selects panel
![Page 69: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/69.jpg)
quantmod
Data. Visualization. Modeling.
80
90
100
110
120
80
90
100
110
120
AAPL [2009−01−02/2009−04−23]
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
Volume (millions):33,719,600
20
30
40
50
60
Jan 022009
Jan 202009
Feb 022009
Feb 172009
Mar 022009
Mar 162009
Mar 302009
Apr 132009
Apr 232009
![Page 70: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/70.jpg)
quantmod
Data. Visualization. Modeling.
chartSeries3d
Jan 022008 Feb 01
2008 Mar 032008 Apr 01
2008 May 012008 Jun 02
2008 Jul 012008 Aug 01
2008 Sep 022008 Oct 01
2008 Nov 032008 Dec 01
2008 Dec 312008
1mo3mo
6mo1yr
2yr3yr
5yr7yr
10yr20yr
30yr0%
1%
2%
3%
4%
5%
Yield Curve 2008 −−− Daily
![Page 71: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/71.jpg)
quantmod
Data. Visualization. Modeling.
chartSeries3d
chartSeries functionality to 3d/persp style graphicsautomatic time axis annotation
“interactive”, reChart, rotChart, etc.
![Page 72: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/72.jpg)
quantmod
Data. Visualization. Modeling.
chartSeries + chartSeries3d
0.00.51.01.52.02.53.0
T[, "3mo"] [2008−01−02/2008−12−31]Last 0.11
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
1.0
1.5
2.0
2.5
3.0T[, "2yr"] [2008−01−02/2008−12−31]
Last 0.76
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
1.5
2.0
2.5
3.0
3.5
T[, "5yr"] [2008−01−02/2008−12−31]Last 1.55
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
2.0
2.5
3.0
3.5
4.0
T[, "10yr"] [2008−01−02/2008−12−31]Last 2.25
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
Jan 022008 Feb 01
2008 Mar 032008 Apr 01
2008 May 012008 Jun 02
2008 Jul 012008 Aug 01
2008 Sep 022008 Oct 01
2008 Nov 032008 Dec 01
2008 Dec 312008
1mo
3mo
6mo
1yr2yr
3yr5yr
7yr10yr
20yr30yr0%
1%
2%
3%
4%
5%
Yield Curve 2008 −−− Daily
![Page 73: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/73.jpg)
quantmod
Data. Visualization. Modeling.
0.00.51.01.52.02.53.0
T[, "3mo"] [2008−01−02/2008−12−31]Last 0.11
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
1.0
1.5
2.0
2.5
3.0T[, "2yr"] [2008−01−02/2008−12−31]
Last 0.76
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
1.5
2.0
2.5
3.0
3.5
T[, "5yr"] [2008−01−02/2008−12−31]Last 1.55
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
2.0
2.5
3.0
3.5
4.0
T[, "10yr"] [2008−01−02/2008−12−31]Last 2.25
Jan 022008
Apr 012008
Jun 022008
Aug 012008
Oct 012008
Dec 312008
Jan 022008 Feb 01
2008 Mar 032008 Apr 01
2008 May 012008 Jun 02
2008 Jul 012008 Aug 01
2008 Sep 022008 Oct 01
2008 Nov 032008 Dec 01
2008 Dec 312008
1mo
3mo
6mo
1yr2yr
3yr5yr
7yr10yr
20yr30yr0%
1%
2%
3%
4%
5%
Yield Curve 2008 −−− Daily
options (risk and volatility)
limit order book data
![Page 74: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/74.jpg)
quantmod
attachSymbols
New functionality to extend upon getSymbols
![Page 75: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/75.jpg)
quantmod
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
![Page 76: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/76.jpg)
quantmod
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"
![Page 77: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/77.jpg)
quantmod
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
![Page 78: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/78.jpg)
quantmod
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"
![Page 79: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/79.jpg)
quantmod
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" ...
![Page 80: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/80.jpg)
quantmod
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" ...
7406 symbols are available
![Page 81: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/81.jpg)
quantmod
attachSymbols
Example: All US Equity symbols on demand.
> str(E.A)An 'xts' object from 2007-01-03 to 2009-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: "2009-03-24 10:59:14"
![Page 82: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/82.jpg)
quantmod
attachSymbols
Example: All US Equity symbols on demand.
> str(E.A)An 'xts' object from 2007-01-03 to 2009-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: "2009-03-24 10:59:14"
First access loads data
![Page 83: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/83.jpg)
quantmod
attachSymbols
Example: All US Equity symbols on demand.
> system.time(E.AKAM) user system elapsed 0.032 0.004 0.267
download from Yahoo
![Page 84: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/84.jpg)
quantmod
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
![Page 85: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/85.jpg)
quantmod
attachSymbols
Two Methods to Cache
after first access, objects are
cached to disk.makeActiveBinding
Disk
after first access objects remain in
memorydelayedAssign
Memory
![Page 86: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/86.jpg)
quantmod
attachSymbols
Custom DDB methods
DDBobject
BindingFunction
Attachsymbols
![Page 87: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/87.jpg)
quantmod
attachSymbols
Custom DDB methods
example: DDB:Yahoo
> DDB_Yahoo()> # creates DDB object of all US Equity Symbols
![Page 88: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/88.jpg)
quantmod
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"
![Page 89: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/89.jpg)
quantmod
attachSymbols
Custom DDB methods
example: DDB:Yahoo
> attachSymbols()> # “binds” symbols to functions to load/reload
![Page 90: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/90.jpg)
quantmod
attachSymbols
Custom DDB methods
example: DDB:Yahoo
> attachSymbols()> # “binds” symbols to functions to load/reload
A few details...
![Page 91: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/91.jpg)
quantmod
attachSymbols
attachSymbols attachSymbols.yahoo create.binding
![Page 92: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/92.jpg)
quantmod
attachSymbols
attachSymbols attachSymbols.yahoo create.binding
> attachSymbolsfunction (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>
![Page 93: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/93.jpg)
quantmod
attachSymbols
attachSymbols attachSymbols.yahoo create.binding
> attachSymbolsfunction (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>
![Page 94: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/94.jpg)
quantmod
attachSymbols
attachSymbols attachSymbols.yahoo create.binding
> quantmod:::attachSymbols.yahoofunction (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>
![Page 95: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/95.jpg)
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>
![Page 96: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/96.jpg)
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>
![Page 97: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/97.jpg)
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())
![Page 98: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/98.jpg)
quantmod
Future Work
Integration of trading/testing with blotter & PerformanceAnalytics package
More data methods, easier to extend
specifyModel - buildModel - tradeModel work
![Page 99: Xts Quantmod Workshop](https://reader036.fdocuments.in/reader036/viewer/2022081717/54fd61cb4a7959fc798b459d/html5/thumbnails/99.jpg)
Presented by Jeffrey A. Ryan [email protected]
More Information
www.insightalgo.com www.quantmod.com
www.quantmod.com/RFinance2009