rsRustPrimer
< Back to module

Structs, enums and pattern matching

Running project · riskforge

Project: the domain model

So far riskforge handled precomputed MtMs. In reality you start from a portfolio of contracts and compute their MtM for each market scenario. So we model the contracts: a Trade holds an Instrument (forward or option), a direction (long or short) and a quantity.

src/model.rs
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Direction {
Long,
Short,
}
impl Direction {
pub fn sign(self) -> f64 {
match self {
Direction::Long => 1.0,
Direction::Short => -1.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OptionKind {
Call,
Put,
}
#[derive(Debug, Clone, PartialEq)]
pub enum Instrument {
Forward { strike: f64 },
EuropeanOption { kind: OptionKind, strike: f64 },
}
#[derive(Debug, Clone, PartialEq)]
pub struct Trade {
pub id: u32,
pub instrument: Instrument,
pub direction: Direction,
pub quantity: f64,
}
impl Trade {
/// MtM du trade pour un prix spot donné (valeur intrinsèque pour les options).
pub fn mtm(&self, spot: f64) -> f64 {
let unit_value = match self.instrument {
Instrument::Forward { strike } => spot - strike,
Instrument::EuropeanOption { kind: OptionKind::Call, strike } => (spot - strike).max(0.0),
Instrument::EuropeanOption { kind: OptionKind::Put, strike } => (strike - spot).max(0.0),
};
unit_value * self.quantity * self.direction.sign()
}
}
fn main() {
let trade = Trade {
id: 1,
instrument: Instrument::EuropeanOption { kind: OptionKind::Call, strike: 100.0 },
direction: Direction::Short,
quantity: 1_000.0,
};
println!("MtM à 112 : {}", trade.mtm(112.0)); // -12000
}
A deliberate simplification: an option is worth its intrinsic value here. The real price (Black-Scholes) will come with the diffusion models.

Next comes the key notion of counterparty risk: netting. If the counterparty defaults, contracts covered by the same netting agreement (ISDA) are settled together. What counts is no longer each trade's exposure but that of their sum: max(sum of MtMs, 0).

src/model.rs
#[derive(Debug, Clone, PartialEq)]
pub enum Instrument {
Forward { strike: f64 },
}
#[derive(Debug, Clone, PartialEq)]
pub struct Trade {
pub id: u32,
pub instrument: Instrument,
pub quantity: f64,
}
impl Trade {
pub fn mtm(&self, spot: f64) -> f64 {
match self.instrument {
Instrument::Forward { strike } => (spot - strike) * self.quantity,
}
}
}
#[derive(Debug)]
pub struct NettingSet {
pub counterparty: String,
trades: Vec<Trade>,
}
impl NettingSet {
pub fn new(counterparty: &str) -> Self {
Self { counterparty: counterparty.to_string(), trades: Vec::new() }
}
pub fn add(&mut self, trade: Trade) {
self.trades.push(trade);
}
pub fn net_mtm(&self, spot: f64) -> f64 {
let mut total = 0.0;
for trade in &self.trades {
total += trade.mtm(spot);
}
total
}
pub fn net_exposure(&self, spot: f64) -> f64 {
self.net_mtm(spot).max(0.0)
}
}
fn main() {
let mut set = NettingSet::new("ACME Bank");
set.add(Trade { id: 1, instrument: Instrument::Forward { strike: 100.0 }, quantity: 1_000.0 });
set.add(Trade { id: 2, instrument: Instrument::Forward { strike: 105.0 }, quantity: -800.0 });
println!("{} : exposition nette {}", set.counterparty, set.net_exposure(110.0)); // 6000
}
trades is not pub: a trade can only be added through add. That is encapsulation the Rust way, at module level.

Your turn

Add gross_exposure, the sum of each trade's exposure taken on its own (without netting), and compare it with the net exposure.

src/main.rs
struct Trade {
strike: f64,
quantity: f64,
}
impl Trade {
fn mtm(&self, spot: f64) -> f64 {
(spot - self.strike) * self.quantity
}
}
struct NettingSet {
trades: Vec<Trade>,
}
impl NettingSet {
fn net_exposure(&self, spot: f64) -> f64 {
let mut total = 0.0;
for trade in &self.trades {
total += trade.mtm(spot);
}
total.max(0.0)
}
// ajoute gross_exposure ici
}
fn main() {
let set = NettingSet {
trades: vec![
Trade { strike: 100.0, quantity: 1_000.0 },
Trade { strike: 105.0, quantity: -800.0 },
],
};
println!("net = {}", set.net_exposure(110.0)); // 6000
println!("brut = {}", set.gross_exposure(110.0)); // 10000
}