Escolar Documentos
Profissional Documentos
Cultura Documentos
R/Finance 2009 Workshop Presented on April 24, 2009 R/Finance 2009: Applied Finance with R University of Illinois at Chicago
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...
fts
Data Classes
matrix
mts
tframe
data.table its
data.frame
timeSeries
ts
zoo
irts
vectors
zooreg
fts
Data Classes
matrix
mts
tframe
data.table its
data.frame
timeSeries
ts
zoo
irts
vectors
Time Classes
zooreg
fts
Data Classes
matrix
mts
tframe
data.table its
data.frame
timeSeries
chron yearmon
ts
zoo
irts
vectors
Time Classes
fts
Data Classes
matrix
mts
tframe
ts irts Sometimes, choice is timeSeries bad for package zooreg vectors developers and interoperability character chron POSIXct Time Classes Date numeric POSIXlt yearmon timeDate yearqtr
data.table its
data.frame
zoo
The solution ?
xts: extensible time series Motivation (c. 2007) Avid user of zoo
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
Matrix
Attr
Matrix
Attr
Matrix
Attr
Matrix
Attr
Matrix
Attr
source: http://en.wikipedia.org/ISO_8601
> x <- xts(rnorm(10), Sys.Date()+1:10) >x [,1] 2009-03-24 0.3554788 2009-03-25 1.2812633 2009-03-26 0.1268833 2009-03-27 -0.6945146 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 2009-04-02 1.4152439
> x[ index(x) >= as.Date("2009-03-28") & index(x) <= + as.Date("2009-04-01") ] [,1] 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 > x["20090328/20090401"] [,1] 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493
> x[ index(x) >= as.Date("2009-03-28") & index(x) <= + as.Date("2009-04-01") ] [,1] 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 > x["20090328/20090401"] [,1] 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493
> x["2009"] [,1] 2009-03-24 0.3554788 2009-03-25 1.2812633 2009-03-26 0.1268833 2009-03-27 -0.6945146 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 2009-04-02 1.4152439
All of 2009
> x["2009"] [,1] 2009-03-24 0.3554788 2009-03-25 1.2812633 2009-03-26 0.1268833 2009-03-27 -0.6945146 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 2009-04-02 1.4152439
> x["2009"] [,1] 2009-03-24 0.3554788 2009-03-25 1.2812633 2009-03-26 0.1268833 2009-03-27 -0.6945146 2009-03-28 -0.3936148 2009-03-29 -0.1938840 2009-03-30 0.2368576 2009-03-31 -1.2152293 2009-04-01 0.8100493 2009-04-02 1.4152439
Identical speed!
> 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
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...
...in development
XTS (disk)
xts (memory)
xtsDB
Parallel processing
period.apply runSum, runCov, runSD, etc. (moving from TTR)
Parallel processing
period.apply runSum, runCov, runSD, etc.
Parallel processing
period.apply runSum, runCov, runSD, etc.
AAPL
105 100 95 90 85 80 Dec 01 2008 Dec 15 2008 Dec 29 2008 Jan 12 2009 Jan 26 2009
[20081201/20090323]
Feb 09 2009
Feb 23 2009
Mar 09 2009
Mar 23 2009
quantmod
quantmod
quantmod was envisioned to be a rapid prototyping environment in R to facilitate quantitative modeling, testing, and trading
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
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
getSymbols
csv SQLite Interactive Brokers Rdata google FRED MySQL yahoo oanda
getSymbols
getSymbols is the top level function that dispatches to custom methods based on user direction setSymbolLookup getSymbolLookup saveSymbolLookup loadSymbolLookup
getSymbols
getSymbols behave like base::load by assigning objects into the users workspace (.GlobalEnv)
getSymbols
getSymbols behave like base::load by assigning objects into the users workspace (.GlobalEnv) Rationale: when dealing with potentially dozens of symbols interactively, it is redundant to have to manually assign each. Also facilitates multiple requests.
quantmod Data.
getSymbols behave like base::load by assigning objects into the users workspace (.GlobalEnv) getSymbols (devel) now returns all symbols in an environment! loadSymbols will be available to directly replace the previous getSymbols behavior
D E T A D P
Visualization.
Modeling.
getSymbols
getSymbols
getSymbols(AAPL) getSymbols(AAPL;SBUX) getSymbols(USD/EUR,src= oanda)
getSymbols
getSymbols(AAPL) getSymbols(AAPL;SBUX) getSymbols(USD/EUR,src= oanda) Additional data wrappers: getDividends, getQuote, getSplits, getFX, getMetals, getOptionChain
Modeling.
price
700 20090202
750
800
850
20090214
20090227 Time
20090311
20090323
quantmod Data.
GSPC
850
Visualization.
Modeling.
[20090202/20090323]
800
750
700
Feb 02 2009
Feb 09 2009
Feb 17 2009
Feb 23 2009
Mar 02 2009
Mar 09 2009
Mar 16 2009
Mar 23 2009
Requirements
Fast rendering (base plotting tools) Interactive and scriptable Work with all timeseries classes Minimal commands Highly customizable Full technical analysis support (via TTR)
Modeling.
[20070103/20090323]
Jan 03 2007
Apr 02 2007
Jul 02 2007
Oct 01 2007
Jan 02 2008
Apr 01 2008
Jul 01 2008
Oct 01 2008
Jan 02 2009
Mar 23 2009
> chartSeries(IBM)
Modeling.
[20070103/20090323]
Jan 03 2007
Apr 02 2007
Jul 02 2007
Oct 01 2007
Jan 02 2008
Apr 01 2008
Jul 01 2008
Oct 01 2008
Jan 02 2009
Mar 23 2009
> addBBands()
quantmod Data.
IBM
130 120 110 100 90 80 70 30 25 20 15 10 5 5 0 5 Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220
Modeling.
[20070103/20090323]
Jan 03 2007
Mar 01 2007
May 01 2007
Jul 02 2007
Sep 04 2007
Nov 01 2007
Jan 02 2008
Mar 03 2008
May 01 2008
Jul 01 2008
Sep 02 2008
Nov 03 2008
Jan 02 2009
Mar 02 2009
> addMACD(32,50,12)
quantmod Data.
IBM
Last 98.71 Bollinger Bands (20,2) [Upper/Lower]: 95.670/83.220 95
Modeling.
[20090102/20090323]
90
85
25 20 15 10 3 2 1 0 1 2 3
Jan 02 2009
Jan 12 2009
Jan 20 2009
Jan 26 2009
Feb 02 2009
Feb 09 2009
Feb 17 2009
Feb 23 2009
Mar 02 2009
Mar 09 2009
Mar 16 2009
Mar 23 2009
Inside chartSeries
[20070103/20090323]
Jan 03 2007
May 01 2007
Sep 04 2007
Jan 02 2008
May 01 2008
Sep 02 2008
Jan 02 2009
IBM
[20090102/20090323]
addTA
chobTA
90
85 3 2 1 0 1 2 3
Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 2009 Jan 20 2009 Feb 02 2009 Feb 17 2009 Mar 02 2009 Mar 16 2009
Inside chartSeries
[20070103/20090323]
Jan 03 2007
May 01 2007
Sep 04 2007
Jan 02 2008
May 01 2008
Sep 02 2008
Jan 02 2009
IBM
[20090102/20090323]
addTA
chobTA
90
85 3 2 1 0 1 2 3
Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 2009 Jan 20 2009 Feb 02 2009 Feb 17 2009 Mar 02 2009 Mar 16 2009
Inside chartSeries
[20070103/20090323]
Jan 03 2007
May 01 2007
Sep 04 2007
Jan 02 2008
May 01 2008
Sep 02 2008
Jan 02 2009
IBM
[20090102/20090323]
addTA
chobTA
90
85 3 2 1 0 1 2 3
Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 2009 Jan 20 2009 Feb 02 2009 Feb 17 2009 Mar 02 2009 Mar 16 2009
Inside chartSeries
[20070103/20090323]
Jan 03 2007
May 01 2007
Sep 04 2007
Jan 02 2008
May 01 2008
Sep 02 2008
Jan 02 2009
IBM
[20090102/20090323]
addTA
chobTA
90
85 3 2 1 0 1 2 3
Moving Average Convergence Divergence (12,26,9): MACD: 1.418 Signal: 0.392 Jan 02 2009 Jan 20 2009 Feb 02 2009 Feb 17 2009 Mar 02 2009 Mar 16 2009
Drawn by chartSeries.chob
Extending chartSeries
> # 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 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()
> # 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
> # 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.
AAPL
180 Last 88.93 160
Visualization.
Modeling.
[20080801/20081201]
140
120
100
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 2008 Aug 11 2008 Aug 18 2008 Aug 25 2008 Sep 02 2008 Sep 15 2008 Sep 22 2008 Sep 29 2008 Oct 06 2008 Oct 13 2008 Oct 20 2008 Oct 27 2008 Nov 03 2008 Nov 10 2008 Nov 17 2008 Nov 24 2008
quantmod Data.
AAPL
180 Last 88.93 160
Visualization.
Modeling.
[20080801/20081201]
140
120
100
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 2008 Aug 11 2008 Aug 18 2008 Aug 25 2008 Sep 02 2008 Sep 15 2008 Sep 22 2008 Sep 29 2008 Oct 06 2008 Oct 13 2008 Oct 20 2008 Oct 27 2008 Nov 03 2008 Nov 10 2008 Nov 17 2008 Nov 24 2008
quantmod Data.
AAPL
180 Last 88.93 160
Visualization.
Modeling.
[20080801/20081201]
140
120
100
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 2008 Aug 11 2008 Aug 18 2008 Aug 25 2008 Sep 02 2008 Sep 15 2008 Sep 22 2008 Sep 29 2008 Oct 06 2008 Oct 13 2008 Oct 20 2008 Oct 27 2008 Nov 03 2008 Nov 10 2008 Nov 17 2008 Nov 24 2008
quantmod Data.
AAPL
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 120
Visualization.
Modeling.
[20090102/20090423]
110
100
90
80 60 50 40 30 20 Jan 02 2009 Jan 20 2009 Feb 02 2009 Feb 17 2009 Mar 02 2009 Mar 16 2009 Mar 30 2009 Apr 13 2009 Apr 23 2009 Volume (millions): 33,719,600
Modeling.
Feb 01 2008
Mar 03 2008
Apr 01 2008
May 01 2008
Jun 02 2008
Jul 01 2008
Aug 01 2008
Sep 02 2008
Oct 01 2008
Nov 03 2008
Dec 01 2008
Dec 31 2008
2yr
3yr
5yr
7yr
quantmod Data. Visualization. chartSeries3d chartSeries functionality to 3d/persp style graphics automatic time axis annotation interactive, reChart, rotChart, etc. Modeling.
chartSeries + chartSeries3d
T[, "3mo"]
3.0 2.5 2.0 1.5 1.0 0.5 0.0 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008 1.5 1.0 Last 0.11
[20080102/20081231]
3.0 2.5 2.0
T[, "2yr"]
Last 0.76
[20080102/20081231]
3.5 3.0 2.5 2.0
T[, "5yr"]
Last 1.55
[20080102/20081231]
4.0 3.5 3.0 2.5
T[, "10yr"]
Last 2.25
[20080102/20081231]
1.5 2.0 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008
5%
4%
3%
2%
1%
Jan 02 2008
Feb 01 2008
0% 30yr Mar 03 2008 20yr Apr 01 2008 10yr May 01 2008 7yr Jun 02 2008 5yr Jul 01 2008 3yr Aug 01 2008 2yr Sep 02 2008 1yr Oct 01 2008 6mo Nov 03 2008 Dec 01 2008 3mo Dec 31 2008 1mo
T[, "3mo"]
3.0 2.5 2.0 1.5 1.0 0.5 0.0 Jan 02 2008 Apr 01 2008 Last 0.11
[20080102/20081231]
3.0 2.5 2.0 1.5 1.0
T[, "2yr"]
Last 0.76
[20080102/20081231]
3.5 3.0 2.5 2.0
T[, "5yr"]
Last 1.55
[20080102/20081231]
4.0 3.5 3.0 2.5
T[, "10yr"]
Last 2.25
[20080102/20081231]
1.5 2.0 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008 Jan 02 2008 Apr 01 2008 Jun 02 Aug 01 Oct 01 2008 2008 2008 Dec 31 2008
Dec 31 2008
5%
4%
3%
2%
1%
0% 30yr 20yr
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
> str(ls("DDB:Yahoo")) chr [1:7406] "E.A" "E.AA" "E.AAC" "E.AACC" "E.AAI" "E.AAII" ...
> str(ls("DDB:Yahoo")) chr [1:7406] "E.A" "E.AA" "E.AAC" "E.AACC" "E.AAI" "E.AAII" ...
Memory
after rst access objects remain in memory
delayedAssign
DDB object
Binding Function
Attach symbols
quantmod attachSymbols Custom DDB methods example: DDB:Yahoo > DDB_Yahoo() > # creates DDB object of all US Equity Symbols
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"
quantmod attachSymbols Custom DDB methods example: DDB:Yahoo > attachSymbols() > # binds symbols to functions to load/reload
quantmod attachSymbols Custom DDB methods example: DDB:Yahoo > attachSymbols() > # binds symbols to functions to load/reload
A few details...
> attachSymbols function (DB = DDB_Yahoo(), pos = 2, prex = NULL, postx = NULL, mem.cache = TRUE, le.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, prex = prex, postx = postx, mem.cache = mem.cache, le.cache = le.cache, cache.dir = cache.dir)) } <environment: namespace:quantmod>
> attachSymbols function (DB = DDB_Yahoo(), pos = 2, prex = NULL, postx = NULL, mem.cache = TRUE, le.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, prex = prex, postx = postx, mem.cache = mem.cache, le.cache = le.cache, cache.dir = cache.dir)) } <environment: namespace:quantmod>
> quantmod:::attachSymbols.yahoo function (DB, pos, prex, postx, mem.cache, le.cache, cache.dir, ...) { attach(NULL, pos = pos, name = DB$name) rsym <- function(x) gsub("_", "-", x, perl = TRUE) lsym <- function(x) paste(prex, as.character(x), postx, sep = "") invisible(sapply(DB$db, create.binding, lsym = lsym, rsym = rsym, mem.cache = mem.cache, le.cache = le.cache, cache.dir = cache.dir, envir = DB$name)) } <environment: namespace:quantmod>
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())
quantmod 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
www.quantmod.com/RFinance2009