Chapter 1 Introduction

This book is designed to not only produce statistics on many of the most common technical patterns in the stock market, but to show actual trades in such scenarios.

  • Test a strategy; reject if results are not promising

  • Apply a range of parameters to strategies for optimization

  • Attempt to kill any strategy that looks promising.

Let me explain that last one a bit. Just because you may find a strategy that seems to outperform the market, have good profit and low drawdown this doesn’t mean you’ve found a strategy to put to work. On the contrary, you must work to disprove it. Nothing is worse than putting a non-profitable strategy to work because it wasn’t rigurously tested. We’ll address that later.

1.1 R Resources

This book assumes you have at least a basic working knowledge of the R platform. If you are new to R or need a refresher, the following site should be beneficial:

In addition, the packages used in this book can be found under the TradeAnalytics projected on R-Forge. You will find forums and source code that have helped inspire this book.

I also recommend you read Guy Yollin’s presentations on backtesting as well as the Using Quantstrat presentation by Jan Humme and Brian Peterson.

This book is not intended to replace any of the existing resources on backtesting strategies in R. Rather, the intent is to enhance and streamline those resources. If something is not addressed in this book read the presentations above.

Also, this book is open-source. Anyone is welcome to contribute. You can find the source code available on my Github account.

1.2 Libraries

The only required library needed to run backtesting strategies is quantstrat. quantstrat will load all additionally required libraries.

  • quantstrat 0.9.1739

This version of quantstrat includes the following packages, among others:

  • blotter 0.9.1741

  • quantmod 0.4-5

  • TTR 0.23-1

With these libraries we will have all we need to fully-test strategies and measure performance. See 1.3 SessionInfo for more details.

library(quantstrat)
## Loading required package: quantmod
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Version 0.4-0 included new data defaults. See ?getSymbols.
## Loading required package: blotter
## Loading required package: FinancialInstrument
## Loading required package: PerformanceAnalytics
## 
## Attaching package: 'PerformanceAnalytics'
## The following object is masked from 'package:graphics':
## 
##     legend
## Loading required package: foreach

Additional libraries we may use for analysis or book presentation:

  • ggplot2 2.0.0

  • dplyr 0.4.3

  • tidyr 0.4.1

library(data.table)
## 
## Attaching package: 'data.table'
## The following object is masked from 'package:xts':
## 
##     last
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, last
## The following objects are masked from 'package:xts':
## 
##     first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(DT)
library(ggplot2)
library(htmltools)
library(htmlwidgets)
library(knitr)
library(lattice)
library(pander)
## 
## Attaching package: 'pander'
## The following object is masked from 'package:htmltools':
## 
##     p
library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:FinancialInstrument':
## 
##     spread
library(webshot)

1.3 SessionInfo

sessionInfo()
## R version 3.2.3 (2015-12-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 14.04.4 LTS
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] webshot_0.3                   tidyr_0.4.1                  
##  [3] pander_0.6.0                  lattice_0.20-33              
##  [5] knitr_1.12.3                  htmlwidgets_0.6              
##  [7] htmltools_0.3.5               ggplot2_2.0.0                
##  [9] DT_0.1                        dplyr_0.4.3                  
## [11] data.table_1.9.6              quantstrat_0.9.1739          
## [13] foreach_1.4.3                 blotter_0.9.1741             
## [15] PerformanceAnalytics_1.4.4000 FinancialInstrument_1.2.0    
## [17] quantmod_0.4-5                TTR_0.23-1                   
## [19] xts_0.9.874                   zoo_1.7-13                   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.4       plyr_1.8.3        formatR_1.3      
##  [4] iterators_1.0.8   tools_3.2.3       digest_0.6.9     
##  [7] gtable_0.1.2      evaluate_0.8.3    shiny_0.13.2     
## [10] DBI_0.3.1         yaml_2.1.13       parallel_3.2.3   
## [13] stringr_1.0.0     grid_3.2.3        R6_2.1.2         
## [16] rmarkdown_0.9.5.9 bookdown_0.0.62   magrittr_1.5     
## [19] scales_0.3.0      codetools_0.2-14  assertthat_0.1   
## [22] colorspace_1.2-6  mime_0.4          xtable_1.8-2     
## [25] httpuv_1.3.3      stringi_1.0-1     miniUI_0.1.1     
## [28] munsell_0.4.2     chron_2.3-47