Introduction

This is a hands-on book for programmers who want to learn about how C++ is used in the financial industry. The book concentrates on the parts of the language that are more frequently used to write financial software, including the STL (standard template library), templates, and support for numerical libraries. I also describe many of the important problems in financial engineering that are part of the day-to-day work of financial programmers and quantitative analysts in investment banks and hedge funds.

The book provides how-to examples that cover all the major tools and concepts used to build working solutions for financial applications. Each chapter teaches readers how to use advanced C++ concepts as well as the basic building libraries used by modern C++ developers, such as the STL and Boost. I discuss how to create correct and efficient applications, leveraging knowledge of object-oriented and template-based programming. I assume only a basic knowledge of C and C++, and build on these concepts to explain techniques already mastered by developers who are familiar with modern C++.

In the process of writing this book, I was concerned with providing a great value for readers who are trying to use their programming knowledge to become proficient in the style of programming used in financial institutions such as banks, hedge funds, and other companies in the financial industry. However, I have introduced the topics covered in the book in a logical and structured way, so that even novice programmers will be able to absorb the most important topics and competencies necessary to develop financial applications in C++.

An important feature of the book is its focus on specific themes and practical solutions for financial problems. While the emphasis is not on the theoretical aspects of finance, I do discuss topics such as numerical algorithms, integration techniques, and differential equations for derivative valuation. Moreover, the reader will gain a good understanding of how to model such problems using modern C++ concepts.

The financial literature for programmers typically has a large number of books written from an academic standpoint, with most of the time spent on the discussion of mathematics concepts behind algorithms, rather than the software engineering challenges that developers need to overcome ers. Therefore, in this book, I decided to focus on working solutions for common programming problems, in the form of code examples, offering readers much more value for their reading efforts.

Audience

This book is intended for readers who already have a working knowledge of programming in C, C++, or another mainstream language. These are usually professionals or advanced students in computer science, engineering, physics, and mathematics, who have an interest in learning C++ financial programming either for personal improvement or for professional reasons. The book is also directed at practitioners of C++ programming in financial institutions, who would use the book as a ready-to-use reference for common development problems.

By reading this book, you will learn how to use modern C++ techniques to implement practical applications. Being a multi-paradigm language, C++ is used slightly differently in each application area. Therefore, skills that are valuable for developing desktop applications are not necessarily the same as those used to write high-performance software. A large part of major high-performance financial applications are written in C++, which means that programmers who want to enter this lucrative market need to acquire a working knowledge of a few specific and relevant parts of the language. This book therefore would be an excellent choice for developers who want to advance their knowledge effectively, while learning one of the most sought after and marketable skill sets for modern application and high-performance software development.

Content Overview

Here is a brief overview of the contents of each chapter.

Chapter 1The Fixed Income Market: The fixed income market is a large part of the financial engineering industry, and it presents unique computational challenges for its practitioners. C++ programming is widely used in this area, offering the ability to compute rates and cash flow variations with incredible speed, as readers will learn in this chapter. I present C++ coding examples that can be used in the solution of some of the most common problems occurring in fixed income markets. I include C++ algorithms for topics such as (1) interest rate calculation, (2) present value computation, (3) cash flows, and (4) valuation of bonds.

Chapter 2The Equities Market: Equity markets are multifaceted and offer a great variety of investment vehicles. As a result, the number and complexity of computational techniques used for financial analysis of equity markets continues to grow. In this chapter I present C++ examples for a few selected problems occurring in the equities markets and their derivatives. I cover programming topics such as the following: (1) moving average computation, (2) calculating volatility, (3) computing instrument correlation, and (3) calculating fundamental indicators.

Chapter 3–C++ Programming Techniques in Finance: The C++ language was created as an extension of C, which means that most programs written in C are also valid C++ programs. However, good C++ programs need to make use of high-level features made available by the language to control program complexity. This is especially important for financial applications, where we want to create fast and expressive applications. In this chapter I explore fundamental techniques that financial C++ programmers use to write better code with less effort, including (1) class templates, (2) auto pointers, (3) shared pointers, (4) resource acquisition is initialization (RAII), (5) automatic type detection, (6) exception handling, and (7) operator overloading.

Chapter 4–Common Libraries for Financial Applications: Modern coding in C++ uses libraries that simplify the creation of fast, standard-conforming classes. The STL offers a set of generic, standard containers that can be used in almost any situation. Knowing how to use the STL well is one of the main skills necessary for effective C++ programming. Another common set of classes is contained in the Boost libraries, which are usually the basis for the next version of the C++ standard. Readers will learn about topics such as (1) STL containers, (2) STL algorithms, (3) boost libraries, and (4) date and time handling.

Chapter 5—Designing Numerical Classes: At the heart of financial application applications is a set of well-designed numerical classes. This chapter tells you how to create numerical classes that will perform efficiently when used in production code. You will also see examples in C++ that show how to integrate with existing numerical classes and algorithms. You will learn how to (1) implement a matrix class, (2) perform calculations at compilation time with templates, (3) represent ratios with C++ templates, and (4) generate statistical data.

Chapter 6—Plotting Financial Data: A common activity in financial programming is the generation of data that needs to be visualized by traders or other financial stakeholders. Most of the time, the data needs to be plotted in the form of a chart for easy visualization. I give a few examples that show how to plot data in C++ programs using common libraries. You will learn about topics such as (1) using Gnuplot to plot data, (2) designing a class to create Gnuplot charts, and (3) plotting from a GUI (graphical user interface) application using Qt.

