quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! Vanilla fixed-float interest rate swap instrument.

use crate::time::day_counter::DayCounter;
use crate::time::schedule::Schedule;
use std::sync::Arc;

/// Swap side (Payer pays fixed / receives floating, Receiver receives fixed / pays floating).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum SwapType {
    /// Payer swap: pay fixed coupon, receive floating rate.
    #[default]
    Payer,
    /// Receiver swap: receive fixed coupon, pay floating rate.
    Receiver,
}

/// Vanilla fixed-float interest rate swap (IRS).
///
/// # Examples
/// ```
/// use std::sync::Arc;
/// use quantlib_rs::instruments::swaps::vanilla_swap::{VanillaSwap, SwapType};
/// use quantlib_rs::time::calendar::BusinessDayConvention;
/// use quantlib_rs::time::calendars::weekends_only::WeekendsOnly;
/// use quantlib_rs::time::date::{Date, Month, Period};
/// use quantlib_rs::time::day_counters::actual_360::Actual360;
/// use quantlib_rs::time::day_counters::thirty_360::{Thirty360, Thirty360Convention};
/// use quantlib_rs::time::schedule::{DateGenerationRule, Schedule};
///
/// let cal = WeekendsOnly;
/// let start = Date::from_ymd(2024, Month::January, 15).unwrap();
/// let end = Date::from_ymd(2029, Month::January, 15).unwrap();
///
/// let fixed_schedule = Schedule::new(
///     start,
///     end,
///     Period::Months(6),
///     &cal,
///     BusinessDayConvention::Following,
///     BusinessDayConvention::Following,
///     DateGenerationRule::Backward,
///     false,
/// ).unwrap();
///
/// let floating_schedule = Schedule::new(
///     start,
///     end,
///     Period::Months(3),
///     &cal,
///     BusinessDayConvention::Following,
///     BusinessDayConvention::Following,
///     DateGenerationRule::Backward,
///     false,
/// ).unwrap();
///
/// let fixed_dc = Arc::new(Thirty360::new(Thirty360Convention::BondBasis));
/// let float_dc = Arc::new(Actual360);
///
/// let swap = VanillaSwap::new(
///     SwapType::Payer,
///     10_000_000.0,
///     fixed_schedule,
///     0.045,
///     fixed_dc,
///     floating_schedule,
///     float_dc,
///     0.0,
/// );
///
/// assert_eq!(swap.nominal(), 10_000_000.0);
/// assert_eq!(swap.fixed_rate(), 0.045);
/// ```
#[derive(Clone)]
pub struct VanillaSwap {
    swap_type: SwapType,
    nominal: f64,
    fixed_schedule: Schedule,
    fixed_rate: f64,
    fixed_day_counter: Arc<dyn DayCounter>,
    floating_schedule: Schedule,
    floating_day_counter: Arc<dyn DayCounter>,
    spread: f64,
}

impl VanillaSwap {
    /// Create a new vanilla fixed-float interest rate swap.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        swap_type: SwapType,
        nominal: f64,
        fixed_schedule: Schedule,
        fixed_rate: f64,
        fixed_day_counter: Arc<dyn DayCounter>,
        floating_schedule: Schedule,
        floating_day_counter: Arc<dyn DayCounter>,
        spread: f64,
    ) -> Self {
        Self {
            swap_type,
            nominal,
            fixed_schedule,
            fixed_rate,
            fixed_day_counter,
            floating_schedule,
            floating_day_counter,
            spread,
        }
    }

    /// Return the swap type (Payer or Receiver).
    pub fn swap_type(&self) -> SwapType {
        self.swap_type
    }

    /// Return the notional / nominal amount of the swap.
    pub fn nominal(&self) -> f64 {
        self.nominal
    }

    /// Return the fixed coupon schedule.
    pub fn fixed_schedule(&self) -> &Schedule {
        &self.fixed_schedule
    }

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

    /// Return the fixed leg day counter.
    pub fn fixed_day_counter(&self) -> &dyn DayCounter {
        self.fixed_day_counter.as_ref()
    }

    /// Return the floating leg schedule.
    pub fn floating_schedule(&self) -> &Schedule {
        &self.floating_schedule
    }

    /// Return the floating leg day counter.
    pub fn floating_day_counter(&self) -> &dyn DayCounter {
        self.floating_day_counter.as_ref()
    }

    /// Return the spread over the floating index.
    pub fn spread(&self) -> f64 {
        self.spread
    }
}