All projects
SoftwareComplete

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.

C++20Options PricingBlack–ScholesMonte CarloFinite DifferencesCrank–NicolsonGreeksImplied VolatilityWebAssemblyEmscriptenQuantLibDifferential TestingCMakedoctest

// 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.