Chapter 7—Linear Algebra: Linear algebra (LA) techniques are used throughout the area of financial engineering. Therefore, it is important to understand how the traditional methods of LA can be applied in C++. With this goal in mind, I present a few examples that show how to use some of the most common LA algorithms. In this chapter you will also learn about (1) integrating existing LA libraries into your code, (2) basic LA operations, (3) the BLAS library, and (4) calculating the determinant of a matrix with BLAS.

Chapter 8—Interpolation: Interpolation is a commonly used technique that finds a mathematical function approximating a set of points. Fast interpolation is the secret for high-performance algorithms in several areas of financial engineering. This chapter will show you programming samples that cover a few of the most common interpolation methods, with efficient implementation in C++. The main techniques discussed in this chapter are (1) linear interpolation and (2) polynomial interpolation.

Chapter 9—Calculating Roots of Equations: Equations are one of building blocks of algorithms in financial engineering, and it is important to be able to calculate equation roots efficiently. In this chapter you will find algorithms for different methods of calculating equation roots, along with explanations of how they work and when they should be used. Topics include (1) the bisection method, (2) the secant method, and (3) Newton’s method.

Chapter 10–Numerical Integration: Function integration is a common part of many financial algorithms. However, it is hard to solve certain classes of equations exactly, and numerical methods need to be employed in such cases. In this chapter, you will see examples of C++ code that can be readily applied to common integration problems. I also discuss the performance and the accuracy of such methods .The programming examples in this chapter cover topics such as (1) the midpoint method, (2) the trapezoid method, and (3) Simpson’s method.

Chapter 11–Solving ODEs and PDEs: Differential equations are at the heart of many techniques used in the analysis of equity markets. There are several processes for solving and analyzing ordinary (ODE) and partial differential equations (PDE) that can be implemented in C++. In this chapter I present programming examples that cover aspects of ODEs and PDE modeling and application in C++. Topics covered include the following: (1) solving ODEs, (2) using the Runge-Kutta method, and (3) solving the Black-Scholes equation.

Chapter 12–Optimization: Optimization refers to a set of techniques used to find the minimum or maximum of a function. Optimization strategies are used in several areas of financial engineering. In this chapter I discuss programming techniques that can be used to implement common aspects of optimization algorithms. I provide a concise explanation of some techniques and how they are typically implemented in C++. You will learn about (1) modeling optimization problems, (2) interfacing with linear programming (LP) solvers, (3) solving two-dimensional LPs problems, and (4) mixed integer-programming models.

Chapter 13Asset and Portfolio Optimization: Portfolio managers have to face the issue of balancing a portfolio for optimal performance, depending on their predefined portfolio goals. Optimization-based techniques have been developed to deal with some of the most common portfolio construction problems. In this chapter we consider algorithms for portfolio optimization using C++. We consider how to design such optimization code in order to get results that are as fast and as accurate as possible. Topics include (1) creating a portfolio model, (2) performing resource allocation, and (3) using linear techniques for portfolio optimization.

Chapter 14–Monte Carlo Methods: Among other programming techniques used in equity markets analysis, Monte Carlo simulation has a special place due to its wide applicability and easy implementation. These methods can be used to forecast prices or to validate buying strategies, for example. In this chapter, I provide programming examples that can be used as part of simulation-based algorithms, with topics such as (1) random number generation, (2) optimization through Monte Carlo methods, and (3) simulation models for price forecasting.

Chapter 15–Extending Financial Libraries: C++ is a complete language that can be used to develop the most complex software. However, it is sometimes beneficial to combine C++ libraries with scripting languages that can simplify the creation of prototypes and other noncritical applications. In this chapter, I show you how to use the solutions and algorithms discussed in the text as external libraries for scripting languages that are commonly employed in the financial industry. In particular, you will learn how to (1) extend C++ with Python and (2) extend C++ with Lua scripts.

Chapter 16–Using C++ Code with R and Maxima: Financial algorithms in C++ can be used not only as part of executable code but also as part of other modeling and development environments. In this chapter I show you how to integrate financial libraries into two well-known simulation and modeling environments for financial analysis: R and Maxima. You will see how it is possible to create loadable modules for these environments, incorporating complex C++ algorithms in a way that they are ready to use from scripts written in R and Maxima.

Chapter 17–Multithreading: Financial applications have very stringent performance requirements. A common way to improve response time is to use concurrency and parallel programming techniques, such as multithreading. C++ can be used to write very responsive multithreaded applications, and in this chapter I explore algorithms for creating and managing threads, with applications to financial problems. I also cover the important topic of data access synchronization. Topics include (1) creating threads, (2) protecting shared memory, and (3) synchronization techniques.

Appendix A–C++11/14 Features: C++ is an evolving language, and in the last few years we have seen a renewed effort to bring much needed updates. The latest efforts are the C++11 and C++14 standards, and major C++ compilers are incorporating these features at a fast pace. In this Appendix I cover examples that show how some of these features can improve your code and simplify the development of new programs and libraries. You will learn about new features such as (1) auto variables, (2) closures, (3) rvalues, and (4) standard library changes.

Compiling the Code Samples

The examples given in this book have all been tested on Windows using the MingW gcc compiler, and on Mac OS X using the Xcode 6 IDE. You should be able to build the code, however, using any standards-compliant C++ compiler that implements the C++11 standard. For example, gcc is available on most platforms, and Microsoft Visual Studio will also work on Windows.

If you use Mac OS X and don’t have Xcode installed in your computer, you can download it for free from Apple’s developer web site at http://developer.apple.com.

If you instead want to use MingW on Windows, you can download it from the web site http://www.mingw.org.

Once MingW is installed, start the command prompt from the MingW program group in the start menu. Then, you can type gcc to check that the compiler is properly installed.

To download the complete set of examples, visit the web page for this book at http://coliveira.net.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.242