0% found this document useful (0 votes)
735 views

Practical C++ Financial Programming: Carlos Oliveira

Practical Finance C++ programming

Uploaded by

jeetutor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
735 views

Practical C++ Financial Programming: Carlos Oliveira

Practical Finance C++ programming

Uploaded by

jeetutor
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Practical C++ Financial

Programming

Carlos Oliveira
Practical C++ Financial Programming
Copyright © 2015 by Carlos Oliveira
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with
reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed
on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or
parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its
current version, and permission for use must always be obtained from Springer. Permissions for use may be
obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under
the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-6715-7
ISBN-13 (electronic): 978-1-4302-6716-4
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director: Welmoed Spahr
Lead Editor: Jonathan Gennick
Developmental Editor: James Markham
Technical Reviewer: Don Reamey
Editorial Board: Steve Anglin, Mark Beckner, Gary Cornell, Louise Corrigan, Jim DeWolf,
Jonathan Gennick, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie,
Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing, Matt Wade,
Steve Weiss
Coordinating Editor: Jill Balzano
Copy Editor: Lori Jacobs
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York,
233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and
the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use.
eBook versions and licenses are also available for most titles. For more information, reference our Special
Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this text is available to readers
at www.apress.com. For detailed information about how to locate your book’s source code, go to
www.apress.com/source-code/.
This book is dedicated to my wife and son,
my biggest inspirations for work.
Contents at a Glance

About the Author����������������������������������������������������������������������������������������������������xix


About the Technical Reviewer��������������������������������������������������������������������������������xxi
Acknowledgments������������������������������������������������������������������������������������������������xxiii
Introduction�����������������������������������������������������������������������������������������������������������xxv

■■Chapter 1: The Fixed Income Market��������������������������������������������������������������������� 1


■■Chapter 2: The Equities Market��������������������������������������������������������������������������� 25
■■Chapter 3: C++ Programming Techniques in Finance����������������������������������������� 55
■■Chapter 4: Common Libraries for Financial Applications������������������������������������ 83
■■Chapter 5: Designing Numerical Classes ���������������������������������������������������������� 111
■■Chapter 6: Plotting Financial Data �������������������������������������������������������������������� 137
■■Chapter 7: Linear Algebra ��������������������������������������������������������������������������������� 155
■■Chapter 8: Interpolation������������������������������������������������������������������������������������ 171
■■Chapter 9: Calculating Roots of Equations�������������������������������������������������������� 183
■■Chapter 10: Numerical Integration�������������������������������������������������������������������� 203
■■Chapter 11: Solving ODEs and PDEs ����������������������������������������������������������������� 221
■■Chapter 12: Optimization����������������������������������������������������������������������������������� 237
■■Chapter 13: Asset and Portfolio Optimization��������������������������������������������������� 255
■■Chapter 14: Monte Carlo Methods �������������������������������������������������������������������� 273
■■Chapter 15: Extending Financial Libraries ������������������������������������������������������� 291

v
■ Contents at a Glance

■■Chapter 16: Using C++ with R and Maxima ����������������������������������������������������� 313


■■Chapter 17: Multithreading������������������������������������������������������������������������������� 325
■■Appendix A: C++11/14 Features ���������������������������������������������������������������������� 343

Index��������������������������������������������������������������������������������������������������������������������� 359

vi
Contents

About the Author����������������������������������������������������������������������������������������������������xix


About the Technical Reviewer��������������������������������������������������������������������������������xxi
Acknowledgments������������������������������������������������������������������������������������������������xxiii
Introduction�����������������������������������������������������������������������������������������������������������xxv

■■Chapter 1: The Fixed Income Market��������������������������������������������������������������������� 1


Fixed Income Overview���������������������������������������������������������������������������������������������������� 2
Why Using C++���������������������������������������������������������������������������������������������������������������� 3
Calculating Simple Interest Rates������������������������������������������������������������������������������������ 5
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 5
Solution�������������������������������������������������������������������������������������������������������������������������������������������������� 5
How It Works������������������������������������������������������������������������������������������������������������������������������������������� 5
Complete Code��������������������������������������������������������������������������������������������������������������������������������������� 7
Sample Use��������������������������������������������������������������������������������������������������������������������������������������������� 9
Compound Interest����������������������������������������������������������������������������������������������������������� 9
Problem�������������������������������������������������������������������������������������������������������������������������������������������������� 9
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 10
How It Works����������������������������������������������������������������������������������������������������������������������������������������� 10
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 11
Sample Use������������������������������������������������������������������������������������������������������������������������������������������� 14
Modeling Cash Flows����������������������������������������������������������������������������������������������������� 14
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 14
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 14
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 17
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 19

