Backtesting Strategies with R Tim Trice

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: Advanced R 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)