Scripting of derivatives transactions has been a central piece of financial software since the 1990s. Most derivatives valuation and risk systems, either in-house or from external vendors, feature scripting technology. Yet, the expertise in this field remains unwritten to date, without any dedicated article or publication.
This volume fills the gap. It is written by Jesper Andreasen and Antoine Savine, who have developed scripting systems for leading investment banks since the 1990s, actively contributed to the development of the scripting technology, and co-developed Danske Bank’s award-winning derivatives system.The publication comes with a complete, professional scripting library written in modern C++, and the chapters gradually and pedagogically guide readers towards its construction.
Scripting technology is widely considered as a convenience for the structuring and risk management of exotic derivatives, only. This publication shows that the scripting technology has much wider applications. Specifically, it demonstrates how scripting provides a unique representation of financial transactions that enables the user to interrogate, aggregate and manipulate cash-flows in multiple ways, facilitating portfolio-wide risk assessment and regulatory calculations like XVA. This is essential reading for developers and analysts, all professionals involved with financial derivatives, as well as students and teachers in Masters and PhD programs in finance.
ANTOINE SAVINE is a mathematician and derivatives practitioner with leading investment banks since 1995. Antoine was the Global Head of Fixed Income Derivatives Research for BNP-Paribas for 10 years, and presently works with Danske Bank, whose xVA system won the In-House System of the Year 2015 Risk Award. Antoine’s Computational Finance books provide a unique practitioner insight into the implementation of derivatives models.
Antoine lectures in the University of Copenhagen’s Masters of Science in Mathematics-Economics, including Volatility Modeling and Numerical Finance, for which his book AAD and Parallel Simulations is the curriculum.
Antoine holds a Masters from the University of Paris (Jussieu) and a PhD from the University of Copenhagen, both in Mathematics. He is best known for his work on volatility, multi-factor interest rate models, scripting, AAD and parallel Monte-Carlo.
JESPER ANDREASEN heads the Quantitative Research Department at Saxo Bank in Copenhagen. Prior to this, Jesper has held senior positions at the quantitative research departments of Danske Bank, Bank of America, Nordea and General Re Financial Products. Jesper's current research interests include real-time risk and capital calculations, IT implementation, statistics, and trading strategies. Jesper earned a PhD in mathematical finance from Aarhus University, Denmark, in 1997. He received Risk Magazine’s Quant of the Year awards in 2001 and 2012, joint with Leif Andersen and Brian Huge respectively, and is an honorary professor of mathematical finance at Copenhagen University.
My Life in Script by Jesper Andreasen xi
PART I
A Scripting Library in C++
Introduction 3
CHAPTER 1
Opening Remarks 7
Introduction 7
1.1 Scripting is not only for exotics 12
1.2 Scripting is for cash-flows not payoffs 13
1.3 Simulation models 15
1.4 Pre-processing 17
1.5 Visitors 19
1.6 Modern implementation in C++ 21
1.7 Script templates 22
CHAPTER 2
Expression Trees 25
2.1 In theory 25
2.2 In code 35
CHAPTER 3
Visitors 41
3.1 The visitor pattern 41
3.2 The debugger visitor 47
3.3 The variable indexer 50
3.4 Pre-processors 54
3.5 Const visitors 55
3.6 The evaluator 57
3.7 Communicating with models 65
CHAPTER 4
Putting Scripting Together with a Model 71
4.1 A simplistic Black-Scholes Monte-Carlo simulator 71
4.1.1 Random number generators 71
4.1.2 Simulation models 73
4.1.3 Simulation engines 76
4.2 Connecting the model to the scripting framework 76
CHAPTER 5
Core Extensions and the “Pays” Keyword 81
5.1 In theory 81
5.2 In code 83
PART II
Basic Improvements
Introduction 93
CHAPTER 6
Past Evaluator 95
CHAPTER 7
Macros 97
CHAPTER 8
Schedules of Cash-Flows 99
CHAPTER 9
Support for Dates 105
CHAPTER 10
Predefined Schedules and Functions 109
CHAPTER 11
Support for Vectors 113
11.1 Basic functionality 113
11.2 Advanced functionality 115
11.2.1 New node types 116
11.2.2 Support in the parser 116
11.2.3 Processing 117
11.2.4 Evaluation 117
PART III
Advanced Improvements
Introduction 121
CHAPTER 12
Linear Products 123
12.1 Interest rates and swaps 123
12.2 Equities, foreign exchange, and commodities 125
12.3 Linear model implementation 126
CHAPTER 13
Fixed Income Instruments 127
13.1 Delayed payments 127
13.2 Discount factors 128
13.3 The simulated data processor 129
13.4 Indexing 129
13.5 Upgrading “pays” to support delayed payments 131
13.6 Annuities 132
13.7 Forward discount factors 132
13.8 Back to equities 132
13.9 Libor and rate fixings 133
13.10 Scripts for swaps and options 134
CHAPTER 14
Multiple Underlying Assets 137
14.1 Multiple assets 137
14.2 Multiple currencies 139
CHAPTER 15
American Monte-Carlo 143
15.1 Least Squares Method 143
15.2 One proxy 147
15.3 Additional regression variables 149
15.4 Feedback and exercise 149
15.5 Multiple exercise and recursion 152
PART IV
Fuzzy Logic and Risk Sensitivities
Introduction 157
CHAPTER 16
Risk Sensitivities with Monte-Carlo 161
16.1 Risk instabilities 161
16.2 Two approaches toward a solution 165
16.3 Smoothing for digitals and barriers 166
16.4 Smoothing for scripted transactions 168
CHAPTER 17
Support for Smoothing 169
CHAPTER 18
An Automated Smoothing Algorithm 175
18.1 Basic algorithm 176
18.2 Nested and combined conditions 179
18.3 Affected variables 179
18.4 Further optimization 180
CHAPTER 19
Fuzzy Logic 183
CHAPTER 20
Condition Domains 189
20.1 Fuzzy evaluation of discrete conditions 189
20.1.1 Condition domains 189
20.1.2 Constant conditions 190
20.1.3 Boolean conditions 191
20.1.4 Binary conditions 193
20.1.5 Discrete conditions 193
20.1.6 Putting it all together 197
20.2 Identification of condition domains 198
20.3 Constant expressions 201
CHAPTER 21
Limitations 203
21.1 Dead and alive 203
21.2 Non-linear use of fuzzy variables 206
CHAPTER 22
The Smoothing Factor 209
22.1 Scripting support 209
22.2 Automatic determination 211
PART V
Application to xVA
CHAPTER 23
xVA 215
CHAPTER 24
Branching 219
CHAPTER 25
Closing Remarks 223
25.1 Script examples 223
25.2 Multi-threading and AAD 228
25.3 Advanced LSM optimizations 229
APPENDIX A
Parsing 231
A.1 Preparing for parsing 231
A.2 Parsing statements 234
A.3 Recursively parsing conditions 238
A.4 Recursively parsing expressions 244
A.5 Performance 252
Bibliography 255
Index 257