Skip to main content

rustyqlib/core/
traits.rs

1use chrono::NaiveDate;
2use crate::core::errors::RustyQLibError;
3use crate::core::results::PricingResult;
4use crate::rates::utils::DayCountConvention;
5use crate::rates::utils::TermStructure;
6
7pub trait Instrument {
8    /// Present value, or a typed error when the instrument cannot be priced
9    /// (invalid inputs, or an engine/product combination the library
10    /// refuses to price).
11    fn try_npv(&self) -> Result<f64, RustyQLibError>;
12
13    /// Present value, panicking on any pricing error. Convenience for
14    /// instruments already known to be valid; fallible callers (batch
15    /// pricing, services) should use [`Instrument::try_npv`].
16    fn npv(&self) -> f64 {
17        match self.try_npv() {
18            Ok(v) => v,
19            Err(e) => panic!("{e}"),
20        }
21    }
22
23    /// Price the instrument once, returning value, Greeks and (for Monte
24    /// Carlo engines) the standard error together in a [`PricingResult`].
25    ///
26    /// The default implementation reports the present value with zero
27    /// Greeks; instruments that compute sensitivities override it.
28    fn price(&self) -> Result<PricingResult, RustyQLibError> {
29        Ok(PricingResult::from_pv(self.try_npv()?))
30    }
31}
32
33
34pub trait Rates {
35    fn get_implied_rates(&self) -> f64;
36    fn get_maturity_date(&self) -> NaiveDate;
37    fn get_rate(&self) -> f64;
38    fn get_maturity_discount_factor(&self) -> f64;
39    fn get_day_count(&self) -> &DayCountConvention;
40    fn set_term_structure(&mut self,term_structure:TermStructure)->();
41}
42
43pub trait Observer{
44    fn update(&mut self);
45    fn reset(&mut self);
46}
47pub trait Observable{
48    fn update(&mut self);
49    fn reset(&mut self);
50}