Computational Finance and Risk Management

mm 40 60 80 100 120

Quantitative Trading Strategies in R
40

Part 2 of 3
60

Guy Yollin
Principal Consultant, r-programming.org Visiting Lecturer, University of Washington

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

1 / 68

Outline
mm
1

40

60

80

100

120

The applyStrategy and updatePortf functions
40

2

Position sizing

3

Passing parameters at apply-time
60

4

Parameter optimization
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

2 / 68

Packages for trading system development in R
mm
PerformanceAnalytics: Econometric tools for performance and risk analysis

40

Performance metrics and graphs

60

80

100

120

quantstrat: quantitative strategy model framework blotter: tools for transaction-oriented trading systems development

40
Quantitative trading rules and trading accouting

quantmod: quantitative financial modelling framework TTR: technical trading rules

60

Data access, charting, indicators

xts: extensible time series

80

zoo: ordered observations

Time series objects

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

3 / 68

Lecture references
mm 40 60 100 TradeAnalytics project page on R-forge: 80 http://r-forge.r-project.org/projects/blotter/ documents and demos for: 40
blotter package quantstrat package

120

R-SIG-FINANCE: https://stat.ethz.ch/mailman/listinfo/r-sig-finance
60

Kent Russell’s Timely Portfolio blog: http://timelyportfolio.blogspot.com/
6-part quantstrat example 80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

4 / 68

Outline
mm
1

40

60

80

100

120

The applyStrategy and updatePortf functions
40

2

Position sizing

3

Passing parameters at apply-time
60

4

Parameter optimization
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

5 / 68

Basic strategy backtesting workflow for quantstrat
mm 40 60 80 100 120

Initialization

Define strategy

Bar-by-bar processing

Update

Reporting

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

6 / 68

Initialize currency and trading instruments
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> > > > > > library(quantstrat) # define stock list stock.str=c("SPY") # inz currency and stocks 60 dummy <- currency('USD') for(symbol in stock.str){ stock(symbol, currency="USD",multiplier=1)

} > # download stocks > start.data <- as.Date("2001-01-01") 80 > initDate <- start.data-1 > getSymbols(stock.str,from=start.data,adjust=T)
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

7 / 68

Initialize portfolio, account, and orders object
mm 40 60
Bar-by-bar processing

Initialization

Define strategy

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > # inz portfolio, account, orders, strategy strat.name <- "MAX" 60 initEq=1000000 dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate) dummy <- initAcct(name=strat.name,portfolios=strat.name, initDate=initDate, initEq=initEq) > initOrders(portfolio=strat.name,initDate=initDate) > strat <-80 strategy(strat.name)

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

8 / 68

Define indicators and signals
mm
Initialization Define strategy

40

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Initialize currency and instruments, and load historic data

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" ) > strat <-60 add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200") > # signals: > strat <- add.signal(strategy = strat,name="sigCrossover", arguments = list(columns=c("ma50","ma200"), relationship="gte"), label="ma50.gt.ma200") > strat <-80 add.signal(strategy = strat,name="sigCrossover", arguments = list(column=c("ma50","ma200"),relationship="lt"), label="ma50.lt.ma200")
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

9 / 68

Define entry and exit rules
mm
Initialization

40

60
Define strategy

Bar-by-bar processing

80
Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> # rules: > strat <-60 add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long'),type='enter') > strat <- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty=-100, ordertype='market', orderside='long'),type='exit')

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

10 / 68

The blotter_portfolio object
mm 40
blotter_portfolio 60 myPortfolio --------------------

80

100

120

40

symbols -------------------symbols -------------------symbols list list -------------------list

summary -------------------portfolio_summary xts

txn -------------------transactions xts

posPL -------------------posPL xts

posPL.USD -------------------posPL xts

60
Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees Net.Txn.Realized.PL Con.Mult Pos.Qty Con.Mult Ccy.Mult Pos.Value Pos.Avg.Cost Txn.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL

Long.Value Short.Value Net.Value Gross.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

11 / 68

The str function
mm 40 60 80 100 120

The str function compactly displays the internal structure of an R object
R Code: The str function

40

> args(str) function (object, ...) NULL

60 Main arguments:

object

the R object to be inspected

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

12 / 68

blotter_portfolio object before applyStrategy
R Code:
> str(getPortfolio(strat.name))

mm

40

60

80

100

120

