Repository Public
1import Foundation2import Statistics34/// Day-count conventions for accrual and discounting.5///6/// Three different rules are in use under the name "30/360 US". They agree on every pair of dates7/// whose start is not the last day of February; `Tests/FinanceModelsTests/Generators/daycount_conventions.py`8/// states each rule from its published text and lists the differences.9public enum DayCountConvention: String, Sendable, Codable, CaseIterable {10 /// US (NASD) 30/360 exactly as Microsoft Excel computes it (Excel basis 0: YEARFRAC, COUPDAYBS, PRICE…).11 /// The end-date tests look at the unadjusted start day, so a start on the last day of February12 /// followed by an end day of 31 keeps the 31 (28 Feb 2019 → 31 Jan 2020 = 331 days).13 /// Identical to the `=YEARFRAC(…, 0)` worksheet function. Default of the bond models.14 case thirty360US15 /// SIA 30U/360 with the end-of-month February rules applied in sequence (Standard Securities16 /// Calculation Methods): a start on the last day of February becomes the 30th *before* the end-day17 /// test, so a following end day of 31 becomes 30 (28 Feb 2019 → 31 Jan 2020 = 330 days).18 case thirty360SIA19 /// ISDA 30/360, "360/360" or "Bond Basis" (2006 ISDA Definitions §4.16(f)): only days 31 are20 /// adjusted; no February rule (28 Feb 2019 → 31 Jan 2020 = 333 days).21 case thirty360ISDA22 /// 30E/360 (Eurobond basis; Excel basis 4).23 case thirtyE36024 /// Actual/360 (money market; Excel basis 2).25 case actual36026 /// Actual/365 Fixed (Excel basis 3).27 case actual365Fixed28 /// Actual/Actual ISDA: each calendar year's days over that year's length (365 or 366).29 case actualActualISDA30 /// Actual/Actual ICMA (ISMA-99, bond basis; Excel basis 1 for regular coupons): days over (coupon-period days × frequency).31 case actualActualICMA32}3334public enum DayCount {35 /// Day-count numerator: the number of accrual days between two dates under the convention.36 ///37 /// The 30/360 family returns 360·ΔY + 30·ΔM + (D2 − D1) after these adjustments:38 /// - `thirty360US` (Excel): D2 = 30 if both dates are the last day of February; D2 = 30 if D2 = 3139 /// and the *original* D1 ≥ 30; then D1 = 30 if D1 = 31 or the start is the last day of February.40 /// - `thirty360SIA`: if the start is the last day of February, D1 = 30 (and D2 = 30 if the end is41 /// too); then D2 = 30 if D2 = 31 and the *adjusted* D1 ≥ 30; then D1 = 30 if D1 = 31.42 /// - `thirty360ISDA`: D1 = 30 if D1 = 31; then D2 = 30 if D2 = 31 and D1 = 30.43 /// - `thirtyE360`: any 31 becomes 30.44 /// Other conventions: actual days. The dates are used as given (negative when `b` precedes `a`).45 public static func days(from a: CalendarDate, to b: CalendarDate, convention: DayCountConvention) -> Int {46 switch convention {47 case .thirty360US:48 var d1 = a.day, d2 = b.day49 if b.isLastDayOfFebruary && a.isLastDayOfFebruary { d2 = 30 }50 if d2 == 31 && d1 >= 30 { d2 = 30 }51 if d1 == 31 || a.isLastDayOfFebruary { d1 = 30 }52 return 360 * (b.year - a.year) + 30 * (b.month - a.month) + (d2 - d1)53 case .thirty360SIA:54 var d1 = a.day, d2 = b.day55 if a.isLastDayOfFebruary {56 if b.isLastDayOfFebruary { d2 = 30 }57 d1 = 3058 }59 if d2 == 31 && d1 >= 30 { d2 = 30 }60 if d1 == 31 { d1 = 30 }61 return 360 * (b.year - a.year) + 30 * (b.month - a.month) + (d2 - d1)62 case .thirty360ISDA:63 let d1 = min(a.day, 30)64 let d2 = b.day == 31 && d1 == 30 ? 30 : b.day65 return 360 * (b.year - a.year) + 30 * (b.month - a.month) + (d2 - d1)66 case .thirtyE360:67 let d1 = min(a.day, 30), d2 = min(b.day, 30)68 return 360 * (b.year - a.year) + 30 * (b.month - a.month) + (d2 - d1)69 default:70 return a.days(to: b)71 }72 }7374 /// Year fraction between two dates.75 ///76 /// `actualActualICMA` needs the coupon period containing the accrual (`referenceStart`…`referenceEnd`)77 /// and the coupon `frequency`: fraction = days / (frequency × days in the reference period). When the78 /// accrual spans several regular periods pass each period separately and add the results. Without a79 /// reference period the function throws.80 public static func yearFraction(from a: CalendarDate, to b: CalendarDate, convention: DayCountConvention,81 referenceStart: CalendarDate? = nil, referenceEnd: CalendarDate? = nil,82 frequency: Int? = nil) throws -> Double {83 if b < a {84 return try -yearFraction(from: b, to: a, convention: convention, referenceStart: referenceStart, referenceEnd: referenceEnd, frequency: frequency)85 }86 switch convention {87 case .thirty360US, .thirty360SIA, .thirty360ISDA, .thirtyE360: return Double(days(from: a, to: b, convention: convention)) / 36088 case .actual360: return Double(a.days(to: b)) / 36089 case .actual365Fixed: return Double(a.days(to: b)) / 36590 case .actualActualISDA:91 if a.year == b.year { return Double(a.days(to: b)) / (DateSerial_isLeap(a.year) ? 366 : 365) }92 let endOfFirst = CalendarDate(a.year + 1, 1, 1), startOfLast = CalendarDate(b.year, 1, 1)93 return Double(a.days(to: endOfFirst)) / (DateSerial_isLeap(a.year) ? 366 : 365)94 + Double(b.year - a.year - 1)95 + Double(startOfLast.days(to: b)) / (DateSerial_isLeap(b.year) ? 366 : 365)96 case .actualActualICMA:97 guard let rs = referenceStart, let re = referenceEnd, let f = frequency, f > 0, rs < re else {98 throw StatError.invalidInput("ACT/ACT ICMA needs the reference coupon period and the coupon frequency")99 }100 return Double(a.days(to: b)) / (Double(f) * Double(rs.days(to: re)))101 }102 }103104 private static func DateSerial_isLeap(_ y: Int) -> Bool { (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 }105}106