use serde::{Deserialize, Serialize};
use crate::{
ad::adreal::{ADReal, IsReal},
core::{
instrument::Instrument,
request::LegsProvider,
trade::{Side, Trade},
},
currencies::currency::Currency,
indices::marketindex::MarketIndex,
instruments::{cashflows::leg::Leg, rates::swap::Swap},
time::date::Date,
};
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum SwaptionExerciseType {
European,
}
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub enum SwaptionType {
Payer,
Receiver,
}
#[derive(Clone)]
pub struct Swaption<T: IsReal> {
identifier: String,
underlying: Swap<T>,
expiry: Date,
underlying_type: SwaptionType,
exercise_type: SwaptionExerciseType,
strike: f64,
market_index: MarketIndex,
currency: Currency,
}
impl<T> Swaption<T>
where
T: IsReal,
{
#[must_use]
#[allow(clippy::too_many_arguments)]
pub const fn new(
identifier: String,
underlying: Swap<T>,
expiry: Date,
underlying_type: SwaptionType,
exercise_type: SwaptionExerciseType,
strike: f64,
market_index: MarketIndex,
currency: Currency,
) -> Self {
Self {
identifier,
underlying,
expiry,
underlying_type,
exercise_type,
strike,
market_index,
currency,
}
}
#[must_use]
pub const fn underlying(&self) -> &Swap<T> {
&self.underlying
}
#[must_use]
pub const fn expiry(&self) -> Date {
self.expiry
}
#[must_use]
pub const fn underlying_type(&self) -> SwaptionType {
self.underlying_type
}
#[must_use]
pub const fn exercise_type(&self) -> SwaptionExerciseType {
self.exercise_type
}
#[must_use]
pub const fn strike(&self) -> f64 {
self.strike
}
#[must_use]
pub fn market_index(&self) -> MarketIndex {
self.market_index.clone()
}
#[must_use]
pub const fn currency(&self) -> Currency {
self.currency
}
}
impl<T> Instrument for Swaption<T>
where
T: IsReal,
{
fn identifier(&self) -> String {
self.identifier.clone()
}
}
impl<T> LegsProvider<T> for Swaption<T>
where
T: IsReal,
{
fn legs(&self) -> &[Leg<T>] {
self.underlying.legs()
}
}
pub struct SwaptionTrade<T: IsReal> {
instrument: Swaption<T>,
trade_date: Date,
notional: f64,
side: Side,
}
impl<T> SwaptionTrade<T>
where
T: IsReal,
{
#[must_use]
pub const fn new(instrument: Swaption<T>, trade_date: Date, notional: f64, side: Side) -> Self {
Self {
instrument,
trade_date,
notional,
side,
}
}
#[must_use]
pub const fn notional(&self) -> f64 {
self.notional
}
}
impl<T> Trade<Swaption<T>> for SwaptionTrade<T>
where
T: IsReal,
{
fn instrument(&self) -> &Swaption<T> {
&self.instrument
}
fn trade_date(&self) -> Date {
self.trade_date
}
fn side(&self) -> Side {
self.side
}
}
impl From<Swaption<f64>> for Swaption<ADReal> {
fn from(value: Swaption<f64>) -> Self {
Self::new(
value.identifier,
value.underlying.into(),
value.expiry,
value.underlying_type,
value.exercise_type,
value.strike,
value.market_index,
value.currency,
)
}
}
impl From<Swaption<ADReal>> for Swaption<f64> {
fn from(value: Swaption<ADReal>) -> Self {
Self::new(
value.identifier,
value.underlying.into(),
value.expiry,
value.underlying_type,
value.exercise_type,
value.strike,
value.market_index,
value.currency,
)
}
}
impl From<SwaptionTrade<f64>> for SwaptionTrade<ADReal> {
fn from(value: SwaptionTrade<f64>) -> Self {
Self::new(
value.instrument.into(),
value.trade_date,
value.notional,
value.side,
)
}
}
impl From<SwaptionTrade<ADReal>> for SwaptionTrade<f64> {
fn from(value: SwaptionTrade<ADReal>) -> Self {
Self::new(
value.instrument.into(),
value.trade_date,
value.notional,
value.side,
)
}
}