1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! Discounting engine for bond valuation.
use crate;
use crateYieldTermStructure;
use crateDate;
use Arc;
/// Discounting pricing engine for bonds.
///
/// Computes Net Present Value (NPV), clean price, and dirty price of bonds
/// given a yield term structure.
///
/// # Examples
/// ```
/// use std::sync::Arc;
/// use quantlib_rs::engines::bond_engine::DiscountingBondEngine;
/// use quantlib_rs::instruments::bonds::fixed_rate_bond::FixedRateBond;
/// use quantlib_rs::termstructures::yield_term_structure::FlatForward;
/// use quantlib_rs::time::calendar::BusinessDayConvention;
/// use quantlib_rs::time::calendars::weekends_only::WeekendsOnly;
/// use quantlib_rs::time::date::{Date, Month, Period};
/// use quantlib_rs::time::day_counters::actual_actual::ActualActual;
/// use quantlib_rs::time::schedule::{DateGenerationRule, Schedule};
///
/// let cal = Arc::new(WeekendsOnly);
/// let dc = Arc::new(ActualActual::default());
/// let issue = Date::from_ymd(2024, Month::January, 15).unwrap();
/// let maturity = Date::from_ymd(2029, Month::January, 15).unwrap();
///
/// let schedule = Schedule::new(
/// issue,
/// maturity,
/// Period::Months(6),
/// cal.as_ref(),
/// BusinessDayConvention::Following,
/// BusinessDayConvention::Following,
/// DateGenerationRule::Backward,
/// false,
/// ).unwrap();
///
/// let bond = FixedRateBond::new(1, 100.0, schedule, 0.04, dc, cal, issue, 100.0);
/// let today = Date::from_ymd(2024, Month::January, 15).unwrap();
/// let curve = Arc::new(FlatForward::new(0.04, today));
/// let engine = DiscountingBondEngine::new(curve);
/// let clean = engine.calculate_clean(&bond, today).unwrap();
/// assert!((clean - 100.0).abs() < 1.0);
/// ```