List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: aAY aAZ Data: num [1, 1:10] 0 0 0 0 0 0 0 0 0 0 - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:10] "Txn.Qty" "Txn.Price" "Txn.Value" "Txn.Avg.Cost" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL :An a˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: ^AY aAZ Data: num [1, 1:11] 0 1 1 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL.USD:An a˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: ^AY aAZ Data: num [1, 1:11] 0 1 1 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL $ summary:An ^˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: aAY aAZ Data: num [1, 1:9] 0 0 0 0 0 0 0 0 0 - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:9] "Long.Value" "Short.Value" "Net.Value" "Gross.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL - attr(*, "class")= chr [1:2] "blotter_portfolio" "portfolio" - attr(*, "currency")= chr "USD" - attr(*, "initDate")= Date[1:1], format: "2000-12-31"

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

13 / 68

Applying the strategy to a portfolio
Initialization Define strategy Bar-by-bar processing Update Reporting

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60
Add indicators, signals, and rules Apply strategy to portfolio

80
Update portfolio, account, equity

100
Generate performance reports and graphs

120

R Code:
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1]

> out <- try(applyStrategy(strategy=strat , portfolios=strat.name)) "2002-04-24 "2002-04-29 "2002-04-30 "2002-05-14 60 "2003-05-12 "2004-08-25 "2004-10-27 "2006-07-25 "2006-08-29 "2007-12-28 80 "2009-06-18 "2010-07-06 "2010-10-15 SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY 100 @ 91.9933301492183" -100 @ 89.8492574695683" 100 @ 90.6900702851173" -100 @ 92.674388529813" 100 @ 81.1354076349475" -100 @ 96.8566013672453" 100 @ 98.8180303441752" -100 @ 114.662711057191" 100 @ 118.211406993905" -100 @ 137.165700378348" 100 @ 88.5716570825431" -100 @ 100.84667390332" 100 @ 116.002709490181"
Quantitative Trading Strategies in R quantstrat-II 14 / 68

40

Guy Yollin (Copyright

©

2011)

blotter_portfolio object after applyStrategy
R Code:
> str(getPortfolio(strat.name))

mm

40

60

80

100

120

List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^˘¨xts^˘´ object from 2000-12-31 to 2010-10-15 containing: aAY aAZ Data: num [1:14, 1:10] 0 100 -100 100 -100 100 -100 100 -100 100 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:10] "Txn.Qty" "Txn.Price" "Txn.Value" "Txn.Avg.Cost" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL :An a˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: ^AY aAZ Data: num [1, 1:11] 0 1 1 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL.USD:An a˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: ^AY aAZ Data: num [1, 1:11] 0 1 1 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL $ summary:An ^˘¨xts^˘´ object from 2000-12-31 to 2000-12-31 containing: aAY aAZ Data: num [1, 1:9] 0 0 0 0 0 0 0 0 0 - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:9] "Long.Value" "Short.Value" "Net.Value" "Gross.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL - attr(*, "class")= chr [1:2] "blotter_portfolio" "portfolio" - attr(*, "currency")= chr "USD" - attr(*, "initDate")= Date[1:1], format: "2000-12-31"

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

15 / 68

Transactions in the blotter_portfolio object
R Code: mm
> getPortfolio(strat.name)$symbols$SPY$txn Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees 0 0.00000 0.000 0.00000 0 0.00000 0.0000 0 100 91.99333 9199.333 91.99333 100 91.99333 0.0000 0 -100 89.84926 -8984.926 89.84926 0 0.00000 -214.4073 0 100 90.69007 9069.007 90.69007 100 90.69007 0.0000 0 -100 92.67439 -9267.439 92.67439 0 0.00000 198.4318 0 100 81.13541 8113.541 81.13541 100 81.13541 0.0000 0 -100 96.85660 -9685.660 96.85660 0 0.00000 1572.1194 0 100 98.81803 9881.803 98.81803 100 98.81803 0.0000 0 -100 114.66271 -11466.271 114.66271 0 0.00000 1584.4681 0 100 118.21141 11821.141 118.21141 100 118.21141 0.0000 0 -100 137.16570 -13716.570 137.16570 0 0.00000 1895.4293 0 100 88.57166 8857.166 88.57166 100 88.57166 0.0000 0 -100 100.84667 -10084.667 100.84667 0 0.00000 1227.5017 0 100 116.00271 11600.271 116.00271 100 116.00271 0.0000 0 Net.Txn.Realized.PL Con.Mult 0.0000 0 0.0000 1 -214.4073 1 0.0000 1 198.4318 1 0.0000 1 1572.1194 1 0.0000 1 1584.4681 1 0.0000 1 1895.4293 1 0.0000 1 1227.5017 1 0.0000 1

40

60

80

100

120

