Welcome to Practical Quantitative Finance with R . R is not only a general-purpose statistical computing language, but also an excellent choice for fast prototyping quant models and backtesting trading strategies in quantitative finance. This book focuses on introducing R as the scripting environment choice for quantitative finance application development. It will provide the basic R programming, quantitative analysis, and mathematical tools you need to develop real-world financial applications. All the techniques described in this book can be directly used to solve those problems that many quants face every day. I hope this book will be useful for quant developers/analysts, individual traders, and students of all skill levels.

In recent years, quantitative finance has been an attractive field due to the intellectual challenge and high remuneration. Many scientists, engineers, and students wish to change their careers to become a quant developer/analyst in investment banks and hedge fund firms. Most of them have solid background in mathematics, statistical analysis, physics modeling, and programming, but lack knowledge and experience in quantitative finance. A question that they constantly ask is “what do I need to prepare myself to become a quant developer or analyst?” This book will provide answer to this question and prepare you for careers in quantitative analysis and development.

On the other hand, more and more individuals want to become independent (“retail”) quantitative traders who are looking to start their own quantitative or algorithmic trading business. The most common issue they are facing is what kind of background do they need in order to be successful in quantitative trading? Most of those individuals received their advanced degrees in physics, mathematics, engineering, or computer science. This kind of training in hard sciences will give them an edge in quantitative analysis and pricing complex derivative instruments. However, the capability to quickly convert trading ideas into trading strategies and the programming skill in implementing the automatic trading system are equally important. This book will prepare you with all the necessary analysis and programming techniques to become a well-equipped individual quant trader.

So what programming languages are most commonly used in quantitative finance? No doubt about it, C++ is traditionally associated with finance applications for pricing complex derivative securities, and much of the older financial infrastructure is also based on C++. In general, C++, C#, and Java tend to be used for more application and infrastructure development. In my previous book, Practical C# and WPF for Financial Markets , I have chosen C# and WPF as the programming framework in developing various business applications in financial markets. The reason for using C# in that book is that C# is relatively easy to learn in comparison to C++. You can learn C# and use it to develop financial applications quickly. People with a background in VBA, R, or Java will find the transition to C# much easier than the transition to C++. Furthermore, in many cases developers’ productivity levels are much higher than those achieved with C++. It is also possible to create interoperable .NET applications that contain different technologies such as C++, VBA, Matlab, and R legacy code.

In addition to create business applications and infrastructure, we also need a script-programming environment that allows for rapid prototyping of an idea, provides us instant feedback, and enables the data and result visualization in an efficient manner. In this regard, R, Python, and Matlab provide the scripting environment and can be used as prototyping quant models. All of these three are high-level languages with time series, linear/matrix/vectorization computation, and fast prototyping for trading strategies. They all have strong communities developing code for numerical analysis and quantitative finance. R is especially strong in the finance community. In my work as a quant, I have used all of these three script languages extensively for prototyping quant models and strategy backtesting. You can use any one of them as long as you are comfortable with it. In this book, I will choose R as our scripting environment, mainly because R is a free open-source language, and has strong packages in quantitative finance, as well as efficient data visualization power.

The main advantage of R is that it is free, extremely flexible and extensible. R is not only free, but also open source. You can see the source code, and change it as per your own requirements. People across different disciplines around world reviewed the core of the R system and contributed to make it better. You can use R to perform data processing and analysis and to produce a variety of graphics. R has a substantial collection of packages, which are written by experts in quantitative finance. That is why, whether you are a quant analyst/developer, or individual trader, you should find a set of functions that serve your purpose. The graphic system in R is one of the most powerful tools in this era, and you have full control over every part of graphics produced in R. R is now becoming one of the platforms to implement and prototype the research work, quant models, and trading strategies. You should be able to find an R package suitable for the most recent developments in quantitative finance.

I write this book with the intention of providing a complete and comprehensive explanation of R programming and usage of the relevant R packages in quantitative finance. The book pays special attention to creating various business applications and reusable R libraries that can be directly used in real-world finance applications. Much of this book contains original work based on my own programming experience when I was prototyping quant models, pricing framework, and trading strategies in quantitative financial field.

