Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 74 lines (56 sloc) 2.544 kb
436cbce @rbresearch edits to quantstrat-rank-backtest.R
rbresearch authored
1 # quantstrat-rank-backtest.R
2862831 @rbresearch deleting some files
rbresearch authored
2
3 rm(list=ls())
4
5 library(quantstrat)
6 library(PerformanceAnalytics)
7
8 # Rank.R contains functions for different ranking algorithms
9 source("/Users/rossbennett/Documents/R Projects/Ranking/rank-functions/Rank.R")
10
11 # monthly-fun.R contains functions for prepping monthly data
12 source("/Users/rossbennett/Documents/R Projects/Ranking/rank-functions/monthly-fun.R")
13
14 # qstratRank.R contains the function to run the Rank backtest using the
15 # quantstrat framework
16 source("/Users/rossbennett/Documents/R Projects/Ranking/rank-functions/qstratRank.R")
17
18 currency("USD")
19 symbols <- c("XLY", "XLP", "XLE", "AGG", "IVV")
20 stock(symbols, currency="USD")
21
22 # get data for the symbols
436cbce @rbresearch edits to quantstrat-rank-backtest.R
rbresearch authored
23 getSymbols(symbols, from="2005-01-01", to="2012-12-31")
2862831 @rbresearch deleting some files
rbresearch authored
24
25 # create an xts object of monthly adjusted close prices
26 symbols.close <- monthlyPrices(symbols)
27
28 # create an xts object of the symbol ranks
29 sym.rank <- applyRank(x=symbols.close, rankFun=ave3ROC, n=c(2, 4, 6))
30
31 # this is an important step in naming the columns, e.g. XLY.Rank
32 # the "Rank" column is used as the trade signal (similar to an indicator)
33 # in the qstratRank function
34 colnames(sym.rank) <- gsub(".Adjusted", ".Rank", colnames(sym.rank))
35
36 # ensure the order of order symbols is equal to the order of columns
37 # in symbols.close
38 stopifnot(all.equal(gsub(".Adjusted", "", colnames(symbols.close)), symbols))
39
40 # bind the rank column to the appropriate symbol market data
41 # loop through symbols, convert the data to monthly and cbind the data
42 # to the rank
43 for(i in 1:length(symbols)) {
44 x <- get(symbols[i])
45 x <- to.monthly(x,indexAt='lastof',drop.time=TRUE)
46 indexFormat(x) <- '%Y-%m-%d'
47 colnames(x) <- gsub("x",symbols[i],colnames(x))
48 x <- cbind(x, sym.rank[,i])
49 assign(symbols[i],x)
50 }
51
52 # run the backtest
53 bt <- qstratRank(symbols=symbols, init.equity=100000, top.N=2,
436cbce @rbresearch edits to quantstrat-rank-backtest.R
rbresearch authored
54 max.size=1000, max.levels=2)
2862831 @rbresearch deleting some files
rbresearch authored
55
56 # get trade stats
436cbce @rbresearch edits to quantstrat-rank-backtest.R
rbresearch authored
57 bt.stats <- bt$stats
2862831 @rbresearch deleting some files
rbresearch authored
58
59 # chart of returns
436cbce @rbresearch edits to quantstrat-rank-backtest.R
rbresearch authored
60 charts.PerformanceSummary(bt$returns[,"total"], geometric=FALSE,
2862831 @rbresearch deleting some files
rbresearch authored
61 wealth.index=TRUE, main="Total Performance")
62
63
64 ###############################################################################
65 # R (http://r-project.org/) Quantitative Strategy Model Framework
66 #
67 # Copyright (c) 2009-2012
68 # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
69 #
70 # This library is distributed under the terms of the GNU Public License (GPL)
71 # for full details see the file COPYING
72 #
73 ###############################################################################
Something went wrong with that request. Please try again.