use crate::curves::Curve;
use crate::error::CurveError;
use crate::error::SurfaceError;
use crate::surfaces::Surface;
use positive::Positive;
pub trait CharmCurve {
fn charm_curve(&self) -> Result<Curve, CurveError>;
}
pub trait CharmSurface {
fn charm_surface(
&self,
price_range: (Positive, Positive),
days_to_expiry: Vec<Positive>,
price_steps: usize,
) -> Result<Surface, SurfaceError>;
}
#[cfg(test)]
mod tests_charm {
use super::*;
use crate::curves::Point2D;
use crate::surfaces::Point3D;
use positive::pos_or_panic;
use rust_decimal::Decimal;
use rust_decimal::MathematicalOps;
use rust_decimal_macros::dec;
use std::collections::BTreeSet;
struct TestCharm {
underlying_price: Positive,
}
impl CharmCurve for TestCharm {
fn charm_curve(&self) -> Result<Curve, CurveError> {
let mut points = BTreeSet::new();
let spot = self.underlying_price.to_dec();
let strikes = [
dec!(380.0),
dec!(400.0),
dec!(420.0),
dec!(440.0),
dec!(450.0),
dec!(460.0),
dec!(480.0),
dec!(500.0),
dec!(520.0),
];
for strike in strikes {
let moneyness = (spot - strike) / spot;
let charm = moneyness * dec!(0.02) * (-moneyness.abs() * dec!(5.0)).exp();
points.insert(Point2D::new(strike, charm));
}
Ok(Curve::new(points))
}
}
impl CharmSurface for TestCharm {
fn charm_surface(
&self,
price_range: (Positive, Positive),
days_to_expiry: Vec<Positive>,
price_steps: usize,
) -> Result<Surface, SurfaceError> {
let mut points = BTreeSet::new();
let price_step = if price_steps > 0 {
(price_range.1 - price_range.0).to_dec() / Decimal::from(price_steps)
} else {
Decimal::ZERO
};
let strike = self.underlying_price.to_dec();
for days in &days_to_expiry {
let time_factor = (dec!(30.0) / days.to_dec()).sqrt().unwrap_or(Decimal::ONE);
for p in 0..=price_steps {
let price = price_range.0.to_dec() + price_step * Decimal::from(p);
let moneyness = (price - strike) / strike;
let charm = moneyness * dec!(0.01) * time_factor;
points.insert(Point3D::new(price, days.to_dec(), charm));
}
}
Ok(Surface::new(points))
}
}
#[test]
fn test_charm_curve_creation() {
let charm = TestCharm {
underlying_price: pos_or_panic!(450.0),
};
let curve = charm.charm_curve();
assert!(curve.is_ok());
let curve = curve.unwrap();
assert_eq!(curve.points.len(), 9);
}
#[test]
fn test_charm_curve_sign_change() {
let charm = TestCharm {
underlying_price: pos_or_panic!(450.0),
};
let curve = charm.charm_curve().unwrap();
let points: Vec<&Point2D> = curve.points.iter().collect();
let itm_charm = points.iter().find(|p| p.x == dec!(400.0)).map(|p| p.y);
let otm_charm = points.iter().find(|p| p.x == dec!(500.0)).map(|p| p.y);
if let (Some(itm), Some(otm)) = (itm_charm, otm_charm) {
assert!(itm > Decimal::ZERO); assert!(otm < Decimal::ZERO); }
}
#[test]
fn test_charm_surface_creation() {
let charm = TestCharm {
underlying_price: pos_or_panic!(450.0),
};
let price_range = (pos_or_panic!(400.0), pos_or_panic!(500.0));
let days = vec![pos_or_panic!(7.0), pos_or_panic!(14.0), pos_or_panic!(30.0)];
let surface = charm.charm_surface(price_range, days, 10);
assert!(surface.is_ok());
let surface = surface.unwrap();
assert_eq!(surface.points.len(), 33);
}
#[test]
fn test_charm_surface_empty_days() {
let charm = TestCharm {
underlying_price: pos_or_panic!(450.0),
};
let price_range = (pos_or_panic!(400.0), pos_or_panic!(500.0));
let days: Vec<Positive> = vec![];
let surface = charm.charm_surface(price_range, days, 10).unwrap();
assert!(surface.points.is_empty());
}
}