use crate::{
ad::{dual::DualFwd, scalar::Scalar},
core::{
instrument::Instrument,
request::LegsProvider,
trade::{Side, Trade},
},
currencies::currency::Currency,
indices::marketindex::MarketIndex,
instruments::cashflows::leg::Leg,
time::date::Date,
utils::errors::Result,
xva::makecontigentclaim::IntoContingentClaims,
};
#[derive(Clone)]
pub struct Swap<T: Scalar> {
identifier: String,
legs: Vec<Leg<T>>,
forward_index: MarketIndex,
currency: Currency,
}
impl<T> Swap<T>
where
T: Scalar,
{
#[must_use]
pub fn new(
identifier: String,
fixed_leg: Leg<T>,
floating_leg: Leg<T>,
forward_index: MarketIndex,
currency: Currency,
) -> Self {
Self {
identifier,
legs: vec![fixed_leg, floating_leg],
forward_index,
currency,
}
}
#[must_use]
pub fn fixed_leg(&self) -> &Leg<T> {
&self.legs[0]
}
#[must_use]
pub fn floating_leg(&self) -> &Leg<T> {
&self.legs[1]
}
#[must_use]
pub fn forward_index(&self) -> MarketIndex {
self.forward_index.clone()
}
#[must_use]
pub const fn currency(&self) -> Currency {
self.currency
}
}
impl<T> Instrument for Swap<T>
where
T: Scalar,
{
fn identifier(&self) -> String {
self.identifier.clone()
}
}
impl<T> LegsProvider<T> for Swap<T>
where
T: Scalar,
{
fn legs(&self) -> &[Leg<T>] {
&self.legs
}
}
pub struct SwapTrade<T: Scalar> {
instrument: Swap<T>,
trade_date: Date,
notional: f64,
side: Side,
}
impl<T> LegsProvider<T> for SwapTrade<T>
where
T: Scalar,
{
fn legs(&self) -> &[Leg<T>] {
self.instrument.legs()
}
}
impl<T> SwapTrade<T>
where
T: Scalar,
{
#[must_use]
pub const fn new(instrument: Swap<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<Swap<T>> for SwapTrade<T>
where
T: Scalar,
{
fn instrument(&self) -> &Swap<T> {
&self.instrument
}
fn trade_date(&self) -> Date {
self.trade_date
}
fn side(&self) -> Side {
self.side
}
}
#[allow(clippy::expect_used)]
impl From<Swap<f64>> for Swap<DualFwd> {
fn from(value: Swap<f64>) -> Self {
let mut legs = value.legs.into_iter();
Self::new(
value.identifier,
legs.next().expect("fixed leg must exist").into(),
legs.next().expect("floating leg must exist").into(),
value.forward_index,
value.currency,
)
}
}
#[allow(clippy::expect_used)]
impl From<Swap<DualFwd>> for Swap<f64> {
fn from(value: Swap<DualFwd>) -> Self {
let mut legs = value.legs.into_iter();
Self::new(
value.identifier,
legs.next().expect("fixed leg must exist").into(),
legs.next().expect("floating leg must exist").into(),
value.forward_index,
value.currency,
)
}
}
impl From<SwapTrade<f64>> for SwapTrade<DualFwd> {
fn from(value: SwapTrade<f64>) -> Self {
Self::new(
value.instrument.into(),
value.trade_date,
value.notional,
value.side,
)
}
}
impl From<SwapTrade<DualFwd>> for SwapTrade<f64> {
fn from(value: SwapTrade<DualFwd>) -> Self {
Self::new(
value.instrument.into(),
value.trade_date,
value.notional,
value.side,
)
}
}
impl SwapTrade<f64> {
pub fn into_contingent_claims(
&self,
) -> Result<Vec<crate::xva::contigentclaim::ContingentClaim>> {
let trade_id = self.instrument().identifier();
self.instrument()
.legs()
.to_vec()
.into_contingent_claims(&trade_id)
}
}