Repository Public
1import Foundation2import SheetCore3import SheetCharts4import FinanceModels5import AnalysisKit67/// Group headings of the Financial Modeling Studio.8enum FinGroup {9 static var timeValue: String { L("Time value") }10 static var loans: String { L("Loans") }11 static var fixedIncome: String { L("Fixed income") }12 static var cashFlows: String { L("Cash flows") }13 static var valuation: String { L("Valuation") }14 static var portfolio: String { L("Portfolio") }15 static var planning: String { L("Statements & planning") }16 static var conventions: String { L("Model conventions") }17}1819/// Small helpers shared by the finance tools.20enum Fin {21 static let gregorian: Calendar = {22 var c = Calendar(identifier: .gregorian)23 c.timeZone = .current24 return c25 }()2627 static func calendarDate(_ d: Date) -> CalendarDate {28 let c = gregorian.dateComponents([.year, .month, .day], from: d)29 return CalendarDate(c.year ?? 2000, c.month ?? 1, c.day ?? 1)30 }3132 static func date(_ y: Int, _ m: Int, _ d: Int) -> Date {33 gregorian.date(from: DateComponents(year: y, month: m, day: d, hour: 12)) ?? Date()34 }3536 static func value(_ d: Decimal) -> Value { .number(Money.double(d)) }37 static func double(_ d: Decimal) -> Double { Money.double(d) }3839 /// Compounding chosen in a form: "same" (as the payment frequency), "other" (m per year) or "continuous".40 static func compounding(_ v: FormValues) -> Compounding? {41 switch v.choice("compounding") {42 case "other": return .perYear(Double(max(v.int("cpy"), 1)))43 case "continuous": return .continuous44 default: return nil45 }46 }4748 static var compoundingFields: [FormField] {49 [.choice("compounding", L("Compounding"), [FieldOption("same", L("Same as payments")), FieldOption("other", L("Other frequency")),50 FieldOption("continuous", L("Continuous"))]),51 .integer("cpy", L("Compounding periods per year"), 2, min: 1, max: 365, visible: { $0.choice("compounding") == "other" })]52 }5354 static func compoundingLabel(_ c: Compounding?, paymentsPerYear: Double) -> String {55 switch c {56 case nil: return L("%@ times a year (same as payments)", Fmt.number(paymentsPerYear))57 case .perYear(let m)?: return L("%@ times a year", Fmt.number(m))58 case .continuous?: return L("continuous")59 }60 }6162 /// Percentages typed as "5, 5, 4" or "5%; 4.5%" → fractions. One value applies to every year.63 static func percentList(_ text: String, years: Int, locale: Locale) -> [Double]? {64 let cleaned = text.replacingOccurrences(of: "%", with: " ")65 guard let list = FormValues.numberList(cleaned, locale: locale) else { return nil }66 if list.count == 1 { return Array(repeating: list[0] / 100, count: years) }67 guard list.count == years else { return nil }68 return list.map { $0 / 100 }69 }7071 /// Adds a dashed reference line to a chart.72 static func mark(_ spec: inout ChartSpec, vertical x: Double? = nil, horizontal y: Double? = nil, label: String = "") {73 if let x, x.isFinite { spec.annotations.append(.referenceLine(.vertical, at: x, label: label)) }74 if let y, y.isFinite { spec.annotations.append(.referenceLine(.horizontal, at: y, label: label)) }75 }7677 static func require(_ condition: Bool, _ message: @autoclosure () -> String) throws {78 if !condition { throw ToolError(message()) }79 }80}81