vii
■ Contents

Modeling Bonds�������������������������������������������������������������������������������������������������������������� 20
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 20
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 20
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 21
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 23

Further Reference���������������������������������������������������������������������������������������������������������� 23
Conclusion���������������������������������������������������������������������������������������������������������������������� 23
■■Chapter 2: The Equities Market��������������������������������������������������������������������������� 25
Equities Market Concepts���������������������������������������������������������������������������������������������� 25
Moving Average Calculation������������������������������������������������������������������������������������������� 27
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 27
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 27
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 30
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 34

Calculating Volatility������������������������������������������������������������������������������������������������������� 35
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 35
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 35
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 37
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 40

Computing Instrument Correlation ��������������������������������������������������������������������������������� 41


Problem������������������������������������������������������������������������������������������������������������������������������������������������ 41
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 41
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 41
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 46
Calculating Fundamental Indicators������������������������������������������������������������������������������� 47
Problem������������������������������������������������������������������������������������������������������������������������������������������������ 47
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 47
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 48
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 53

Conclusion���������������������������������������������������������������������������������������������������������������������� 53

viii
■ Contents

■■Chapter 3: C++ Programming Techniques in Finance����������������������������������������� 55


Calculating Interest Rates for Investment Instruments�������������������������������������������������� 55
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 56
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 57
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 62

Creating Financial Statement Objects���������������������������������������������������������������������������� 62


Solution������������������������������������������������������������������������������������������������������������������������������������������������ 62
Smart Pointers�������������������������������������������������������������������������������������������������������������������������������������� 62
Using Auto Pointers������������������������������������������������������������������������������������������������������������������������������ 63
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 64
Transferring Ownership������������������������������������������������������������������������������������������������������������������������ 67
Pitfalls of Auto Pointers������������������������������������������������������������������������������������������������������������������������ 67

Determining Credit Ratings�������������������������������������������������������������������������������������������� 68


Solution������������������������������������������������������������������������������������������������������������������������������������������������ 68
Using Shared Pointers�������������������������������������������������������������������������������������������������������������������������� 68
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 69
Using the auto Keyword������������������������������������������������������������������������������������������������������������������������ 72

Collecting Transaction Data�������������������������������������������������������������������������������������������� 73


Solution������������������������������������������������������������������������������������������������������������������������������������������������ 73
Exception Handling������������������������������������������������������������������������������������������������������������������������������� 73
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 74
Implementing Vector Operations������������������������������������������������������������������������������������ 77
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 77
Operator Overloading���������������������������������������������������������������������������������������������������������������������������� 78
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 79

Conclusion���������������������������������������������������������������������������������������������������������������������� 81

ix
■ Contents

■■Chapter 4: Common Libraries for Financial Applications������������������������������������ 83


Handling Analyst Recommendations������������������������������������������������������������������������������ 84
Solution������������������������������������������������������������������������������������������������������������������������������������������������ 84
More About STL Vectors and Maps������������������������������������������������������������������������������������������������������� 84
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 87

Performing Time-Series Transformations����������������������������������������������������������������������� 91


Solution������������������������������������������������������������������������������������������������������������������������������������������������ 91
Using STL Algorithms��������������������������������������������������������������������������������������������������������������������������� 93
Complete Code������������������������������������������������������������������������������������������������������������������������������������� 93
Running the Code��������������������������������������������������������������������������������������������������������������������������������� 96

Copying Transaction Files����������������������������������������������������������������������������������������������� 96


Solution������������������������������������������������������������������������������������������������������������������������������������������������ 96
Boost Libraries�������������������������������������������������������������������������������������������������������������������������������������� 96
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 100
Running the Code������������������������������������������������������������������������������������������������������������������������������� 103

