Touchstone
A C++20 library that prices European calls and puts three ways — the Black–Scholes–Merton closed form, Monte Carlo and Crank–Nicolson finite differences — and holds the closed form and the grid to QuantLib’s reference values, the Monte Carlo to the closed form, and the three to each other, within tolerances its tests state and assert. The grid also prices the American contract. Every claim in its architecture document is numbered against the test or file that holds it. The library depends on nothing but the C++ standard library, builds as a WebAssembly module, and is released as version 1.0.0. It is one half of a pair: the other half is touchstone.garethcooke.com, a tutorial for developers who work alongside quants, in progress, whose demos are to run on this library.
// Overview
Touchstone is a C++20 library that prices European calls and puts on one underlying under Black–Scholes–Merton, with a constant rate, a constant volatility and a continuous dividend yield. It prices them three ways: the closed form with its six analytic Greeks, Monte Carlo, and Crank–Nicolson finite differences. The finite-difference grid also prices the American contract. Around the three sit implied volatility by two solvers, Greeks by bumping any pricer, and sample paths of Brownian and geometric Brownian motion drawn from a frozen random-number generator.
Touchstone is one half of a pair. The other half is touchstone.garethcooke.com, a tutorial for developers who work alongside quants, in progress. Its demos are to run on this library’s WebAssembly build; at 1.0.0 they run on a TypeScript backend. The “Open the tool” button on this page opens the tutorial; until its demos move, they run the tutorial’s TypeScript and not this library.
QuantLib is the oracle and never a dependency. A committed reference file holds 7,200 grid rows and 34 edge rows generated by QuantLib 1.43, and CI regenerates it. The closed form agrees with QuantLib on every row of that file. The grid agrees on every row of the 7,200-row block at 512 space intervals by 256 time steps, which is coarser than its defaults. The Monte Carlo agrees with the closed form, in units of its own standard error, on the rows where enough paths finish in the money to score it. The three are compared side by side on 136 of the rows. Every tolerance is stated in a test and asserted there.
The three methods are independent in method, not in every line. All three form their scalars through one file of scale functions, the Monte Carlo and the grid share the payoff, and the Monte Carlo takes its discount factor and discounted strike from the closed form. The architecture document names what they share because a check of its own claims found that the earlier one, that the three share no arithmetic, said more than the code held.
Every claim in the architecture document is numbered against the test or file that holds it. Before release, an agent that had not written the document checked 129 claims against their citations: 9 said more than their sources held, and 26 needed a narrower wording or a better citation. All were corrected before 1.0.0. The review that followed found the same over-claims still standing in the README and in header comments. One comment promised about a cent of Monte Carlo standard error at the defaults, where the document’s own example measures 0.077. Those were corrected before 1.0.0 as well.
The library has rules of its own, each held by a named test or file. Every entry point validates its inputs before it computes, and refuses an input outside its domain with an exception that names the reason. An input the closed form, the Monte Carlo or the path functions accept never produces a NaN. An infinity is allowed where the true value is unbounded or exceeds a double, because an infinity compares correctly against a tolerance and a NaN makes every ordered comparison false. The same seed gives the same result, bit for bit, and the generator reproduces a committed fixture produced by the tutorial’s TypeScript.
One rule came from a bug. Floating-point multiplication does not associate, so a domain check and a formula that group one product differently guard different inputs. An early check took the drift as one product, (r − q + ½σ²)T, while the formulas formed (r − q)T and ½σ²T separately. At σ = √(2·10³⁰⁰), q = 10³⁰⁰, r = 0 and T = 10¹⁰ the sum is zero and the input passed, while the two terms are −∞ and +∞ and all seven outputs were NaN. Each such product is now written once, in one file, and the checks call the same functions the formulas do. A test keeps the input.
The library also builds as a WebAssembly module: one ES module and one .wasm file, built by Emscripten 6.0.13. The binding translates and does nothing else, and nothing is thrown across the boundary. Its smoke test makes 300,000 refused calls and then reproduces the generator’s fixture and one reference row exactly as before.
The timings are one machine’s on one date, and are not a claim of speed. On an AMD Ryzen 7 3800X with g++ 15.2.0 at -O3, and for one option (a call struck at 100 with a year to expiry, on a spot of 100), the median of one call at the defaults was 74–76 ns for the closed form and its Greeks, 336–349 ns for implied volatility, 1.39–1.41 µs for bumped Greeks, 1.78–1.79 ms for the Monte Carlo and 4.71–4.74 ms for the grid, across three runs. Nothing was tuned for the measurement, and the WebAssembly build was not timed.
Out of scope for version 1: volatility surfaces, local and stochastic volatility, jumps, rates and curve building, exotics, credit, and live market data. Nothing is claimed for MSVC, which CI does not build, or for a browser, which the library’s CI does not run.
// Key decisions
Why price one option three ways?
The three reach the same price by different methods: a formula, a sample and a grid. A mistake in one of them shows up as a disagreement with a number on it, and the tests assert the agreement within stated tolerances. The methods are not wholly separate code: the architecture document names what they share, and a mistake in a shared piece shows only against a method that does not share it. The grid is also the only one of the three that prices the American contract.
Why is QuantLib the oracle but not a dependency?
Touchstone does not link QuantLib. QuantLib-Python generates a committed reference file, and CI regenerates that file and re-derives it independently. The library itself includes nothing outside the C++ standard library. doctest and nlohmann/json are single headers linked by the test executable only.
Why is the exercise style part of the contract’s type?
EuropeanVanilla and AmericanVanilla are one template with two exercise styles, so their fields are written once. The grid accepts either. Every other pricing entry point accepts the European contract only and refuses the American one at compile time, so a function that can price only one of the two cannot price the other as the wrong thing. The test that holds this fails by not compiling.
Why do the grid’s entry points stay overloaded, when one template would do?
A template would have turned a compile error into a silent wrong answer. Another header declares a three-argument require_valid that takes bump sizes. Where both headers are included, require_valid(option, market, {}) is today an ambiguous call that does not compile. With a template in the grid’s place, overload resolution prefers the non-template, and the same line would validate bump sizes instead of a grid. Each pair of overloads forwards to one template in the source file, so no logic is written twice.
Why do bumped Greeks use two different spot bumps?
A second difference balances truncation against cancellation at a larger bump than a first difference does. Delta uses a relative spot bump of 1e-6 and gamma one of 3e-5. A test sweeps the bumps for delta, gamma, vega and rho from 1e-9 to 1e-2 against the analytic Greeks, and asserts that each is most accurate at its default size and more than ten times less accurate at both ends of the sweep.
Why are errors returned across the WebAssembly boundary, not thrown?
An exception unwinding WebAssembly frames leaves the module’s stack pointer behind, and a module was measured killed by it at call 2,074. So each bound function catches every C++ exception and returns a JavaScript error, which a shim throws once the call has returned: RangeError for an input the library refuses, TypeError for an argument of the wrong shape. Arguments are structured-cloned before the module is entered, so a getter that throws does so before any WebAssembly frame exists.