Practical Quantitative Finance with R provides everything you need to create your own advanced applications in quantitative finance and reusable packages using R. It shows you how to use R and relevant R packages to create a variety of financial applications that range from simple market data collection, data visualization, quantitative analysis to pricing equity options and complex fixed income instruments, machine learning, trading strategy development, and portfolio optimization. I will try my best to introduce you to R programming in quantitative finance in a simple way – simple enough to be easily followed by a quant or individual trader who has basic prior experience in developing business applications using R.

What this Book Includes

This book and its sample code listings, which are available for download at my website at www.drxudotnet.com, provide you with:

A complete, in-depth instruction on practical quantitative finance programming with R. After reading this book and running the example programs, you will be able to create various sophisticated business applications in quantitative finance.

Ready-to-run example programs that allow you to explore the quantitative finance programming techniques described in the book. You can use these examples to understand how the algorithms in finance work. You can modify the code examples or add new features to them to form the basis of your own projects. Some of the example code listings provided in this book are already sophisticated programming packages in quantitative finance that you can use directly in your own real-world business applications.

Many R functions in the sample code listings that you will find useful in your quant development. These functions include charting libraries, various quantitative analysis models, pricing engines for options and fixed income instruments, machine learning for trading strategy development and back-testing, and the other useful utility classes. You can extract these functions and plug them into your own business applications.

Is This Book for You?

You do not have to be an experienced quant developer/analyst or R programmer to use this book. I designed this book to be useful to people of all levels of R programming experience and financial background. In fact, I believe that if you have some prior experience with the quantitative analysis and development, programming language such as C++, Java, VBA, C#, Matlab, or Python, you will be able to sit down in front of your computer, startup RStudio, follow the examples provided in this book, and quickly become proficient with quantitative application development using R. For those of you who are already experienced quant analyst/developer or R user, I believe this book has much to offer as well. A great deal of the information in this book about R programming in quantitative finance is not available in other tutorial and reference books. In addition, you can use most of the example programs in this book directly in your own real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything related to quantitative finance application development using R.

Perhaps you are a scientist, an engineer, a mathematician, or a student, rather than a professional quant developer/analyst; nevertheless, this book is still a good bet for you. In fact, my own background is in theoretical physics, a field involving extensive physical modeling, numerical calculations, and graphical representations of calculated data. I devoted my effort to this field for many years, starting from undergraduate up to PhD. My first computer experience was with FORTRAN. Later on, I had programming experience with Basic, C, C++, MATLAB, VBA, C#, WPF, python, and R. I always tried to find an ideal development tool that would allow me not only to prototype my research work easily but also to represent data graphically. The R development environment made it possible to develop such integrated applications. I have been able to use R successfully to prototype my quant models and trading strategies quickly for quantitative analysis when I have worked on Wall Street.

Quant analysts/developers, and individual quant traders can use the majority of the example programs in this book routinely. Throughout the book, I will emphasize the usefulness of R programming to real-world quantitative finance applications. If you follow the instructions presented in this book closely, you will easily be able to develop various practical business applications in quantitative finance from linear analysis, machine learning to pricing engines, trading strategy development, asset allocation, and portfolio optimization. At the same time, I will not spend too much time discussing programming style, execution speed, and code vectorization/optimization, because a plethora of books and tutorial information out there already deal with these topics. Most of the example programs you will find in this book omit error handlings. This makes the code easier to understand by focusing only on the key concepts and practical applications.

What Do You Need to Use This Book?

You will need no special equipment to make the best use of this book and understand the algorithms. To run and modify the sample programs, you will need to install R and RStudio on your computer. R and RStudio are both free, open-source software, available for all commonly used operating systems, including Windows, Mac OS X, and Linux systems. You can find the installation instructions for different systems at http://cran.r-project.org (for R itself) and http://www.rstudio.com (for RStudio). The RStudio is an R IDE for an integrated editor, execution, and graphical environment. The example code provided with this book was developed and tested on Windows 10 operating system with R version 3.2.3 and RStudio version 0.99.891.

How the Book Is Organized

This book is organized into eleven chapters, each of which covers a different topic about quantitative finance applications using R. The following summaries of each chapter should give you an overview of the book’s content:

