Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Terminal/YieldCurveChart.swift3// Description: The US Treasury par curve of the market overview (Swift Charts): the latest day against the day four weeks earlier, one point per tenor, maturities in months on a logarithmic axis so the short end is readable, a readout on hover.4import Charts5import SwiftUI6import VQuantKit7import VQuantUI89struct YieldCurveChart: View {10 let curve: VQMarketOverview.YieldCurve11 let locale: VQNumberLocale12 @State private var hover: Double?1314 private struct P: Identifiable { let tenor: String; let months: Double; let value: Double; var id: Double { months } }1516 private func series(_ points: [VQMarketOverview.YieldCurve.Point]) -> [P] {17 // LineMark draws in x order whatever the order of the data: sorting here keeps points and line in step18 points.compactMap { p in p.yield.flatMap { y in p.months > 0 ? P(tenor: p.tenor, months: p.months, value: y) : nil } }.sorted { $0.months < $1.months }19 }2021 var body: some View {22 let today = series(curve.points), previous = series(curve.previous?.points ?? [])23 let all = (today + previous).map(\.value)24 let lo = (all.min() ?? 0), hi = (all.max() ?? 1), pad = max(0.05, (hi - lo) * 0.12)25 let xs = today.map(\.months)26 let labelled: Set<Double> = [1, 3, 6, 12, 24, 60, 120, 360]27 let hovered = hover.flatMap { h in today.min { abs(log($0.months) - log(h)) < abs(log($1.months) - log(h)) } }28 Chart {29 ForEach(previous) { p in30 LineMark(x: .value("Maturity (months)", p.months), y: .value("Yield", p.value), series: .value("Day", "previous"))31 .foregroundStyle(TP.text3).lineStyle(StrokeStyle(lineWidth: 1.25, dash: [4, 3])).interpolationMethod(.monotone)32 PointMark(x: .value("Maturity (months)", p.months), y: .value("Yield", p.value))33 .symbol { Circle().fill(TP.ink0).overlay { Circle().strokeBorder(TP.text3, lineWidth: 1.25) }.frame(width: 6, height: 6) }34 }35 ForEach(today) { p in36 LineMark(x: .value("Maturity (months)", p.months), y: .value("Yield", p.value), series: .value("Day", "latest"))37 .foregroundStyle(TP.text1).lineStyle(StrokeStyle(lineWidth: 2)).interpolationMethod(.monotone)38 PointMark(x: .value("Maturity (months)", p.months), y: .value("Yield", p.value))39 .symbol { Circle().fill(TP.text1).frame(width: 6.5, height: 6.5) }40 }41 if let h = hovered {42 RuleMark(x: .value("Maturity (months)", h.months)).foregroundStyle(TP.lineStrong).lineStyle(StrokeStyle(lineWidth: 1))43 .annotation(position: .top, spacing: 2, overflowResolution: .init(x: .fit(to: .chart), y: .disabled)) { readout(h, previous.first { $0.months == h.months }) }44 }45 }46 .chartXScale(domain: (xs.min() ?? 1) * 0.85 ... (xs.max() ?? 360) * 1.3, type: .log)47 .chartYScale(domain: (lo - pad)...(hi + pad))48 .chartXAxis {49 AxisMarks(values: xs) { v in50 let m = v.as(Double.self) ?? 051 AxisGridLine().foregroundStyle(TP.line)52 if labelled.contains(m) || xs.count <= 8 { AxisValueLabel { Text(today.first { $0.months == m }?.tenor ?? "").font(.system(size: 10.5, design: .monospaced)).foregroundStyle(TP.text2) } }53 }54 }55 .chartYAxis {56 AxisMarks(position: .leading, values: .automatic(desiredCount: 5)) { v in57 AxisGridLine().foregroundStyle(TP.line)58 AxisValueLabel { if let y = v.as(Double.self) { Text(VQFormat.number(y, decimals: 2, locale: locale)).font(.system(size: 10.5, design: .monospaced)).foregroundStyle(TP.text2) } }59 }60 }61 .chartLegend(.hidden)62 .chartOverlay { proxy in63 GeometryReader { g in64 Rectangle().fill(.clear).contentShape(Rectangle())65 .onContinuousHover { phase in66 switch phase {67 case .active(let p):68 guard let frame = proxy.plotFrame.map({ g[$0] }) else { return }69 hover = proxy.value(atX: p.x - frame.origin.x, as: Double.self)70 case .ended: hover = nil71 }72 }73 }74 }75 .accessibilityLabel("US Treasury yield curve, \(curve.date)" + (curve.previous.map { " against \($0.date)" } ?? ""))76 .accessibilityIdentifier("overview.yieldcurve")77 }7879 private func readout(_ p: P, _ before: P?) -> some View {80 HStack(spacing: 8) {81 Text(p.tenor).font(.system(size: 11, weight: .semibold, design: .monospaced)).foregroundStyle(TP.text1)82 Text(VQFormat.percentValue(p.value, locale: locale)).font(.system(size: 11, design: .monospaced)).foregroundStyle(TP.text1)83 if let b = before {84 TDelta(value: p.value - b.value, text: VQFormat.number((p.value - b.value) * 100, decimals: 0, signed: true, locale: locale) + "\u{00A0}bp", size: 11)85 }86 }87 .padding(.horizontal, 8).padding(.vertical, 4)88 .background(RoundedRectangle(cornerRadius: 6, style: .continuous).fill(TP.ink2))89 .overlay { RoundedRectangle(cornerRadius: 6, style: .continuous).strokeBorder(TP.lineStrong, lineWidth: 1) }90 }91}92