Implementation:Avhz RustQuant Instrument Trait
| Knowledge Sources | |
|---|---|
| Domains | Financial_Instruments, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Defines the Instrument trait, the common interface that all priceable financial instruments in the library must implement.
Description
The Instrument trait establishes a uniform API for querying the price (net present value), pricing error, valuation date, and instrument type of any financial instrument. It serves as the central abstraction layer that allows different instrument types (bonds, currencies, derivatives) to be handled polymorphically.
The trait requires four methods:
- price(): Returns the net present value (NPV) of the instrument as an f64. This is the primary pricing method.
- error(): Returns an optional pricing error estimate. This is useful for stochastic pricing engines (e.g., Monte Carlo) that can provide a standard error. Analytic pricers typically return None.
- valuation_date(): Returns the date at which the NPV is computed. For most instruments this is the trade date; for some exotic products it may be the exercise date.
- instrument_type(): Returns a static string identifying the type of instrument (e.g., "Coupon Bond", "Zero Coupon Bond").
The trait is implemented by CouponBond, CoxIngersollRoss, HullWhite, Vasicek, and Currency in this crate.
Usage
Implement this trait on any new financial instrument to integrate it into the library's pricing framework. Use it as a trait bound when writing generic pricing or reporting code.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_instruments/src/instrument.rs
- Lines: 1-30
Signature
pub trait Instrument {
/// Returns the price (net present value) of the instrument.
fn price(&self) -> f64;
/// Returns the error on the NPV in case the pricing engine can
/// provide it (e.g. Monte Carlo pricing engine).
fn error(&self) -> Option<f64>;
/// Returns the date at which the NPV is calculated.
fn valuation_date(&self) -> time::Date;
/// Instrument type.
fn instrument_type(&self) -> &'static str;
}
Import
use RustQuant::instruments::Instrument;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| (trait methods) | -- | -- | This is a trait definition; inputs are determined by each implementor's struct fields |
Outputs
| Name | Type | Description |
|---|---|---|
| price() | f64 | Net present value of the instrument |
| error() | Option<f64> | Optional pricing error estimate (e.g., Monte Carlo standard error) |
| valuation_date() | time::Date | The date at which the NPV is being calculated |
| instrument_type() | &'static str | A string identifying the instrument type |
Usage Examples
use RustQuant::instruments::Instrument;
// Generic function that works with any instrument
fn print_instrument_summary(inst: &dyn Instrument) {
println!("Type: {}", inst.instrument_type());
println!("Valuation Date: {}", inst.valuation_date());
println!("Price: {:.4}", inst.price());
if let Some(err) = inst.error() {
println!("Error: {:.6}", err);
}
}
// Implementing for a custom instrument
struct MyBond { /* fields */ }
impl Instrument for MyBond {
fn price(&self) -> f64 { /* pricing logic */ 0.95 }
fn error(&self) -> Option<f64> { None }
fn valuation_date(&self) -> time::Date { /* return date */ }
fn instrument_type(&self) -> &'static str { "My Custom Bond" }
}