Quantitative Trading Strategies in R Part 2 of 3
February 17, 2017 | Author: lycancapital | Category: N/A
Short Description
Download Quantitative Trading Strategies in R Part 2 of 3 ...
Description
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
40
60
80
1
The applyStrategy and updatePortf functions
2
Position sizing
3
Passing parameters at apply-time
100
120
40
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 PerformanceAnalytics: Econometric tools for performance and risk analysis
mm
40
60
80
Performance metrics and graphs
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 zoo: ordered observations
80
Time series objects
Guy Yollin (Copyright
©
2011)
Quantitative Trading Strategies in R
quantstrat-II
3 / 68
Lecture references mm 40 60 R-forge: 80 100 TradeAnalytics project page on http://r-forge.r-project.org/projects/blotter/
120
documents and demos for: 40
blotter package quantstrat package
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
40
60
80
1
The applyStrategy and updatePortf functions
2
Position sizing
3
Passing parameters at apply-time
100
120
40
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
Initialization
60
80
100
120
Define strategy
Bar-by-bar processing
Update
Reporting
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Generate performance reports and graphs
40 Initialize currency and instruments, and load historic data
Initialize portfolio, account, orders, strategy
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
Add indicators, signals, and rules
Bar-by-bar processing
Update
Apply strategy to portfolio
Update portfolio, account, equity
80
Reporting
100
120 Generate performance reports and graphs
40 R Code: > > > > > >
library(quantstrat) # define stock list stock.str=c("SPY") # inz currency and stocks 60 dummy # download stocks > start.data 80 initDate 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
Initialization
Initialize currency and instruments, and load historic data
40
Initialize portfolio, account, orders, strategy
60
80
100
Define strategy
Bar-by-bar processing
Update
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Reporting
120
Generate performance reports and graphs
R Code: > > > > >
# inz portfolio, account, orders, strategy strat.name 60 strat strat # signals: > strat strat # rules: > strat strat 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:
mm
40
> str(getPortfolio(strat.name))
60
80
100
120
List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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 ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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
mm
40
Initialize currency and instruments, and load historic data
R Code:
Define strategy
Initialize portfolio, account, orders, strategy
Bar-by-bar processing
60 Add indicators, signals, and rules
Update
80 Apply strategy to portfolio
Reporting
100 Update portfolio, account, equity
120
Generate performance reports and graphs
40
> out str(getPortfolio(strat.name))
60
80
100
120
List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2010-10-15 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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 ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2000-12-31 containing: 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
40
60
80
100
120
> getPortfolio(strat.name)$symbols$SPY$txn
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
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
Guy Yollin (Copyright
©
2011)
Quantitative Trading Strategies in R
quantstrat-II
16 / 68
Retrieving transactions mm
40
60
80
100
120
R Code: > getTxns(Portfolio=strat.name, Symbol=stock.str)
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
Txn.Qty 0 100 -100 100 -100 100 -100 100 -100 100 -100 100 -100 100
40
60
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
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
Initialize portfolio, account, orders, strategy
60
Add indicators, signals, and rules
Bar-by-bar processing
Update
Apply strategy to portfolio
Update portfolio, account, equity
80
Reporting
100
120 Generate performance reports and graphs
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('::',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:
mm
40
> str(getPortfolio(strat.name))
60
80
100
120
List of 2 $ symbols:List of 1 ..$ SPY:List of 3 .. ..$ txn :An ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2010-10-15 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2011-08-05 containing: 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 ^˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2011-08-05 containing: 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 ^ a˘ A¨ Yxts^ a˘ A´ Z object from 2000-12-31 to 2011-08-05 containing: 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
mm
40
2002
60
Long.Value
80
0 5000 15000−0.4 0.0 0.4
0 5000 15000 0 5000 15000
Plot of blotter_portfolio$summary object
Net.Value
40
0 1000 −600
0 400 −600
0 400
Gross.Trading.PL
60
2006
2008
100
2010
Short.Value
2012
120
Gross.Value
Period.Unrealized.PL −0.4 0.0 0.4 −2000 −500
Period.Realized.PL
2004
Net.Trading.PL
Txn.Fees
80 2002
2004
Guy Yollin (Copyright
©
2006
2011)
2008
2010
2012
Quantitative Trading Strategies in R
quantstrat-II
20 / 68
How the blotter_portfolio object gets updated mm
myPortfolio --------------------
40
blotter_portfolio 60
80
symbols -------------------symbols list -------------------symbols list -------------------list
Updated by applyStrategy
40
txn -------------------transactions xts
posPL -------------------posPL xts
80
Guy Yollin (Copyright
©
2011)
120
summary -------------------portfolio_summary 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
100
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
Quantitative Trading Strategies in R
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
quantstrat-II
21 / 68
Chart of moving average crossover performance mm
40
Initialization
40
Initialize currency and instruments, and load historic data
Initialize portfolio, account, orders, strategy
60
80
100
120
Define strategy
Bar-by-bar processing
Update
Reporting
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
40
60
80
1
The applyStrategy and updatePortf functions
2
Position sizing
3
Passing parameters at apply-time
100
120
40
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 mm
40function R Code: The ruleSignal
60
80
100
120
> 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
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 is the default mm osNoOp 40 60order sizing80function 100
120
R Code: The osNoOp function > args(osNoOp) function (timestamp, orderqty, portfolio, symbol, ruletype, ...) NULL 40
Main arguments: timestamp timestamp (coercible into a POSIXct object) that will mark 60 the time of order insertion orderqty
the order quanty; modified by osFUN
portfolio
name of the portfolio for the order
symbol ruletype
80
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 the share quantity such that mmsizing function 40 adjusts 60 80 100 the transaction value is approximately equal to a pre-defined tradesize
120
R Code: > osFixedDollar > > > > > >
# inz portfolio, account, orders, strategy strat.name strat strat out getTxns(Portfolio=strat.name, Symbol=stock.str)
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
Txn.Qty 0 109 -109 110 -110 123 -123 101 -101 85 -85 113 -113 86
40
60
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
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
40
Initialization
60
80
100
120
Define strategy
Bar-by-bar processing
Update
Reporting
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Generate performance reports and graphs
40 Initialize currency and instruments, and load historic data
Initialize portfolio, account, orders, strategy
R Code: 60 > dummy 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 account mm 40 percentage 60of the available 80 100equity 120 R Code: > osPercentEquity strat strat out getTxns(Portfolio=strat.name, Symbol=stock.str)
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
Txn.Qty 0 217 -217 220 -220 246 -246 203 -203 170 -170 228 -228 175
40
60
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
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
40
Initialization
60
80
100
120
Define strategy
Bar-by-bar processing
Update
Reporting
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Generate performance reports and graphs
40 Initialize currency and instruments, and load historic data
Initialize portfolio, account, orders, strategy
R Code: 60 > dummy 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
40
60
80
1
The applyStrategy and updatePortf functions
2
Position sizing
3
Passing parameters at apply-time
100
120
40
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
40
Initialization
Initialize currency and instruments, and load historic data
40
Initialize portfolio, account, orders, strategy
60
80
100
120
Define strategy
Bar-by-bar processing
Update
Reporting
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Generate performance reports and graphs
R Code: > # download 60 all of SPY > start.data initDate 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
Initialize currency and instruments, and load historic data
40
40
Initialize portfolio, account, orders, strategy
60
80
100
Define strategy
Bar-by-bar processing
Update
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Reporting
120
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 strat # indicators: > strat # signals: 60 > strat strat # rules: > strat strat out dummy 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
Initialize currency and instruments, and load historic data
40
40
Initialize portfolio, account, orders, strategy
60
80
100
Define strategy
Bar-by-bar processing
Update
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
Reporting
120
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 strat # indicators: > strat # signals: 60 > strat strat # rules: > strat strat out dummy 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
40
60
80
1
The applyStrategy and updatePortf functions
2
Position sizing
3
Passing parameters at apply-time
100
120
40
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
Initialize currency and instruments, and load historic data
40
40
Initialize portfolio, account, orders, strategy
Define strategy
Bar-by-bar processing
Update
Add indicators, signals, and rules
Apply strategy to portfolio
Update portfolio, account, equity
60
80
100
Reporting
120
Generate performance reports and graphs
R Code: > > > > >
# define GSPC instrument dummy trade.percent strat # indicators: > strat # signals: > strat strat # rules: > strat strat parm.seq res.vec 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) 40initPortf(name=strat.name,symbols="GSPC", initDate=initDate) dummy
View more...
Comments