85
quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan [email protected] www.quantmod.com/Columbia2008

quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan [email protected]

Embed Size (px)

Citation preview

Page 1: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

quantmod+

xts

Computational Finance with RColumbia University, New York

December 4, 2008

Presented by Jeffrey A. Ryan [email protected]

www.quantmod.com/Columbia2008

Page 2: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

Original Purpose:Provide a unified interface to R for quantitative traders

who are tired of Excel®

(Data)+(Visualization)+(Modelling)

quantmod

Page 3: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

quantmodOriginal Purpose:

Provide a unified interface to R for quantitative traders who are tired of Excel®

(Data)+(Visualization)+(Modelling)

Page 4: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

• Provide full financial charting abilities to R

• Allow interaction with charts

• Simple and fast interface and execution

Requirements

Page 5: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

• Provide full financial charting abilities to R

• Allow interaction with charts

• Simple and fast interface and execution

Requirements

Page 6: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

• Provide full financial charting abilities to R

• Allow interaction with charts

• Simple and fast interface and execution

Requirements

Page 7: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

• Provide full financial charting abilities to R

• Allow interaction with charts

• Simple and fast interface and execution

Requirements

Page 8: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

chartSeries

barChart candleChart matchChart lineChart

The Basics

Page 9: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)barChart

14

16

18

20

22

24

CSCO [2008−09−02/2008−11−28]

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 10: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)candleChart

14

16

18

20

22

24

CSCO [2008−09−02/2008−11−28]

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 11: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)lineChart

14

16

18

20

22

24

CSCO [2008−09−02/2008−11−28]

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 12: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)matchChart

14

16

18

20

22

24

CSCO [2008−09−02/2008−11−28]

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 13: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Creating charts is easy

getSymbols(“QQQQ”); chartSeries(QQQQ)

Page 14: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Creating charts is easy

25

30

35

40

45

50

55