2000-12-31 2002-04-24 2002-04-29 2002-04-30 2002-05-14 2003-05-12 2004-08-25 2004-10-27 2006-07-25 2006-08-29 2007-12-28 2009-06-18 2010-07-06 2010-10-15 2000-12-31 2002-04-24 2002-04-29 2002-04-30 2002-05-14 2003-05-12 2004-08-25 2004-10-27 2006-07-25 2006-08-29 2007-12-28 2009-06-18 2010-07-06 2010-10-15

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

16 / 68

Retrieving transactions
mm
R Code:
> getTxns(Portfolio=strat.name, Symbol=stock.str) Txn.Qty 0 100 -100 100 -100 100 -100 100 -100 100 -100 100 -100 100 Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 0.00000 0 0.000 0.00000 0.0000 91.99333 0 9199.333 91.99333 0.0000 89.84926 0 -8984.926 89.84926 -214.4073 90.69007 0 9069.007 90.69007 0.0000 92.67439 0 -9267.439 92.67439 198.4318 81.13541 0 8113.541 81.13541 0.0000 96.85660 0 -9685.660 96.85660 1572.1194 98.81803 0 9881.803 98.81803 0.0000 114.66271 0 -11466.271 114.66271 1584.4681 118.21141 0 11821.141 118.21141 0.0000 137.16570 0 -13716.570 137.16570 1895.4293 88.57166 0 8857.166 88.57166 0.0000 100.84667 0 -10084.667 100.84667 1227.5017 116.00271 0 11600.271 116.00271 0.0000

40

60

80

100

120

2000-12-31 2002-04-24 2002-04-29 2002-04-30 2002-05-14 2003-05-12 2004-08-25 2004-10-27 2006-07-25 2006-08-29 2007-12-28 2009-06-18 2010-07-06 2010-10-15

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

17 / 68

Calling updatePortf
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> getPortfolio(strat.name)$summary Long.Value Short.Value Net.Value Gross.Value Realized.PL 0 0 0 0 0 60 Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL 2000-12-31 0 0 0 0 2000-12-31 > dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep='')) > # updatePortf(Portfolio=strat.name,Dates=paste('::',tail(index(SPY),1),sep='')) 80 > # updatePortf(Portfolio=strat.name) > library(lattice) > plot(xyplot(getPortfolio(strat.name)$summary,type="h",col=4,xlab=""))
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

18 / 68

blotter_portfolio object after updatePortf
R Code:
> str(getPortfolio(strat.name))

mm

40

60

80

100

120

List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^˘¨xts^˘´ object from 2000-12-31 to 2010-10-15 containing: aAY aAZ Data: num [1:14, 1:10] 0 100 -100 100 -100 100 -100 100 -100 100 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:10] "Txn.Qty" "Txn.Price" "Txn.Value" "Txn.Avg.Cost" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL :An a˘¨xts^˘´ object from 2000-12-31 to 2011-08-05 containing: ^AY aAZ Data: num [1:2666, 1:11] 0 0 0 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL .. ..$ posPL.USD:An a˘¨xts^˘´ object from 2000-12-31 to 2011-08-05 containing: ^AY aAZ Data: num [1:2666, 1:11] 0 0 0 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:11] "Pos.Qty" "Con.Mult" "Ccy.Mult" "Pos.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL $ summary:An ^˘¨xts^˘´ object from 2000-12-31 to 2011-08-05 containing: aAY aAZ Data: num [1:2666, 1:9] 0 0 0 0 0 0 0 0 0 0 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:9] "Long.Value" "Short.Value" "Net.Value" "Gross.Value" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL - attr(*, "class")= chr [1:2] "blotter_portfolio" "portfolio" - attr(*, "currency")= chr "USD" - attr(*, "initDate")= Date[1:1], format: "2000-12-31"

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

19 / 68

Plot of blotter_portfolio$summary object
mm 40 60
0 5000 15000 −0.4 0.0 0.4 2002

0 5000 15000 0 5000 15000

Long.Value

80

2004

2006

Short.Value

100

2008

2010

120

2012

Net.Value

Gross.Value

40
Period.Realized.PL

Period.Unrealized.PL
−0.4 0.0 0.4 −2000 −500

0 1000

0 400 −600

0 400

60

Gross.Trading.PL

Txn.Fees

Net.Trading.PL

−600

80
2002 2004 2006 2008 2010 2012

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

20 / 68

How the blotter_portfolio object gets updated
mm 40
blotter_portfolio 60 myPortfolio --------------------

80

100

120

Updated by applyStrategy

40

symbols -------------------symbols -------------------symbols list list -------------------list

summary -------------------portfolio_summary xts

txn -------------------transactions xts

posPL -------------------posPL xts