Chapter 1, Introduction to R
This chapter introduces the basics of R and reviews some of the general aspects of R programming, including the built-in functions, math operations, data frame, data table, and user-defined functions.

Chapter 2, Market Data
This chapter contains instructions on how to use R to interact with market data providers’ API and how to get the free market data from online data sources. It describes methods to retrieve various market data, including the end of the day (EOD) stock data, intraday data, interest rate data, foreign exchange rate data, and option chain data.

Chapter 3, Plots and Graphics in R
Data visualization plays a critical role in quantitative finance and trading. Quant analysts and traders need to monitor the real-time changes in market and trading signals visually on their screen. This chapter demonstrates how to use the R built-in plotting capability as well as the other powerful charting and graphic packages, including ggplot2, plot3D, rgl, etc.

Chapter 4, Stock Charts and Technical Indicators
This chapter discusses various stock charts and technical indicators. We often use stock charts (line, bar, candlestick) and indicators in quantitative analysis. A technical indicator is just a mathematical calculation based on historic market data including price and volume that is used to predict market direction. I will demonstrate how to use the common indicators from the TTR package to display different indicators on screen and how to extract the output results from the indicators.

Chapter 5, Option Pricing
This chapter covers the Black-Scholes formula used for options pricing. It shows several different implementations for calculating the price and Greeks of the European and American options. It also discusses how to use the open source quant libraries to price various exotic options, including barrier options.

Chapter 6, Pricing Fixed-Income Instruments
This chapter discusses pricing for the fixed-income instruments, including interest rates, bonds, and credit default swaps, as well as various related topics, such as cash flows, term structures, yield curves, discount factors, and zero-coupon bonds. I will also provide the detailed procedures on how to use QuantLib to price these complex financial instruments.

Chapter 7, Linear Analysis
This chapter presents the most fundamental analysis approach in quantitative finance based on linear analysis. I will discuss how to develop different business applications using the linear regression, principal component analysis (PCA), and correlation.

Chapter 8, Time Series Analysis
This chapter introduces some common modeling techniques for time series analysis, including the autoregressive moving average (ARMA), the autoregressive integrated moving average (ARIMA), and the volatility modeling generalized autoregressive conditional heteroscedasticity (GARCH). It also examines stationary of a time series using different approaches, such as augmented Dickey-Fuller (ADF) unit root test and Johansen test.

Chapter 9, Machine Learning
This chapter represents the advanced quantitative analysis techniques: machine learning. Machine-learning technique has become one of the most promising fields in quantitative finance. It is widely used in quantitative finance for predicting the future stock prices. This chapter will concentrate on the supervised learning and covers several commonly used machine-learning algorithms in finance, including the K-nearest neighbors, random forest, support vector machines, and neural networks.

Chapter 10, Trading Strategies and Backtesting
This chapter presents several trading strategies using the simple quantitative analysis techniques, including the moving average and linear regression, as well as the commonly used technical indicators. I will also discuss a long-short based backtesting framework and a useful backtesting package named quantstrat, which allows you to examine the historical performance of your trading strategies for single stock trading, stock pairs trading, and trading for multi-asset portfolios.

Chapter 11, Portfolio Optimization
This chapter presents introduction to portfolio optimization. Expected return and risk are the most important parameters with regard to optimal portfolios. The goal of portfolio optimization is to find the best asset allocation strategy that maximizes portfolio’s return and minimizes its risk. This chapter will discuss portfolio optimization using several different techniques, including, Markowitz’s classical mean-variance method, mean-CVaR (conditional risk at risk), and the portfolioAnalytics package.

Using Code Examples

You may use the code in this book in your own applications and documentation. You do not need to contact the author or the publisher for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation also requires permission. Integrating the example code from this book into commercial products is not allowed without written permission of the author.

Customer Support

I am always interested in hearing from readers, and enjoy learning of your thoughts on this book. You can send me comments by e-mail to: jxu at DrXuDotNet.com. I also provide updates, bug fixes, and ongoing support via my website:

www.DrXuDotNet.com

You can also obtain the complete source code for all of examples in this book from the foregoing website.