Implementation:Avhz RustQuant Cashflow
| Knowledge Sources | |
|---|---|
| Domains | Cashflow_Analysis, Quantitative_Finance |
| Last Updated | 2026-02-07 19:00 GMT |
Overview
Concrete tool for representing and manipulating individual cashflows provided by the RustQuant library.
Description
The Cashflow struct models a single financial cashflow consisting of an amount (f64) and a date (time::Date). It provides a constructor new(), accessor methods amount() and date(), and a npv() method that computes the Net Present Value by multiplying the amount by a discount rate. The struct implements a comprehensive suite of arithmetic operator traits: Add and AddAssign for combining cashflows on the same date (panics if dates differ), Sub and SubAssign for subtraction with date matching, Mul<f64> and MulAssign<f64> for scalar multiplication, Div<f64> and DivAssign<f64> for scalar division, and Neg (for owned, borrowed, and mutable references) for sign inversion. It also implements Display for formatted output as "Cashflow(amount, date)". The struct derives Debug, Clone, Copy, PartialEq, and PartialOrd.
Usage
Use this struct as the fundamental building block for cashflow-based financial calculations, including bond pricing, loan amortization, NPV analysis, and as components of Leg sequences in structured products.
Code Reference
Source Location
- Repository: RustQuant
- File: crates/RustQuant_cashflows/src/cashflow.rs
- Lines: 1-265
Signature
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct Cashflow {
pub amount: f64,
pub date: Date,
}
impl Cashflow {
pub fn new(amount: f64, date: Date) -> Self;
pub fn amount(&self) -> f64;
pub fn date(&self) -> Date;
pub fn npv(&self, discount_rate: f64) -> f64;
}
// Arithmetic traits: Add, AddAssign, Sub, SubAssign,
// Mul<f64>, MulAssign<f64>, Div<f64>, DivAssign<f64>, Neg
Import
use RustQuant::cashflows::Cashflow;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| amount | f64 | Yes | The monetary amount of the cashflow (positive or negative) |
| date | Date | Yes | The date on which the cashflow occurs |
| discount_rate | f64 | For npv() | Discount factor to apply when computing Net Present Value |
Outputs
| Name | Type | Description |
|---|---|---|
| Cashflow | struct | A dated monetary amount |
| f64 | amount() | The cashflow amount |
| Date | date() | The cashflow date |
| f64 | npv() | Net Present Value (amount * discount_rate) |
Usage Examples
use RustQuant::cashflows::Cashflow;
use time::Date;
let today = time::OffsetDateTime::now_utc().date();
// Create cashflows
let cf1 = Cashflow::new(100.0, today);
let cf2 = Cashflow::new(50.0, today);
// Arithmetic operations (dates must match)
let combined = cf1 + cf2;
assert_eq!(combined.amount(), 150.0);
let difference = cf1 - cf2;
assert_eq!(difference.amount(), 50.0);
// Scalar operations
let doubled = cf1 * 2.0;
assert_eq!(doubled.amount(), 200.0);
// Net Present Value with 10% discount
let npv = cf1.npv(0.9);
assert!((npv - 90.0).abs() < 1e-10);
// Negate a cashflow
let neg_cf = -cf1;
assert_eq!(neg_cf.amount(), -100.0);