posPL.USD -------------------posPL xts

60
Txn.Qty Txn.Price Txn.Value Txn.Avg.Cost Pos.Qty Pos.Avg.Cost Gross.Txn.Realized.PL Txn.Fees Net.Txn.Realized.PL Con.Mult Pos.Qty Con.Mult Ccy.Mult Pos.Value Pos.Avg.Cost Txn.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL

Long.Value Short.Value Net.Value Gross.Value Period.Realized.PL Period.Unrealized.PL Gross.Trading.PL Txn.Fees Net.Trading.PL

Updated by updatePortf

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

21 / 68

Chart of moving average crossover performance
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:

60 > source("chart_Posn.R")
> chart_Posn(Portfolio=strat.name,Symbol=stock.str) > add_SMA(n=50 , on=1,col='blue',lwd=2) > add_SMA(n=200, on=1,col='red',lwd=2)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

22 / 68

Chart of moving average crossover performance
mm 40 60 80 100 120

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

23 / 68

Outline
mm
1

40

60

80

100

120

The applyStrategy and updatePortf functions
40

2

Position sizing

3

Passing parameters at apply-time
60

4

Parameter optimization
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

24 / 68

The ruleSignal function
ruleSignal is the default rule to generate a trade order on a signal
40 R Code: The ruleSignal function
> args(ruleSignal) function (data = mktdata, timestamp, sigcol, sigval, orderqty = 0, ordertype, orderside = NULL, threshold = NULL, tmult = FALSE, replace = TRUE, delay = 1e-04, osFUN = "osNoOp", pricemethod = c("market", 40 "opside", "maker"), portfolio, symbol, ..., ruletype, TxnFees = 0, prefer = NULL, sethold = FALSE) NULL

mm

60

80

100

120

Main arguments: 60 an xts object containing market data (defaults to mktdata) data sigcol column name to check for signal sigval signal value to match orderqty quantity for order or ’all’, modified by osFUN ordertype80 ” market” limit”,”stoplimit”,”stoptrailing”,”iceberg” ,” orderside ” long” ” , short” or NULL , osFUN function or name of order sizing function (default is osNoOp)
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

25 / 68

The osNoOp function
The function osNoOp 40 the default order sizing function 100 is mm 60 80
R Code: The osNoOp function
> args(osNoOp) function (timestamp, orderqty, portfolio, symbol, ruletype, ...) NULL 40

120

Main arguments: timestamp timestamp (coercible into a POSIXct object) that will mark 60 the time of order insertion orderqty portfolio symbol ruletype
80

the order quanty; modified by osFUN name of the portfolio for the order symbol of instrument one of ” risk” ”order”, ”rebalance”, ”enter”, ” , exit”

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

26 / 68

Fixed-dollar order sizing function
This order sizing function adjusts 60 share quantity such that the the mm 40 80 100 transaction value is approximately equal to a pre-defined tradesize
R Code:
> osFixedDollar <- function(timestamp,orderqty, portfolio, symbol, ruletype, ...) 40 { ClosePrice <- as.numeric(Cl(mktdata[timestamp,])) orderqty <- sign(orderqty)*round(tradeSize/ClosePrice) return(orderqty) }

120

60

function retrieves the current close price and sets order quantity as follows:
80

orderqty =

tradeSize ClosePrice

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

27 / 68

Re-initializing the .blotter and .strategy environments
mm 40 60 80 100 120

It is not possible to run initPortf, initAcct, or initOrders a second time in the same R session using the same names
40 It is necessary to either remove the individual objects or remove all objects from the .blotter and .strategy environments
R Code:

60 > try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE) > try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

28 / 68

Initialize portfolio, account, and orders object
Initialization mm

40

Define strategy

60

Bar-by-bar processing

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > > > # inz portfolio, account, orders, strategy strat.name <- "MAX" 60 initEq=1000000 trade.percent <- 0.01 tradeSize <- initEq * trade.percent dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate) dummy <- initAcct(name=strat.name,portfolios=strat.name, initDate=initDate, initEq=initEq) 80 > initOrders(portfolio=strat.name,initDate=initDate) > strat <- strategy(strat.name)

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

29 / 68

Define indicators and signals
mm
Initialization Define strategy

40

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Initialize currency and instruments, and load historic data

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" ) > strat <-60 add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200") > # signals: > strat <- add.signal(strategy = strat,name="sigCrossover", arguments = list(columns=c("ma50","ma200"), relationship="gte"), label="ma50.gt.ma200") > strat <-80 add.signal(strategy = strat,name="sigCrossover", arguments = list(column=c("ma50","ma200"),relationship="lt"), label="ma50.lt.ma200")
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

