Skip to main content
Top

2016 | Book

Automated Trading with R

Quantitative Research and Platform Development

insite
SEARCH

About this book

This book explains the broad topic of automated trading, starting with its mathematics and moving to its computation and execution. Readers will gain a unique insight into the mechanics and computational considerations taken in building a backtester, strategy optimizer, and fully functional trading platform.

Automated Trading with R provides automated traders with all the tools they need to trade algorithmically with their existing brokerage, from data management, to strategy optimization, to order execution, using free and publically available data. If your brokerage’s API is supported, the source code is plug-and-play.

The platform built in this book can serve as a complete replacement for commercially available platforms used by retail traders and small funds. Software components are strictly decoupled and easily scalable, providing opportunity to substitute any data source, trading algorithm, or brokerage. The book’s three objectives are:

To provide a flexible alternative to common strategy automation frameworks, like Tradestation, Metatrader, and CQG, to small funds and retail traders.

To offer an understanding the internal mechanisms of an automated trading system.

To standardize discussion and notation of real-world strategy optimization problems.

What you’ll learn

Programming an automated strategy in R gives the trader access to R and its package library for optimizing strategies, generating real-time trading decisions, and minimizing computation time.

How to best simulate strategy performance in their specific use case to derive accurate performance estimates.

Important machine-learning criteria for statistical validity in the context of time-series.

An understanding of critical real-world variables pertaining to portfolio management and performance assessment, including latency, drawdowns, varying trade size, portfolio growth, and penalization of unused capital.

Who This Book Is For

This book is for traders/practitioners at the retail or small fund level with at least an undergraduate background in finance or computer science. Graduate level finance or data science students.

Table of Contents

Frontmatter

Problem Scope

Frontmatter
Chapter 1. Fundamentals of Automated Trading
Abstract
The fundamental goal of trading is to maximize risk-adjusted return. When developing strategies, we will simulate trading performance in an attempt to maximize risk-adjusted return in simulation. There are many ways to measure risk-adjusted return. They involve examining the shape of the equity curve and the return series.
Chris Conlan

Building the Platform

Frontmatter
Chapter 2. Networking Part I
Abstract
In Part 2 of this book, we will be introducing topics in order of appearance in the source code of our trading platform. This chapter will discuss acquisition, storage, and updating of data using free APIs. Our trading platform will contain various processes that run automatically throughout the day, and our data needs to be in our R environment for us to work with it. We may want to run R and RStudio to do unrelated jobs between those times, which means we need a way to store the data in files in our computer so that they can be fetched when the analysis starts. In this chapter, we will explore various ways to fetch, store, and load data. We will ultimately settle on the most efficient method to execute the following algorithm. This algorithm will be referred to as “the algorithm” throughout the chapter.
Chris Conlan
Chapter 3. Data Preparation
Abstract
There are a handful of specific reasons why a certain stock may have NA values on any given day. We want to diagnose and treat these reasons appropriately to ensure the validity of our simulation results.
Chris Conlan
Chapter 4. Indicators
Abstract
Indicators are at heart of the trading strategy. They make it unique and profitable. They can be single computations or a long series of analyses.
Chris Conlan
Chapter 5. Rule Sets
Abstract
Rule sets link the indicator to the trading decisions. We have given many simple examples in the discussions about indicators, but rule sets tend to get very complex when we include money management elements. It is dangerous to completely decouple the trading decision aspects from the money management aspects of rule sets. In other words, we do not want to decouple the offense from the defense. They should interact and complement each other in an optimal manner discovered through research and optimization. We will discuss common rule sets for making trading decisions as they correspond to certain types of indicators. We will then discuss money management considerations, both integrated and decoupled from generation of trading decisions.
Chris Conlan
Chapter 6. High-Performance Computing
Abstract
In Chapter 7, we will be building our first iteration of a full simulator. We need to cover some high-performance computing concepts so our simulator is not painfully slow. We will begin with a general discussion of high-performance computing in R and then move to implementing different methods in both Windows and UNIX systems. Windows and UNIX systems require different configurations and packages for multicore computing in R.
Chris Conlan
Chapter 7. Simulation and Backtesting
Abstract
In this chapter, we will use the data and functions established thus far to build a backtester to simulate the results of trading with a given strategy. We will run our simulator with a few example strategies. We will introduce many practical trading considerations as we construct sample strategies.
Chris Conlan
Chapter 8. Optimization
Abstract
Optimization is more than finding the best simulation results. It is itself a complex and evolving field that, subject to certain information constraints, allows data scientists, statisticians, engineers, and traders alike to perform reality checks on modeling results. We will discuss common ideological pitfalls and how to avoid them.
Chris Conlan
Chapter 9. Networking Part II
Abstract
In this chapter, we will discuss the available APIs for automating your trading strategies. The structure of commercially available APIs varies widely with each requiring a specific set of skills and software considerations. We will survey the most popular and accessible APIs to help traders determine the optimal system to pursue or how to best integrate with an existing brokerage.
Chris Conlan

Production Trading

Frontmatter
Chapter 10. Organizing and Automating Scripts
Abstract
This chapter will cover CRON jobs for UNIX machines and task scheduling for Windows machines to automate routine execution of trading scripts. We will discuss which jobs should be run when, referencing Appendix A frequently for production-ready code examples.
Chris Conlan
Chapter 11. Looking Forward
Abstract
Our platform stands out in transparency and flexibility. R is very legible but generally slow. We have made efforts to speed up our platform but will always be slower than the equivalent multicore program in C/C++.
Chris Conlan
Chapter 12. Source Code
Abstract
This appendix will include all the production code covered in this book organized into an R project directory. An R project directory is a traditional file hierarchy of source code files that will be called by delegating scripts in the root of the project directory. In our case, we will continue using the root directory named in variable rootdir in the “Setting Up Directories” section of Chapter 2. We will create some new directories for holding source code and declare all of our directories in a list format to keep them organized.
Chris Conlan
Chapter 13. Appendix B: Scoping in Multicore R
Abstract
This book makes frequent use of the foreach package in R to parallelize computations. This package, developed by Steve Weston of Revolution Analytics, allows users to write operating system–independent (OS-independent) multicore code.
Chris Conlan
Backmatter
Metadata
Title
Automated Trading with R
Author
Chris Conlan
Copyright Year
2016
Publisher
Apress
Electronic ISBN
978-1-4842-2178-5
Print ISBN
978-1-4842-2177-8
DOI
https://doi.org/10.1007/978-1-4842-2178-5

Premium Partner