Skip to main content

Crate quantsupport

Crate quantsupport 

Source
Expand description

QuantSupport is a Rust library for financial calculations and analysis.

This library provides tools for computing prices, sensitivities and other metrics of financial products.

§Quick-start example

The snippet below prices a 5-year receive-fixed / pay-floating USD IRS, retrieves its NPV, cashflow schedule and curve sensitivities. A complete, runnable version lives in examples/valuation/.

§1 — Imports

Everything needed is re-exported through the prelude module.

use std::{cell::RefCell, rc::Rc};
use quantsupport::prelude::*;

§2 — Build the swap trade

Use MakeSwap to configure the instrument, then wrap it in a SwapTrade that carries trade-level metadata (trade date, notional, side).

let start_date    = Date::new(2024, 1, 15);
let maturity_date = Date::new(2029, 1, 15);
let notional      = 10_000_000.0;
let fixed_rate    = 0.030; // 3.0%

// Day-count / compounding convention for the fixed leg coupon rate.
let rate_definition = RateDefinition::new(
    DayCounter::Actual360,
    Compounding::Simple,
    Frequency::Semiannual,
);

let swap = MakeSwap::<DualFwd>::default()
    .with_identifier("USD_IRS_5Y".to_string())
    .with_start_date(start_date)
    .with_maturity_date(maturity_date)
    .with_fixed_rate(fixed_rate)
    .with_notional(notional)
    .with_rate_definition(rate_definition)
    .with_currency(Currency::USD)
    .with_market_index(MarketIndex::SOFR)
    .with_side(Side::LongReceive)               // receive fixed, pay floating
    .with_fixed_leg_frequency(Frequency::Semiannual)
    .with_floating_leg_frequency(Frequency::Semiannual)
    .build()
    .expect("Failed to build swap");

let trade = SwapTrade::new(swap, start_date, notional, Side::LongReceive);

§3 — Set up the pricing context

A ContextManager holds market data (discount curves, quote / fixing stores) that pricers consult during evaluation. Here we create a flat SOFR discount curve at 3.0%.

let evaluation_date = Date::new(2024, 1, 15);
let discount_rate   = 0.03; // 3.0% flat curve

// Curve rate convention: continuous compounding, ACT/360.
let curve_definition = RateDefinition::new(
    DayCounter::Actual360,
    Compounding::Continuous,
    Frequency::Annual,
);

// Build the flat-forward term structure.
let discount_curve = FlatForwardTermStructure::new(
    evaluation_date,
    DualFwd::from(discount_rate),
    curve_definition,
)
.with_pillar_label("SOFR_flat".to_string());

// Register the curve as the SOFR discount curve.
let mut constructed_elements = ConstructedElementStore::default();
constructed_elements.discount_curves_mut().insert(
    MarketIndex::SOFR,
    DiscountCurveElement::new(
        MarketIndex::SOFR,
        Rc::new(RefCell::new(discount_curve)),
    ),
);

// Empty stores — no live quotes or historical fixings in this example.
let quote_store  = QuoteStore::new(evaluation_date);
let fixing_store = FixingStore::default();

let context = ContextManager::new(quote_store, fixing_store)
    .with_base_currency(Currency::USD)
    .with_constructed_elements(constructed_elements);

§4 — Price the swap and read results

Create a DiscountedCashflowPricer, choose which outputs you need via Request, and call evaluate.

let pricer   = CashflowDiscountPricer::<Swap<DualFwd>, SwapTrade<DualFwd>>::new();
let requests = vec![Request::Value, Request::Cashflows, Request::Sensitivities];
let results  = pricer.evaluate(&trade, &requests, &context).expect("pricing failed");

// --- NPV ---
if let Some(price) = results.price() {
    println!("Swap NPV = {price:.2}");
}

// --- Sensitivities (dV/dQuote per curve pillar) ---
if let Some(sensitivities) = results.sensitivities() {
    println!("\nSensitivities:");
    for (key, exposure) in sensitivities.instrument_keys()
        .iter()
        .zip(sensitivities.exposure().iter())
    {
        println!("  {key}: {exposure:.4}");
    }
}

// --- Cashflow schedule ---
if let Some(cashflows) = results.cashflows() {
    let dates      = cashflows.payment_dates();
    let types      = cashflows.cashflow_types();
    let amounts    = cashflows.amounts();
    let currencies = cashflows.currencies();

    println!("\nCashflows ({} rows):", dates.len());
    for i in 0..dates.len() {
        println!(
            "  {} | {:<20} | {:>14.2} {}",
            dates[i], types[i], amounts[i], currencies[i]
        );
    }
}

Modules§

ad
Automatic differentiation (AD) support.
calibration
Calibration framework: traits and processes for model calibration.
core
Core types and utilities.
currencies
Currency-related types and utilities.
indices
Market index definitions.
instruments
Financial instruments.
math
Mathematical functions and utilities.
models
Pricing models (GBM, Hull-White, etc.).
prelude
Commonly used public exports for pricing and market-data workflows. Prelude module for convenient imports.
pricers
Pricer implementations.
quotes
Market data: quotes and fixings.
rates
Interest rates, term structures, and bootstrapping.
scripting
Payoff scripting language and model-backed evaluation runtime. Payoff scripting language.
simulations
Monte Carlo simulation engine.
time
Time and date utilities.
utils
General utilities.
volatility
Volatility surface and cube definitions.
xva
XVA (Credit / Funding / Capital Valuation Adjustments) framework.