30 / 68

Add rules with an order sizing function specified
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # rules: > strat <- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', osFUN='osFixedDollar'),type='enter') 60 > strat <- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty='all', ordertype='market', orderside='long'),type='exit')

The 80 ruleSignal argument osFUN is now set to the name of our custom order sizing function for the entry rule The argument orderqty is now set to ’all’ for the exit rule
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

31 / 68

Applying the strategy to a portfolio
Initialization Define strategy Bar-by-bar processing Update Reporting

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60
Add indicators, signals, and rules Apply strategy to portfolio

80
Update portfolio, account, equity

100
Generate performance reports and graphs

120

R Code:
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1]

> out <- try(applyStrategy(strategy=strat , portfolios=strat.name)) "2002-04-24 "2002-04-29 "2002-04-30 "2002-05-14 60 "2003-05-12 "2004-08-25 "2004-10-27 "2006-07-25 "2006-08-29 "2007-12-28 80 "2009-06-18 "2010-07-06 "2010-10-15 SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY 109 @ 91.9933301492183" -109 @ 89.8492574695683" 110 @ 90.6900702851173" -110 @ 92.674388529813" 123 @ 81.1354076349475" -123 @ 96.8566013672453" 101 @ 98.8180303441752" -101 @ 114.662711057191" 85 @ 118.211406993905" -85 @ 137.165700378348" 113 @ 88.5716570825431" -113 @ 100.84667390332" 86 @ 116.002709490181"
Quantitative Trading Strategies in R quantstrat-II 32 / 68

40

Guy Yollin (Copyright

©

2011)

Transactions with varying quantities
mm
R Code:
> getTxns(Portfolio=strat.name, Symbol=stock.str) Txn.Qty 0 109 -109 110 -110 123 -123 101 -101 85 -85 113 -113 86 Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 0.00000 0 0.000 0.00000 0.0000 91.99333 0 10027.273 91.99333 0.0000 89.84926 0 -9793.569 89.84926 -233.7039 90.69007 0 9975.908 90.69007 0.0000 92.67439 0 -10194.183 92.67439 218.2750 81.13541 0 9979.655 81.13541 0.0000 96.85660 0 -11913.362 96.85660 1933.7068 98.81803 0 9980.621 98.81803 0.0000 114.66271 0 -11580.934 114.66271 1600.3128 118.21141 0 10047.970 118.21141 0.0000 137.16570 0 -11659.085 137.16570 1611.1149 88.57166 0 10008.597 88.57166 0.0000 100.84667 0 -11395.674 100.84667 1387.0769 116.00271 0 9976.233 116.00271 0.0000

40

60

80

100

120

2000-12-31 2002-04-24 2002-04-29 2002-04-30 2002-05-14 2003-05-12 2004-08-25 2004-10-27 2006-07-25 2006-08-29 2007-12-28 2009-06-18 2010-07-06 2010-10-15

40

60

Each entry has an approximate value of $10,000
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

33 / 68

Calling updatePortf
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep=''))

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

34 / 68

Chart of fixed-dollar MA crossover performance
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity

Generate performance reports and graphs

R Code: 60
> chart_Posn(Portfolio=strat.name,Symbol=stock.str) > add_SMA(n=50 , on=1,col='blue',lwd=2) > add_SMA(n=200, on=1,col='red',lwd=2)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

35 / 68

Moving average crossover with fixed-dollar entries
mm 40 60 80 100 120

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

36 / 68

Percent-of-available-equity order sizing function
This order sizing function adjusts the share quantity such that each trade is approximately a fixed percentage of the available account equity mm 40 60 80 100 120
R Code:
> osPercentEquity <- function (timestamp,orderqty,portfolio,symbol,ruletype,...) { tempPortfolio <- getPortfolio(portfolio) 40 dummy <- updatePortf(Portfolio=portfolio, Dates=paste('::',as.Date(timestamp),sep='')) trading.pl <- sum(getPortfolio(portfolio)$summary$Net.Trading.PL) assign(paste("portfolio.",portfolio,sep=""),tempPortfolio,pos=.blotter) total.equity <- initEq+trading.pl tradeSize <- total.equity * trade.percent 60 ClosePrice <- as.numeric(Cl(mktdata[timestamp,])) orderqty <- sign(orderqty)*round(tradeSize/ClosePrice) return(orderqty) }

80

orderqty =
Guy Yollin (Copyright

©

(initEq + trading P&L) × trade.percent ClosePrice
2011) Quantitative Trading Strategies in R quantstrat-II 37 / 68