QQQQ [2007−01−03/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Jan 032007

Apr 022007

Jul 022007

Oct 012007

Jan 022008

Apr 012008

Jul 012008

Oct 012008

getSymbols(“QQQQ”); chartSeries(QQQQ)

Page 15: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Creating charts is easy

25

30

35

40

45

50

55

QQQQ [2007−01−03/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Jan 032007

Apr 022007

Jul 022007

Oct 012007

Jan 022008

Apr 012008

Jul 012008

Oct 012008

Main Series

getSymbols(“QQQQ”); chartSeries(QQQQ)

Page 16: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Creating charts is easy

25

30

35

40

45

50

55

QQQQ [2007−01−03/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Jan 032007

Apr 022007

Jul 022007

Oct 012007

Jan 022008

Apr 012008

Jul 012008

Oct 012008

TA window

getSymbols(“QQQQ”); chartSeries(QQQQ)

Page 17: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

25

30

35

40

45

50

55

QQQQ [2007−01−03/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Jan 032007

Apr 022007

Jul 022007

Oct 012007

Jan 022008

Apr 012008

Jul 012008

Oct 012008

(Visualization)

Change perspective and style!

Page 18: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

25

30

35

40

45

50

55

QQQQ [2007−01−03/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Jan 032007

Apr 022007

Jul 022007

Oct 012007

Jan 022008

Apr 012008

Jul 012008

Oct 012008

(Visualization)

> zoomChart(“last 3 months”)

Page 19: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Zoomed to “last 3 months”

25

30

35

40

45

QQQQ [2008−09−02/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 20: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Switch to candlesticks

25

30

35

40

45

QQQQ [2008−09−02/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

> reChart(type= “candles”)

Page 21: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)done!

25

30

35

40

45

QQQQ [2008−09−02/2008−11−28]

Last 29.12

Volume (millions):41,238,200

100200300400500600

Sep 022008

Sep 152008

Sep 292008

Oct 132008

Oct 272008

Nov 102008

Nov 242008

Page 22: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)40+ Built-in Technical Indicators

Page 23: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)40+ Built-in Technical Indicators

Built-in TA functionality from quantmod and TTR

addADX addATR addAroon addAroonOsc addBBands

addCCI addCLV addCMF addCMO addChAD

addChVol addDEMA addDPO addEMA addEMV

addEnvelope addEVWMA addExpiry addKST addLines

addMACD addMFI addMomentum addOBV addPoints

addROC addRSI addSAR addSMA addSMI

addShading addTDI addTRIX addVo addVolatility

addWMA addWPR addZLEMA addZigZag ... and more!

Page 24: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)40+ Built-in Technical Indicators

Built-in TA functionality from quantmod and TTR

addADX addATR addAroon addAroonOsc addBBands

addCCI addCLV addCMF addCMO addChAD

addChVol addDEMA addDPO addEMA addEMV

addEnvelope addEVWMA addExpiry addKST addLines

addMACD addMFI addMomentum addOBV addPoints

addROC addRSI addSAR addSMA addSMI

addShading addTDI addTRIX addVo addVolatility

addWMA addWPR addZLEMA addZigZag ... and more!

Easy to add to charts

Page 25: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Start with a chart of AAPL (in happier times)

90

100

110

120

AAPL [2007−01−03/2007−06−29]

Jan 032007

Jan 222007

Feb 052007

Feb 202007

Mar 052007

Mar 192007

Apr 022007

Apr 162007

Apr 302007

May 142007

May 292007

Jun 112007

Jun 252007

Page 26: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

90

100

110

120

AAPL [2007−01−03/2007−06−29]

Jan 032007

Jan 222007

Feb 052007

Feb 202007

Mar 052007

Mar 192007

Apr 022007

Apr 162007

Apr 302007

May 142007

May 292007

Jun 112007

Jun 252007

(Visualization)

> addMACD()

...add Moving Average Convergence Divergence

Page 27: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

90

100

110

120

AAPL [2007−01−03/2007−06−29]

Last 122.04

Moving Average Convergence Divergence (12,26,9):MACD: 2.048Signal: 2.759

−4−2

024

Jan 032007

Jan 222007

Feb 052007

Feb 202007

Mar 052007

Mar 192007

Apr 022007

Apr 162007

Apr 302007

May 142007

May 292007

Jun 112007

Jun 252007

Page 28: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

90

100

110

120

AAPL [2007−01−03/2007−06−29]

Last 122.04

Moving Average Convergence Divergence (12,26,9):MACD: 2.048Signal: 2.759

−4−2

024

Jan 032007

Jan 222007

Feb 052007

Feb 202007

Mar 052007

Mar 192007

Apr 022007

Apr 162007

Apr 302007

May 142007

May 292007

Jun 112007

Jun 252007

> addBBands(on=1)

...add Bollinger Bands

Page 29: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

90

100

110

120

AAPL [2007−01−03/2007−06−29]

Last 122.04Bollinger Bands (20,2) [Upper/Lower]: 125.750/118.084

Moving Average Convergence Divergence (12,26,9):MACD: 2.048Signal: 2.759

−4−2

024

Jan 032007

Jan 222007

Feb 052007

Feb 202007

Mar 052007

Mar 192007

Apr 022007

Apr 162007

Apr 302007

May 142007

May 292007

Jun 112007

Jun 252007

done!

Page 30: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

Page 31: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> chartSeries(AAPL, TA= “addVo();addRSI()”) > addBBands()

Page 32: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> chartSeries(AAPL, TA= “addVo();addRSI()”) > addBBands()

80

100

120

140

160

180

200

AAPL [2007−01−03/2008−12−01]Last 88.93Bollinger Bands (20,2) [Upper/Lower]: 108.491/79.105

Volume (millions):32,991,700

20406080

100120

Relative Strength Index (14):43.973

20

40

60

80

Jan 032007

Mar 012007

May 012007

Jul 022007

Sep 042007

Nov 012007

Jan 022008

Mar 032008

May 012008

Jul 012008

Aug 012008

Oct 012008

Nov 032008

Page 33: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> chartSeries(AAPL, TA= “addVo();addRSI()”) > addBBands()

80

100

120

140

160

180

200

AAPL [2007−01−03/2008−12−01]Last 88.93Bollinger Bands (20,2) [Upper/Lower]: 108.491/79.105

Volume (millions):32,991,700

20406080

100120

Relative Strength Index (14):43.973

20

40

60

80

Jan 032007

Mar 012007

May 012007

Jul 022007

Sep 042007

Nov 012007

Jan 022008

Mar 032008

May 012008

Jul 012008

Aug 012008

Oct 012008

Nov 032008

> setTA()

Page 34: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> getSymbols(“IBM”) > chartSeries(IBM, theme= “beige”)

Page 35: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> getSymbols(“IBM”) > chartSeries(IBM, theme= “beige”)

70

80

90

100

110

120

130

IBM [2007−01−03/2008−12−01]Last 76.9Bollinger Bands (20,2) [Upper/Lower]: 93.226/71.017

Volume (millions):10,265,000

51015202530

Relative Strength Index (14):40.484

20304050607080

Jan 032007

Mar 012007

May 012007

Jul 022007

Sep 042007

Nov 012007

Jan 022008

Mar 032008

May 012008

Jul 012008

Aug 012008

Oct 012008

Nov 032008

Page 36: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> getSymbols(“IBM”) > chartSeries(IBM, theme= “beige”) > reChart(theme= “white”, subset= “2008”)

70

80

90

100

110

120

130

IBM [2007−01−03/2008−12−01]Last 76.9Bollinger Bands (20,2) [Upper/Lower]: 93.226/71.017

Volume (millions):10,265,000

51015202530

Relative Strength Index (14):40.484

20304050607080

Jan 032007

Mar 012007

May 012007

Jul 022007

Sep 042007

Nov 012007

Jan 022008

Mar 032008

May 012008

Jul 012008

Aug 012008

Oct 012008

Nov 032008

Page 37: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Customizing: setTA, theme and layout

> getSymbols(“IBM”) > chartSeries(IBM, theme= “beige”) > reChart(theme= “white”, subset= “2008”)

70

80

90

100

110

120

130

IBM [2008−01−02/2008−12−01]Last 76.9Bollinger Bands (20,2) [Upper/Lower]: 93.226/71.017

Volume (millions):10,265,000

510152025

Relative Strength Index (14):40.484

203040506070

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

Page 38: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

One main series

Custom layouts

70

80

90

100

110

120

130

IBM [2008−07−01/2008−12−01]

Jul 012008

Jul 212008

Aug 112008

Sep 022008

Sep 222008

Oct 132008

Nov 032008

Nov 242008

Page 39: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

Two Series

Custom layouts

Page 40: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

Two Series(up and down)

Custom layouts

Page 41: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Custom layouts

Page 42: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Custom indicators

addTA

newTA

add xtsible or raw data directly to a chart

create a new TA function like the built-in ones

Page 43: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)addTA

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]

Last 88.93

Volume (millions):32,991,700

20406080

100120

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

Page 44: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)addTA

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]

