quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Fixed rate coupon cash flow with accrued interest calculations.

use crate::cashflows::traits::CashFlow;
use crate::time::date::Date;
use crate::time::day_counter::DayCounter;
use std::sync::Arc;

/// A coupon paying a fixed annual interest rate over an accrual period.
///
/// # Examples
/// ```
/// use std::sync::Arc;
/// use quantlib_rs::cashflows::coupon::FixedRateCoupon;
/// use quantlib_rs::cashflows::traits::CashFlow;
/// use quantlib_rs::time::date::{Date, Month};
/// use quantlib_rs::time::day_counters::actual_actual::ActualActual;
///
/// let start = Date::from_ymd(2024, Month::January, 15).unwrap();
/// let end = Date::from_ymd(2024, Month::July, 15).unwrap();
/// let dc = Arc::new(ActualActual::default());
///
/// let coupon = FixedRateCoupon::new(100.0, 0.05, end, start, end, dc);
///
/// // Half year coupon at 5% on 100 par ≈ 2.50
/// assert!((coupon.amount() - 2.50).abs() < 0.05);
/// ```
#[derive(Clone)]
pub struct FixedRateCoupon {
    nominal: f64,
    rate: f64,
    payment_date: Date,
    accrual_start_date: Date,
    accrual_end_date: Date,
    day_counter: Arc<dyn DayCounter>,
}

impl FixedRateCoupon {
    /// Create a new fixed rate coupon.
    pub fn new(
        nominal: f64,
        rate: f64,
        payment_date: Date,
        accrual_start_date: Date,
        accrual_end_date: Date,
        day_counter: Arc<dyn DayCounter>,
    ) -> Self {
        Self {
            nominal,
            rate,
            payment_date,
            accrual_start_date,
            accrual_end_date,
            day_counter,
        }
    }

    /// Return the nominal (face value) of the coupon.
    pub fn nominal(&self) -> f64 {
        self.nominal
    }

    /// Return the fixed annual coupon rate.
    pub fn rate(&self) -> f64 {
        self.rate
    }

    /// Return the start date of the accrual period.
    pub fn accrual_start_date(&self) -> Date {
        self.accrual_start_date
    }

    /// Return the end date of the accrual period.
    pub fn accrual_end_date(&self) -> Date {
        self.accrual_end_date
    }

    /// Return the accrual period (year fraction) from start to end date.
    pub fn accrual_period(&self) -> f64 {
        self.day_counter
            .year_fraction(self.accrual_start_date, self.accrual_end_date)
    }

    /// Return the accrued interest amount from accrual start date up to `settlement_date`.
    ///
    /// If `settlement_date <= accrual_start_date`, accrued interest is 0.0.
    /// If `settlement_date >= accrual_end_date`, accrued interest is the full coupon amount.
    pub fn accrued_amount(&self, settlement_date: Date) -> f64 {
        if settlement_date <= self.accrual_start_date {
            return 0.0;
        }
        if settlement_date >= self.accrual_end_date {
            return self.amount();
        }
        let fraction = self
            .day_counter
            .year_fraction(self.accrual_start_date, settlement_date);
        self.nominal * self.rate * fraction
    }
}

impl CashFlow for FixedRateCoupon {
    #[inline]
    fn date(&self) -> Date {
        self.payment_date
    }

    #[inline]
    fn amount(&self) -> f64 {
        self.nominal * self.rate * self.accrual_period()
    }
}