quantlib_rs 0.1.0

A ground-up, idiomatic Rust quantitative finance library inspired by QuantLib.
Documentation
//! European vanilla options and payoff specifications.

use crate::time::date::Date;

/// Option type (Call or Put).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum OptionType {
    /// Call option: right to buy underlying at strike
    #[default]
    Call,
    /// Put option: right to sell underlying at strike
    Put,
}

/// Vanilla European option instrument.
///
/// An option that can only be exercised at its maturity date.
///
/// # Examples
/// ```
/// use quantlib_rs::instruments::option::{EuropeanOption, OptionType};
/// use quantlib_rs::time::date::{Date, Month};
///
/// let maturity = Date::from_ymd(2027, Month::September, 3).unwrap();
/// let call = EuropeanOption::new(OptionType::Call, 100.0, maturity);
///
/// assert_eq!(call.strike(), 100.0);
/// assert_eq!(call.payoff(115.0), 15.0);
/// assert_eq!(call.payoff(90.0), 0.0);
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EuropeanOption {
    option_type: OptionType,
    strike: f64,
    maturity_date: Date,
}

impl EuropeanOption {
    /// Create a new European option.
    pub const fn new(option_type: OptionType, strike: f64, maturity_date: Date) -> Self {
        Self {
            option_type,
            strike,
            maturity_date,
        }
    }

    /// Return the option type (Call or Put).
    pub const fn option_type(&self) -> OptionType {
        self.option_type
    }

    /// Return the strike price $K$.
    pub const fn strike(&self) -> f64 {
        self.strike
    }

    /// Return the maturity (exercise) date.
    pub const fn maturity_date(&self) -> Date {
        self.maturity_date
    }

    /// Calculate the intrinsic payoff given the spot price $S$ at maturity.
    pub fn payoff(&self, spot: f64) -> f64 {
        match self.option_type {
            OptionType::Call => (spot - self.strike).max(0.0),
            OptionType::Put => (self.strike - spot).max(0.0),
        }
    }
}