Last 88.93

Volume (millions):32,991,700

20406080

100120

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

> addTA(RSI(Cl(AAPL)), col=4)

Page 45: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)with our own RSI

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

Page 46: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

RSI above 70 rule as shaded region?

Page 47: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

RSI above 70 rule as shaded region?

Pass a logical vector to addTA

Page 48: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)same chart...

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

Page 49: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)same chart...

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

> addTA(RSI(Cl(AAPL)) > 70,+ col=”#888888”, border=NA,+ on= -(1:3))

Page 50: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)same chart...

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

> addTA(RSI(Cl(AAPL)) > 70,+ col=”#888888”, border=NA,+ on= -(1:3))

Page 51: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)same chart...

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

> addTA(RSI(Cl(AAPL)) > 70,+ col=”#888888”, border=NA,+ on= -(1:3))

Page 52: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)same chart...

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

Volume (millions):32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

> addTA(RSI(Cl(AAPL)) > 70,+ col=”#888888”, border=NA,+ on= -(1:3))

Page 53: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Bands are automatically created!

80

100

120

140

160

180

200

80

100

120

140

160

180

200

AAPL [2008−01−02/2008−12−01]Last 88.93

20406080

100120

20406080

100120 Volume (millions):

32,991,700

20406080

100120

RSI(Cl(AAPL)) :43.973

20304050607080

20304050607080

20304050607080 RSI(Cl(AAPL)) :43.973RSI(Cl(AAPL)) :43.973

Jan 022008

Feb 012008

Mar 032008

Apr 012008

May 012008

Jun 022008

Jul 012008

Aug 012008

Sep 022008

Oct 012008

Nov 032008

Dec 012008

Page 54: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

GMMAGuppy Multiple Moving Average

Custom indicators: newTA

Page 55: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Custom indicators: newTA

> # 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)+ }>

Page 56: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Custom indicators: newTA

> # 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”

Page 57: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)Custom indicators: newTA

> # 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()

Page 58: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

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

Page 59: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

(Visualization)

book/depth data 2D/3D/4D

option surfaces/payoffs

real-time updating...

The Future

Page 60: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

New Release 0.6-2 !

Jeffrey A. Ryan & Joshua M. Ulrich

Page 61: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time seriesQ: What is xts?

(and why another time-series?)

Page 62: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time seriesQ: What is xts?

(and why another time-series?)

A: xts is a matrix plus a time index.(formally extending zoo)