Initialize portfolio, account, and orders object
mm Initialization 40
Define strategy

60

Bar-by-bar processing

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > > try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE) try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE) 60 # inz portfolio, account, orders, strategy trade.percent <- 0.02 dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate) dummy <- initAcct(name=strat.name,portfolios=strat.name, initDate=initDate, initEq=initEq) > initOrders(portfolio=strat.name,initDate=initDate) 80 > strat <- strategy(strat.name)

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

38 / 68

Define indicators and signals
mm
Initialization Define strategy

40

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Initialize currency and instruments, and load historic data

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=50),label= "ma50" ) > strat <-60 add.indicator(strategy = strat, name = "SMA", arguments = list(x=quote(Cl(mktdata)), n=200),label= "ma200") > # signals: > strat <- add.signal(strategy = strat,name="sigCrossover", arguments = list(columns=c("ma50","ma200"), relationship="gte"), label="ma50.gt.ma200") > strat <-80 add.signal(strategy = strat,name="sigCrossover", arguments = list(column=c("ma50","ma200"),relationship="lt"), label="ma50.lt.ma200")
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

39 / 68

Add rules with an order sizing function specified
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # rules: > strat <- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.gt.ma200",sigval=TRUE, orderqty=100, ordertype='market', orderside='long', osFUN='osPercentEquity'),type='enter') 60 > strat <- add.rule(strategy = strat,name='ruleSignal', arguments = list(sigcol="ma50.lt.ma200",sigval=TRUE, orderqty='all', ordertype='market', orderside='long'),type='exit')

The 80 ruleSignal argument osFUN is now set to the name of our custom order sizing function for the entry rule The argument orderqty is now set to ’all’ for the exit rule
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

40 / 68

Applying the strategy to a portfolio
Initialization Define strategy Bar-by-bar processing Update Reporting

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60
Add indicators, signals, and rules Apply strategy to portfolio

80
Update portfolio, account, equity

100
Generate performance reports and graphs

120

R Code:
[1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1] [1]

> out <- try(applyStrategy(strategy=strat , portfolios=strat.name)) "2002-04-24 "2002-04-29 "2002-04-30 "2002-05-14 60 "2003-05-12 "2004-08-25 "2004-10-27 "2006-07-25 "2006-08-29 "2007-12-28 80 "2009-06-18 "2010-07-06 "2010-10-15 SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY SPY 217 @ 91.9933301492183" -217 @ 89.8492574695683" 220 @ 90.6900702851173" -220 @ 92.674388529813" 246 @ 81.1354076349475" -246 @ 96.8566013672453" 203 @ 98.8180303441752" -203 @ 114.662711057191" 170 @ 118.211406993905" -170 @ 137.165700378348" 228 @ 88.5716570825431" -228 @ 100.84667390332" 175 @ 116.002709490181"
Quantitative Trading Strategies in R quantstrat-II 41 / 68

40

Guy Yollin (Copyright

©

2011)

Transactions with varying quantities
mm
R Code:
> getTxns(Portfolio=strat.name, Symbol=stock.str) Txn.Qty 0 217 -217 220 -220 246 -246 203 -203 170 -170 228 -228 175 Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 0.00000 0 0.00 0.00000 0.0000 91.99333 0 19962.55 91.99333 0.0000 89.84926 0 -19497.29 89.84926 -465.2638 90.69007 0 19951.82 90.69007 0.0000 92.67439 0 -20388.37 92.67439 436.5500 81.13541 0 19959.31 81.13541 0.0000 96.85660 0 -23826.72 96.85660 3867.4137 98.81803 0 20060.06 98.81803 0.0000 114.66271 0 -23276.53 114.66271 3216.4702 118.21141 0 20095.94 118.21141 0.0000 137.16570 0 -23318.17 137.16570 3222.2299 88.57166 0 20194.34 88.57166 0.0000 100.84667 0 -22993.04 100.84667 2798.7038 116.00271 0 20300.47 116.00271 0.0000

40

60

80

100

120

2000-12-31 2002-04-24 2002-04-29 2002-04-30 2002-05-14 2003-05-12 2004-08-25 2004-10-27 2006-07-25 2006-08-29 2007-12-28 2009-06-18 2010-07-06 2010-10-15

40

60

Each entry is approximately 2% of the account value
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

42 / 68

Call updatePortf
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep=''))

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

43 / 68

Chart of percent-equity MA crossover performance
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity

Generate performance reports and graphs

R Code: 60
> chart_Posn(Portfolio=strat.name,Symbol=stock.str) > add_SMA(n=50 , on=1,col='blue',lwd=2) > add_SMA(n=200, on=1,col='red',lwd=2)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