Handling Dates������������������������������������������������������������������������������������������������������������� 103


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 103
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 105
Running the Code������������������������������������������������������������������������������������������������������������������������������� 109

Conclusion�������������������������������������������������������������������������������������������������������������������� 109
■■Chapter 5: Designing Numerical Classes ���������������������������������������������������������� 111
Representing Matrices in C++������������������������������������������������������������������������������������� 111
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 111
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 114

Using Templates to Calculate Factorials���������������������������������������������������������������������� 118


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 118
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 121
Running the Code������������������������������������������������������������������������������������������������������������������������������� 122

x
■ Contents

Representing Calmar Ratios at Compile Time�������������������������������������������������������������� 123


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 123
Representing Calmar Ratios��������������������������������������������������������������������������������������������������������������� 124
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 125
Running the Code������������������������������������������������������������������������������������������������������������������������������� 127

Generating Statistical Data������������������������������������������������������������������������������������������� 127


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 127
Probability Distributions��������������������������������������������������������������������������������������������������������������������� 128
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 131
Running the Code������������������������������������������������������������������������������������������������������������������������������� 134

Conclusion�������������������������������������������������������������������������������������������������������������������� 135
■■Chapter 6: Plotting Financial Data �������������������������������������������������������������������� 137
Plotting with Gnuplot���������������������������������������������������������������������������������������������������� 137
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 137
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 143
Running the Code������������������������������������������������������������������������������������������������������������������������������� 147

Plotting Data from a GUI����������������������������������������������������������������������������������������������� 147


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 147
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 149
Running the Code������������������������������������������������������������������������������������������������������������������������������� 153

Conclusion�������������������������������������������������������������������������������������������������������������������� 153
■■Chapter 7: Linear Algebra ��������������������������������������������������������������������������������� 155
Using Basic Linear Algebra Operations������������������������������������������������������������������������ 156
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 156
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 158

Using Matrix-Oriented Operations�������������������������������������������������������������������������������� 161


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 161
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 163
Running the Application���������������������������������������������������������������������������������������������������������������������� 168

xi
■ Contents

Calculate the Determinant of a Matrix������������������������������������������������������������������������� 168


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 168
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 169

Conclusion�������������������������������������������������������������������������������������������������������������������� 170
■■Chapter 8: Interpolation������������������������������������������������������������������������������������ 171
Linear Interpolation ����������������������������������������������������������������������������������������������������� 171
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 171
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 173
Running the Code������������������������������������������������������������������������������������������������������������������������������� 175

Polynomial Interpolation����������������������������������������������������������������������������������������������� 176


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 176
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 178
Running the Code������������������������������������������������������������������������������������������������������������������������������� 181

Conclusion�������������������������������������������������������������������������������������������������������������������� 181
■■Chapter 9: Calculating Roots of Equations�������������������������������������������������������� 183
Bisection Method �������������������������������������������������������������������������������������������������������� 183
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 183
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 186
Running the Code������������������������������������������������������������������������������������������������������������������������������� 189

The Secant Method������������������������������������������������������������������������������������������������������ 189


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 189
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 192
Running the Code������������������������������������������������������������������������������������������������������������������������������� 194

Newton’s Method��������������������������������������������������������������������������������������������������������� 195


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 195
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 197
Running the Code������������������������������������������������������������������������������������������������������������������������������� 200

Conclusion�������������������������������������������������������������������������������������������������������������������� 201

xii
■ Contents

■■Chapter 10: Numerical Integration�������������������������������������������������������������������� 203


The Midpoint Method��������������������������������������������������������������������������������������������������� 203
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 203
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 206
Running the Code������������������������������������������������������������������������������������������������������������������������������� 209

Trapezoid Method��������������������������������������������������������������������������������������������������������� 209


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 209
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 211
Running the Code������������������������������������������������������������������������������������������������������������������������������� 214

Using Simpson’s Method���������������������������������������������������������������������������������������������� 214


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 214
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 216
Running the Code������������������������������������������������������������������������������������������������������������������������������� 218

