Implementation:Avhz RustQuant Leg
| Knowledge Sources | |
|---|---|
| Domains | Cashflow_Analysis, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for representing a sequence of cashflows (a leg) provided by the RustQuant library.
Description
The Leg struct represents a sequence of Cashflow objects, stored internally as a Vec<Cashflow>. It provides methods for construction (new() from a vector of cashflows), querying (size() for the number of cashflows, cashflows() returning a slice), and financial computation (npv() calculating the Net Present Value by summing the NPV of each constituent cashflow). Cashflows can be added incrementally via add_cashflow(). Date-related methods include start_date() and end_date() which return the earliest and latest dates among the cashflows respectively, and is_active() which checks if a given date falls within the leg's date range (inclusive). The struct derives Debug, Clone, Default, PartialEq, and PartialOrd.
Usage
Use this struct when modeling the payment schedule of a financial instrument such as a bond coupon stream, a swap leg, a loan amortization schedule, or any structured product that generates a series of dated payments.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_cashflows/src/legs.rs
- Lines: 1-157
Signature
#[derive(Debug, Clone, Default, PartialEq, PartialOrd)]
pub struct Leg {
cashflows: Vec<Cashflow>,
}
impl Leg {
pub fn new(cashflows: Vec<Cashflow>) -> Self;
pub fn size(&self) -> usize;
pub fn npv(&self, discount_rate: f64) -> f64;
pub fn add_cashflow(&mut self, cashflow: Cashflow);
pub fn cashflows(&self) -> &[Cashflow];
pub fn start_date(&self) -> Option<Date>;
pub fn end_date(&self) -> Option<Date>;
pub fn is_active(&self, current_date: Date) -> bool;
}
Import
use RustQuant::cashflows::Leg;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cashflows | Vec<Cashflow> | Yes (for new()) | A vector of Cashflow objects composing the leg |
| cashflow | Cashflow | Yes (for add_cashflow()) | A single cashflow to append to the leg |
| discount_rate | f64 | For npv() | Discount factor applied to each cashflow for NPV calculation |
| current_date | Date | For is_active() | Date to check if it falls within the leg's active period |
Outputs
| Name | Type | Description |
|---|---|---|
| Leg | struct | A sequence of cashflows |
| usize | size() | Number of cashflows in the leg |
| f64 | npv() | Sum of NPV values across all cashflows |
| &[Cashflow] | cashflows() | Slice view of all cashflows |
| Option<Date> | start_date() | Earliest cashflow date, or None if empty |
| Option<Date> | end_date() | Latest cashflow date, or None if empty |
| bool | is_active() | True if current_date is within [start_date, end_date] |
Usage Examples
use RustQuant::cashflows::{Cashflow, Leg};
use time::Duration;
let today = time::OffsetDateTime::now_utc().date();
// Build a leg with three monthly cashflows
let cashflows = vec![
Cashflow::new(100.0, today),
Cashflow::new(200.0, today + Duration::days(30)),
Cashflow::new(300.0, today + Duration::days(60)),
];
let mut leg = Leg::new(cashflows);
assert_eq!(leg.size(), 3);
assert_eq!(leg.start_date(), Some(today));
assert_eq!(leg.end_date(), Some(today + Duration::days(60)));
// Check if the leg is active
assert!(leg.is_active(today + Duration::days(15)));
assert!(!leg.is_active(today + Duration::days(61)));
// Compute NPV with a 10% discount
let npv = leg.npv(0.9); // (100 + 200 + 300) * 0.9 = 540.0
// Add another cashflow
leg.add_cashflow(Cashflow::new(400.0, today + Duration::days(90)));
assert_eq!(leg.size(), 4);