Page 63: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time seriesQ: Why another time-series?

We needed a tool that was time-aware, not just ordered...

Page 64: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time seriesQ: Why another time-series?

We needed a tool that was time-aware, not just ordered...

and had the ability to handle all time-series classes equally --- a developer’s time-series.

Page 65: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

structure

Page 66: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

structure• S3 class extending zoo and matrix

• index attribute holds time-based index

• arbitrary attributes can be attached

Page 67: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

structure• S3 class extending zoo and matrix

• index attribute holds time-based index

• arbitrary attributes can be attached

• index must be time-based

• no rownames allowed

• special formatting tools (time and attr)

Page 68: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

unique features

Page 69: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

unique features• ISO 8601 style subsetting by time

x[‘200701’] returns January of 2007x[‘2000/200803’] start of ‘00 to Mar ‘08

Page 70: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

unique features• ISO 8601 style subsetting by time

x[‘200701’] returns January of 2007

• Advanced lossless conversion utilitiesx[‘2000/200803’] start of ‘00 to Mar ‘08

try.xts: coerce data to xts, if possiblereclass: reconvert automaticallyspeed development, add flexibility!

Page 71: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

unique features• ISO 8601 style subsetting by time

x[‘200701’] returns January of 2007

• Advanced lossless conversion utilitiesx[‘2000/200803’] start of ‘00 to Mar ‘08

try.xts: coerce data to xts, if possiblereclass: reconvert automaticallyspeed development, add flexibility!

• Time-based utilities:periodicity, to.period, endpointsperiod.apply, axTicksByTime, plotting, ...

Page 72: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

New features for 0.6-2

Page 73: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Internal Structure Changes

• index is now POSIXct representation (int or double)

• index class is preserved and used for index(), as well as for printing, and conversion with as.

• xts.compat.zoo.lag global option

Page 74: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Internal Algorithm Changes

• [.xts subsetting is carried out with a binary search

Page 75: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Internal Algorithm Changes

• [.xts subsetting is carried out with a binary search

• Optimized on 10’s of millions of observations

Page 76: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Internal Algorithm Changes

• [.xts subsetting is carried out with a binary search

• Optimized on 10’s of millions of observations

• 3200+ lines of C code specific to xtsmerge, rbind, cbind, lag, Ops, diff, ...

Page 77: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time seriesPerformance Benchmarks*

matrix vector ts timeSeries fts zoo/xts xts (0.6-2)

construct 0.052 0.537 0.022 65.00* 0.128 1.032 0.055

subset by time 0.130 0.132 0.003 103.40* 0.247 0.453 0.007

merge/cbind* 0.031* 0.031 0.257 170.00* 1.146* 16.77 0.052

rbind 0.05 0.035 0.024 0.30** 1.853 9.527 0.048

diff 0.164 0.205 1.049 56.35* 0.133 11.49 0.053

lag 0.047 0.052 0.016 57.55* 0.024 1.226 0.024

x + x 0.018 0.028 1.068 0.270* 1.403 8.920 0.018

x + x[-1] error error error error 1.403 9.200 0.089

* memory limits limited timeSeries objects to 100,000 obs, so these are extrapolated timings* results in an unordered time series *cbind for fts

* timing on a very modest 2.2 GHz MacBook with 2GB RAM calling: .xts(1:1e6L, 1:1e6L)

Page 78: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

New C level API

• Access xts functionality from C code linked to R

• Worked package example installed in api_example/

• #include “xts.h” & linkingTo in DESCRIPTION

Page 79: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes

Page 80: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes• In-memory database functionality -- keys, joins

Page 81: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes• In-memory database functionality -- keys, joins• Persistent storage mechanisms

Page 82: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes

• Mixed factor/numeric support in xts objects

• In-memory database functionality -- keys, joins• Persistent storage mechanisms

Page 83: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes

• Mixed factor/numeric support in xts objects• (xts)data.frame style object, i.e. xts lists

• In-memory database functionality -- keys, joins• Persistent storage mechanisms

Page 84: quantmod · PDF file · 2008-12-05quantmod + xts Computational Finance with R Columbia University, New York December 4, 2008 Presented by Jeffrey A. Ryan jeffrey.ryan@insightalgo.com

xts: extensible time series

Future Direction

• Column attributes

• Mixed factor/numeric support in xts objects• (xts)data.frame style object, i.e. xts lists

• support for data.table, bigmemory or ff in place of matrix objects

• In-memory database functionality -- keys, joins• Persistent storage mechanisms