Conclusion�������������������������������������������������������������������������������������������������������������������� 219
■■Chapter 11: Solving ODEs and PDEs ����������������������������������������������������������������� 221
Solving Ordinary Differential Equations����������������������������������������������������������������������� 221
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 221
Euler’s Method ����������������������������������������������������������������������������������������������������������������������������������� 223
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 224
Running the Code������������������������������������������������������������������������������������������������������������������������������� 226
Runge-Kutta Method for Solving ODEs������������������������������������������������������������������������� 226
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 226
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 227
Running the Code������������������������������������������������������������������������������������������������������������������������������� 230

Solving the Black-Scholes Equation����������������������������������������������������������������������������� 231


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 231
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 232
Running the Code������������������������������������������������������������������������������������������������������������������������������� 236

Conclusion�������������������������������������������������������������������������������������������������������������������� 236

xiii
■ Contents

■■Chapter 12: Optimization����������������������������������������������������������������������������������� 237


Interfacing with a Linear Programming Solver������������������������������������������������������������ 237
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 237
Linear Programming Concepts����������������������������������������������������������������������������������������������������������� 238
Using LP Solver Libraries�������������������������������������������������������������������������������������������������������������������� 239
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 240
Running the Code������������������������������������������������������������������������������������������������������������������������������� 245

Solving Two-Dimensional Investment Problems���������������������������������������������������������� 245


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 245
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 247
Running the Code������������������������������������������������������������������������������������������������������������������������������� 249

Creating Mixed-Integer Programming Models������������������������������������������������������������� 250


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 250
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 251
Running the Code������������������������������������������������������������������������������������������������������������������������������� 253

Conclusion�������������������������������������������������������������������������������������������������������������������� 254
■■Chapter 13: Asset and Portfolio Optimization��������������������������������������������������� 255
Financial Resource Allocation�������������������������������������������������������������������������������������� 255
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 255
Implementation����������������������������������������������������������������������������������������������������������������������������������� 256
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 257
Running the Code������������������������������������������������������������������������������������������������������������������������������� 259
Portfolio Optimization��������������������������������������������������������������������������������������������������� 259
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 260
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 264
Running the Code������������������������������������������������������������������������������������������������������������������������������� 267

xiv
■ Contents

Extensions to Modified CAP������������������������������������������������������������������������������������������ 267


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 268
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 269
Running the Code������������������������������������������������������������������������������������������������������������������������������� 272

Conclusion�������������������������������������������������������������������������������������������������������������������� 272
■■Chapter 14: Monte Carlo Methods �������������������������������������������������������������������� 273
Monte Carlo-Based Integral Computation�������������������������������������������������������������������� 273
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 273
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 274
Running the Code������������������������������������������������������������������������������������������������������������������������������� 278

Simulating Asset Prices������������������������������������������������������������������������������������������������ 278


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 278
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 280
Running the Code������������������������������������������������������������������������������������������������������������������������������� 282

Calculating Option Probabilities����������������������������������������������������������������������������������� 282


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 282
Determining Profit Probabilities��������������������������������������������������������������������������������������������������������� 283
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 285
Running the Code������������������������������������������������������������������������������������������������������������������������������� 289

Conclusion�������������������������������������������������������������������������������������������������������������������� 289
■■Chapter 15: Extending Financial Libraries�������������������������������������������������������� 291
Exporting C++ Stock Handling Code to Python������������������������������������������������������������ 291
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 291
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 294
Running the Code������������������������������������������������������������������������������������������������������������������������������� 298

Exporting C++ Classes Directly to Python������������������������������������������������������������������� 299


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 299
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 301
Running the Code������������������������������������������������������������������������������������������������������������������������������� 303

xv
■ Contents

Using Lua as an Extension Language��������������������������������������������������������������������������� 304


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 304
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 305
Running the Code������������������������������������������������������������������������������������������������������������������������������� 311

Conclusion�������������������������������������������������������������������������������������������������������������������� 311
■■Chapter 16: Using C++ with R and Maxima������������������������������������������������������ 313
Integrating C++ with R������������������������������������������������������������������������������������������������� 313
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 313
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 315
Running the Code������������������������������������������������������������������������������������������������������������������������������� 317

Integrating with the Maxima CAS��������������������������������������������������������������������������������� 319


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 319
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 321
Running the Code������������������������������������������������������������������������������������������������������������������������������� 323

