r/csharp • • 6d ago

Tutorial nanoql: stateless quant finance in C#: immutable values, static pricers, .NET 10

nanoql is an open-source pricing library for .NET 10: curves, swaps, swaptions, SABR, equity options, Heston, CDS. Instruments, curves and models are immutable values, and pricers are static functions of them; the evaluation date is an argument, not a global setting.

var today = new Date(15, Month.October, 2026);
var context = new EvaluationContext { EvaluationDate = today };
var curve = new FlatForward(today, 0.036, new Actual365Fixed());

var swap = new MakeOIS(new Period(5, TimeUnit.Years), new Sofr(curve))
    .WithFixedRate(0.036).WithNominal(10e6)
    .ToOvernightIndexedSwap(context);

var result = DiscountingSwapEngine.Calculate(swap, context, curve);
Console.WriteLine($"NPV {result.NPV:N2}, fair rate {result.FairRate:P4}");
// NPV 7,041.24, fair rate 3.6154%

Some .NET details that may interest you:

  • It uses static abstract interface members, so it needs .NET 10 and has no .NET Framework build.
  • Python, C and C++ reach it through Native AOT: one C interface over the compiled library, so pip install nanoql needs no .NET runtime.
  • The Excel add-in is generated from the same surface with Excel-DNA.
  • It's derived from QuantLib 1.44 and checked against it: 3.35 million reference outputs, 99.86% within a relative 1e-10, and QuantLib's own tests ported at their tolerances.

dotnet add package NanoQL · getting started · GitHub · Apache-2.0

20 Upvotes

6 comments sorted by

3

u/Inevitable_Buy_4957 6d ago

That's the kind of minimalist beauty I can get behind.

2

u/gangning 6d ago

Thanks! That was the goal: no global settings and no observers, just values in and a price out.

1

u/MrLyttleG 5d ago

If your lib has public exports in your AOT, then I can use it in my scripting engine https://lyttleg.github.io/ScriptLangCore/

1

u/gangning 5d ago

It does. The C API zip on the releases page has nanoql.dll, built with Native AOT so there's no .NET runtime to install, plus nanoql.h: a plain C export for every public member, about 11,500 of them. Each function returns an int32 status (0 is OK) and writes its result through a last out-pointer. Objects are opaque handles that you free with nql_release, and nql_last_error_message() tells you why a call was refused. nanoql's own Python package is just ctypes over that same DLL.

1

u/smartcave 4d ago

This sounds worth checking it out. What do/have you use(d) it for so far?

1

u/gangning 4d ago

Pricing and risk tooling, where I needed QuantLib's models without the global evaluation date and the observer graph.