44 / 68

Moving average crossover with percent-equity entries
mm 40 60 80 100 120

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

45 / 68

Outline
mm
1

40

60

80

100

120

The applyStrategy and updatePortf functions
40

2

Position sizing

3

Passing parameters at apply-time
60

4

Parameter optimization
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

46 / 68

Reload price history and convert to monthly bars
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> # download all of SPY 60 > start.data <- as.Date("1993-01-01") > initDate <- start.data-1 > getSymbols(stock.str,from=start.data,adjust=T) > SPY=to.monthly(SPY, indexAt='endof')

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

47 / 68

Initialize portfolio, account, and orders object
mm Initialization 40
Define strategy

60

Bar-by-bar processing

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > > # clear .blotter and .strategy environments try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE) 60 try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE) # inz portfolio, account, orders, strategy dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate) dummy <- initAcct(name=strat.name,portfolios=strat.name, initDate=initDate, initEq=initEq) > initOrders(portfolio=strat.name,initDate=initDate) 80 > strat <- strategy(strat.name)

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

48 / 68

Define indicators and signals
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10") > # signals: 60 > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMA10"),relationship="gt"), label="Cl.gt.SMA") > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMA10"),relationship="lt"), label="Cl.lt.SMA") 80

MA length (n=10) passed in arguments list to add.indicator
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

49 / 68

Add rules with an order sizing function specified
mm
Initialization

40

60
Define strategy

Bar-by-bar processing

80
Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> # rules: > strat <-60 add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, ordertype='market', orderside='long', pricemethod='market',osFUN='osPercentEquity'), type='enter') > strat <- add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long'), type='exit')

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

50 / 68

Applying the strategy to a portfolio
mm 40 60 80 100 120

Initialization

Define strategy

Bar-by-bar processing

Update

Reporting

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code:

60

> out <- try(applyStrategy(strategy=strat , portfolios=strat.name))

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

51 / 68

Call updatePortf
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep=''))

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

52 / 68

Chart Faber 10-month SMA system
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> chart_Posn(Portfolio=strat.name,Symbol=stock.str) > add_SMA(n=10 , on=1,col='blue',lwd=2)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

53 / 68

Faber 10-month SMA system
mm 40 60 80 100 120

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

54 / 68

Initialize portfolio, account, and orders object
mm Initialization 40
Define strategy

60

Bar-by-bar processing

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > > # clear .blotter and .strategy environments try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE) 60 try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE) # inz portfolio, account, orders, strategy dummy <- initPortf(name=strat.name,symbols=stock.str, initDate=initDate) dummy <- initAcct(name=strat.name,portfolios=strat.name, initDate=initDate, initEq=initEq) > initOrders(portfolio=strat.name,initDate=initDate) 80 > strat <- strategy(strat.name)

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

55 / 68

Define indicators and signals
Initialization Define strategy

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60

Bar-by-bar processing

Update

Reporting

80

100

120
Generate performance reports and graphs

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

40
R Code:
> # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x = quote(Cl(mktdata))), label="SMAn") > # signals: 60 > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMAn"),relationship="gt"), label="Cl.gt.SMA") > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMAn"),relationship="lt"), label="Cl.lt.SMA") 80

No MA length parameter specified in add.indicator
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

56 / 68

Add rules with an order sizing function specified
mm
Initialization

40

60
Define strategy

Bar-by-bar processing

80
Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> # rules: > strat <-60 add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, ordertype='market', orderside='long', pricemethod='market',osFUN='osPercentEquity'), type='enter') > strat <- add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long'), type='exit')

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

57 / 68

Applying the strategy with apply-time parameter passing
mm
Initialization

40

60
Define strategy Bar-by-bar processing

80
Update

100
Reporting

120

40
R Code:

Initialize currency and instruments, and load historic data

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

> out <- try(applyStrategy(strategy=strat , portfolios=strat.name, parameters=list(n=5))) 60

The moving average length (n=5) is passed in the named-list parameters argument
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

58 / 68

Call updatePortf
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep=''))

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

59 / 68

Chart Faber 5-month SMA system
mm
Initialization

40

60
Define strategy

80
Bar-by-bar processing Update

100
Reporting

120

40
Initialize currency and instruments, and load historic data Initialize portfolio, account, orders, strategy Add indicators, signals, and rules Apply strategy to portfolio Update portfolio, account, equity Generate performance reports and graphs

R Code: 60
> chart_Posn(Portfolio=strat.name,Symbol=stock.str) > add_SMA(n=5 , on=1,col='blue',lwd=2)

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

60 / 68

