RustQuant_time/schedule.rs
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// RustQuant: A Rust library for quantitative finance tools.
// Copyright (C) 2022-2024 https://github.com/avhz
// Dual licensed under Apache 2.0 and MIT.
// See:
// - LICENSE-APACHE.md
// - LICENSE-MIT.md
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
use crate::date_rolling::{DateRoller, DateRollingConvention};
use crate::day_counting::{DayCountConvention, DayCounter};
use crate::Calendar;
use std::fmt;
use time::Date;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// STRUCTS, ENUMS, AND TRAITS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/// Schedule struct.
///
/// Many financial instruments have a schedule of dates associated with them.
/// For example, interest rate caps/floors have a schedule of payment dates,
/// which are the expiration dates of the caplets/floorlets, or a coupon bond
/// has a schedule of coupon payment dates.
///
/// The Schedule struct is used to represent these schedules,
/// and pricing methods should be implemented using date/time functionality.
#[derive(Clone, Debug)]
pub struct Schedule {
/// The dates of the schedule.
pub dates: Vec<Date>,
/// The day count factors of the schedule.
pub day_count_factors: Vec<f64>,
/// The convention of the schedule.
pub day_counting_convention: DayCountConvention,
/// The business day convention of the schedule.
pub date_rolling_convention: DateRollingConvention,
}
/// The `Scheduler` trait.
/// This trait is used to generate schedules for a `Calendar`.
pub trait Scheduler {
/// Generate a schedule from a slice of `Date`s.
/// For example, a list of coupon payment dates.
///
/// Note: The effective date is not included in the dates input, and assumed to be today.
///
/// # Arguments
///
/// * `dates` - A slice of `Date`s (such as coupon payment dates).
/// * `date_rolling_convention` - The date rolling convention.
/// * `day_counting_convention` - The day counting convention.
fn generate_schedule_from_dates(
&self,
dates: &[Date],
date_rolling_convention: DateRollingConvention,
day_counting_convention: DayCountConvention,
) -> Schedule;
}
impl<C> Scheduler for C
where
C: Calendar,
{
fn generate_schedule_from_dates(
&self,
dates: &[Date],
date_rolling_convention: DateRollingConvention,
day_counting_convention: DayCountConvention,
) -> Schedule {
let today = crate::today();
// First we need to roll the dates according to a given convention.
let rolled_dates = self.roll_dates(dates, &date_rolling_convention);
// Then we need to compute the day count factors.
let mut day_count_factors = self.day_count_factors(&rolled_dates, &day_counting_convention);
day_count_factors.insert(
0,
self.day_count_factor(today, rolled_dates[0], &day_counting_convention),
);
Schedule {
dates: rolled_dates,
day_count_factors,
day_counting_convention,
date_rolling_convention,
}
}
}
impl fmt::Display for Schedule {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Dates: {:?}\n\
Day Count Factors: {:?}\n\
Day Counting Convention: {}\n\
Date Rolling Convention: {}",
self.dates,
self.day_count_factors,
self.day_counting_convention,
self.date_rolling_convention
)
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// IMPLEMENTATIONS
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// impl Schedule {
// /// Create a new schedule from a vector of dates.
// ///
// /// The list can be in any order, and the schedule will be
// /// sorted in chronological order.
// pub fn new(mut dates: &[Date]) -> Schedule {
// dates.sort();
// Schedule {
// dates: dates.to_vec(),
// start: Some(dates[0]),
// end: Some(dates[dates.len() - 1]),
// frequency: None,
// day_count_convention: DayCountConvention::default(),
// date_rolling_convention: DateRollingConvention::default(),
// }
// }
// /// Creates a new schedule from a given start date, period length, and number
// /// of periods in the schedule.
// pub fn new_from_start(start: Date, period: Duration, num_periods: i64) -> Schedule {
// let mut payments = Vec::with_capacity(num_periods as usize + 1);
// let mut current_time = start;
// for _ in 0..=num_periods {
// payments.push(current_time);
// current_time += period;
// }
// Schedule {
// dates: payments,
// start: Some(start),
// end: Some(current_time),
// frequency: None,
// day_count_convention: DayCountConvention::Actual365,
// date_rolling_convention: DateRollingConvention::Following,
// }
// }
// /// Creates a new schedule from a given end date, period length, and number
// /// of periods in the schedule.
// pub fn new_from_end(end: Date, period: Duration, num_periods: i64) -> Schedule {
// let mut payments = Vec::with_capacity(num_periods as usize + 1);
// let mut current_time = end;
// for _ in 0..=num_periods {
// payments.push(current_time);
// current_time -= period;
// }
// payments.reverse();
// Schedule {
// dates: payments,
// start: Some(current_time),
// end: Some(end),
// frequency: None,
// day_count_convention: DayCountConvention::Actual365,
// date_rolling_convention: DateRollingConvention::Following,
// }
// }
// /// Creates a new schedule from a vector of dates.
// /// Dates must be in ascending order.
// pub fn new_from_dates(dates: &&[Date]) -> Schedule {
// assert!(&dates.windows(2).all(|window| window[0] < window[1]));
// Schedule {
// dates: dates.clone(),
// start: Some(dates[0]),
// end: Some(dates[dates.len() - 1]),
// frequency: None,
// day_count_convention: DayCountConvention::Actual365,
// date_rolling_convention: DateRollingConvention::Following,
// }
// }
// /// Drops a given date from the schedule.
// pub fn drop(&mut self, date: Date) {
// // let date = date.midnight_at(UtcOffset::UTC); // Convert to Date for comparison
// self.dates.retain(|&payment| payment.date() != date.date());
// }
// }
// // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// // UNIT TESTS
// // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// #[cfg(test)]
// mod test_schedule {
// use super::*;
// use time::macros::datetime;
// #[test]
// fn test_new_from_start() {
// let start = datetime!(2023-06-01 0:0:0 UTC);
// let period = Duration::days(30);
// let num_periods = 3;
// let schedule = Schedule::new_from_start(start, period, num_periods);
// assert_eq!(schedule.dates.len(), num_periods as usize + 1);
// assert_eq!(
// schedule.dates,
// vec![
// datetime!(2023-06-01 0:0:0 UTC),
// datetime!(2023-07-01 0:0:0 UTC),
// datetime!(2023-07-31 0:0:0 UTC),
// datetime!(2023-08-30 0:0:0 UTC),
// ],
// );
// for i in 0..num_periods {
// assert_eq!(schedule.dates[i as usize], start + period * i as i32);
// }
// }
// #[test]
// fn test_new_from_end() {
// let end = datetime!(2023-08-01 0:0:0 UTC);
// let period = Duration::days(30);
// let num_periods = 3;
// let schedule = Schedule::new_from_end(end, period, num_periods);
// // Length is num_periods + 1 because the end date is included.
// // Fencepost problem.
// assert_eq!(schedule.dates.len(), num_periods as usize + 1);
// assert_eq!(
// schedule.dates,
// vec![
// datetime!(2023-05-03 0:0:0 UTC),
// datetime!(2023-06-02 0:0:0 UTC),
// datetime!(2023-07-02 0:0:0 UTC),
// datetime!(2023-08-01 0:0:0 UTC),
// ],
// );
// // for i in 0..num_periods {
// // assert_eq!(
// // schedule.dates[(num_periods - 1 - i) as usize],
// // end - period * i as i32
// // );
// // }
// }
// #[test]
// fn test_new_from_dates() {
// let dates = vec![
// datetime!(2023-06-01 0:0:0 UTC),
// datetime!(2023-07-01 0:0:0 UTC),
// datetime!(2023-08-01 0:0:0 UTC),
// ];
// let schedule = Schedule::new_from_dates(&dates);
// assert_eq!(schedule.dates, dates);
// }
// #[test]
// #[should_panic(expected = "assertion failed")]
// fn test_new_from_dates_out_of_order() {
// let dates = vec![
// datetime!(2023-07-01 0:0:0 UTC),
// datetime!(2023-06-01 0:0:0 UTC),
// ];
// let _ = Schedule::new_from_dates(&dates);
// }
// #[test]
// fn test_drop() {
// let mut schedule =
// Schedule::new_from_start(datetime!(2023-06-01 0:0:0 UTC), Duration::days(30), 3);
// schedule.drop(datetime!(2023-07-01 0:0:0 UTC));
// assert_eq!(schedule.dates.len(), 3);
// assert_eq!(
// schedule.dates,
// vec![
// datetime!(2023-06-01 0:0:0 UTC),
// // datetime!(2023-07-01 0:0:0 UTC), // Dropped
// datetime!(2023-07-31 0:0:0 UTC),
// datetime!(2023-08-30 0:0:0 UTC),
// ]
// );
// }
// }