Conclusion�������������������������������������������������������������������������������������������������������������������� 324
■■Chapter 17: Multithreading������������������������������������������������������������������������������� 325
Creating Threads with the Pthreads Library����������������������������������������������������������������� 326
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 326
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 328
Running the Code������������������������������������������������������������������������������������������������������������������������������� 331
Calculating Options Probabilities in Parallel����������������������������������������������������������������� 331
Solution���������������������������������������������������������������������������������������������������������������������������������������������� 331
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 332
Running the Code������������������������������������������������������������������������������������������������������������������������������� 336

Using Mutexes to Prevent Unsynchronized Access������������������������������������������������������ 337


Solution���������������������������������������������������������������������������������������������������������������������������������������������� 337
Complete Code����������������������������������������������������������������������������������������������������������������������������������� 338
Running the Code������������������������������������������������������������������������������������������������������������������������������� 341

Conclusion�������������������������������������������������������������������������������������������������������������������� 342

xvi
■ Contents

■■Appendix A: C++11/14 Features����������������������������������������������������������������������� 343


Automatic Type Detection��������������������������������������������������������������������������������������������� 344
Lambdas����������������������������������������������������������������������������������������������������������������������� 345
User-Defined Literals���������������������������������������������������������������������������������������������������� 346
Range-based for����������������������������������������������������������������������������������������������������������� 346
Rvalue References������������������������������������������������������������������������������������������������������� 347
New Function Declarator Syntax and Decltype������������������������������������������������������������ 349
Delegating Constructors����������������������������������������������������������������������������������������������� 350
Inheriting Constructors������������������������������������������������������������������������������������������������� 351
Generalized Attributes�������������������������������������������������������������������������������������������������� 352
Generalized Constant Expressions������������������������������������������������������������������������������� 352
Null Pointer Constant��������������������������������������������������������������������������������������������������� 353
Defaulted and Deleted Member Functions������������������������������������������������������������������� 354
Right-Angle Brackets��������������������������������������������������������������������������������������������������� 355
Initializer Lists�������������������������������������������������������������������������������������������������������������� 355

Index��������������������������������������������������������������������������������������������������������������������� 359

xvii
About the Author

Carlos Oliveira works in the area of quantitative finance, with more


than ten years of experience in creating scientific and financial models
in C++. During his career, Carlos has developed several large-scale
applications for financial companies such as Bloomberg L.P. and
F-Squared Investments. Carlos Oliveira obtained a PhD in Operations
Research and Systems Engineering from the University of Florida, an MSc
in Computer Science from UFC (Brazil), and a BSc in Computer Science
from UECE (Brazil). He has also performed academic research in the field
of combinatorial optimization, with applications in diverse areas such
as finance, telecommunications, computational biology, and logistics.
Carlos has written more than 30 academic papers on optimization, and
authored three books, including Mathematical Aspects of Network Routing
Optimization (Springer, 2010).
Carlos Oliveira currently lives in New Jersey with his wife and son (and a Beagle). You can contact
Carlos Oliveira at his web site, http://coliveira.net.

xix
  
About the Technical Reviewer

Don Reamey is currently a Senior Engineering Manager at Apptio


Corporation. Previously he worked at Microsoft as a Principal Software
Engineer and Program Manager. Don also managed engineering teams
at TIBCO Software and Bank of America. He enjoys learning and creating
new programming languages, creating web-based applications, and
playing guitar and weight lifting.

xxi
Acknowledgments

It would be very difficult to write a book like this without the help of a competent and dedicated group.
I would like to acknowledge the help of the editorial team at Apress for their great support.
First, thanks to Editor Jonathan Gennick for giving me the opportunity to write this text; his help has
been appreciated. The Technical Reviewer, Don Reamey, has done a great job of pointing out any issues
found in the programming code. Editor Jill Balzano has been incredibly helpful in all practical aspects of
the project.
Last but not least, thanks to my family for their loving support during my whole career and especially
during the time spent writing this book.

xxiii
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

xxv
■ Introduction

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 1–The 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 2–The 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.

xxvi
■ Introduction

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 13–Asset 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.

xxvii
■ Introduction

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.

xxviii

You might also like