Faber 5-month SMA system
mm 40 60 80 100 120

40

60

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

61 / 68

Outline
mm
1

40

60

80

100

120

The applyStrategy and updatePortf functions
40

2

Position sizing

3

Passing parameters at apply-time
60

4

Parameter optimization
80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

62 / 68

Initialize currency and trading instruments
mm Initialization 40
Define strategy

60

Bar-by-bar processing

80

Update

100

Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> > > > > # define GSPC instrument dummy <- stock("GSPC", currency="USD",multiplier=1) 60 # download stocks start.data <- as.Date("1950-01-01") initDate <- start.data-1

> getSymbols("^GSPC",from=start.data,adjust=T) > GSPC=to.monthly(GSPC, indexAt='endof') 80 > trade.percent <- 1.0

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

63 / 68

Define indicators and signals
Initialization Define strategy Bar-by-bar processing Update Reporting

mm
Initialize currency and instruments, and load historic data

40
Initialize portfolio, account, orders, strategy

60
Add indicators, signals, and rules

80
Apply strategy to portfolio

100
Update portfolio, account, equity

120
Generate performance reports and graphs

R Code:

40

> strat <- strategy(strat.name) > # indicators: > strat <- add.indicator(strategy = strat, name = "SMA", arguments = list(x = quote(Cl(mktdata))), label="SMAn") 60 > # signals: > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMAn"),relationship="gt"), label="Cl.gt.SMA") > strat <- add.signal(strat,name="sigCrossover", arguments = list(columns=c("Close","SMAn"),relationship="lt"), 80 label="Cl.lt.SMA")

No MA length parameter specified in add.indicator
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

64 / 68

Add rules with an order sizing function specified
mm
Initialization

40

60
Define strategy

Bar-by-bar processing

80
Update

100
Reporting

120

Initialize currency and instruments, and load historic data

40

Initialize portfolio, account, orders, strategy

Add indicators, signals, and rules

Apply strategy to portfolio

Update portfolio, account, equity

Generate performance reports and graphs

R Code:
> # rules: > strat <-60 add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, ordertype='market', orderside='long', pricemethod='market',osFUN='osPercentEquity'), type='enter') > strat <- add.rule(strat, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long'), type='exit')

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

65 / 68

Applying the strategy across range of parameter values
R Code:

mm 40 60 80 100 120 > parm.seq <- seq(3,20,by=1) > res.vec <- rep(NA,length(parm.seq)) > for(i in 1:length(parm.seq)) { # initialize portfolio and orders try(rm(list=ls(pos=.blotter),pos=.blotter),silent=TRUE) try(rm(list=ls(pos=.strategy),pos=.strategy),silent=TRUE) 40 dummy <- initPortf(name=strat.name,symbols="GSPC", initDate=initDate) initOrders(portfolio=strat.name,initDate=initDate) # apply strategy N <- parm.seq[i] applyStrategy(strategy=strat , portfolios=strat.name, parameters=list(n=N)) # calculate performance matric 60 dummy <- updatePortf(Portfolio=strat.name, Dates=paste('::',as.Date(Sys.time()),sep='')) tstats <- tradeStats(Portfolio=strat.name, Symbol="GSPC") if(is.numeric(tstats$maxDrawdown)) { returnOnMDD <- tstats$Net.Trading.PL / abs(tstats$maxDrawdown) 80 res.vec[i] <- returnOnMDD } }
Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

66 / 68

Plot optimization results
mm
R Code: Plot code
> names(res.vec) <- parm.seq > sort(res.vec,decreasing=T)

40

60

80

100

120

40

17 18 10 9 8 16 20 19 12 7.182873 7.154502 7.140314 7.125639 7.113975 6.941945 5.842226 5.210899 4.945895 15 13 11 14 6 7 5 3 4 4.938753 4.934645 4.928929 4.926389 4.557785 3.924239 3.583817 2.945360 2.925350 > barplot(height=res.vec,names.arg=parm.seq,col="lightslateblue", 60 xlab="length of MA (months)",ylab="Return on MaxDD") > title("Return on Maximum Drawdown versus Moving Average Length")

80

Guy Yollin (Copyright

©

2011)

Quantitative Trading Strategies in R

quantstrat-II

67 / 68

Optimization performance metric
mm
7

Return on Maximum Drawdown versus Moving Average Length

40

60

80

100

120

6

40

Return on MaxDD

3

4

5

60
2 0 1

80
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Guy Yollin (Copyright

©

length of MA (months)
2011) Quantitative Trading Strategies in R quantstrat-II 68 / 68

Sign up to vote on this title
UsefulNot useful