Repository Public
1//2// VQuant iOS3// File: VQuant/Charts/ChartTypes.swift4// Description: What a price chart can show (mark type, overlays, sub-panels) and the indicator series computed once5// per data set over all the bars.6//7// Author: Simon-Pierre Boucher8// Contact: contact@spboucher.ai9//1011import SwiftUI12import VQuantUI1314enum ChartKind: String, CaseIterable, Codable, Sendable, Identifiable {15 case candles, ohlc, line, area16 var id: String { rawValue }17 var label: String {18 switch self { case .candles: "Chandeliers"; case .ohlc: "OHLC"; case .line: "Ligne"; case .area: "Aire" }19 }20 var systemImage: String {21 switch self { case .candles: "chart.bar.xaxis"; case .ohlc: "chart.xyaxis.line"; case .line: "chart.line.uptrend.xyaxis"; case .area: "chart.line.flattrend.xyaxis" }22 }23 /// Narrowest slot, in points, a mark of this kind stays readable in.24 var minimumSlot: Double { self == .candles || self == .ohlc ? 3 : 1 }25}2627enum ChartOverlay: String, CaseIterable, Codable, Sendable, Identifiable {28 case sma20, sma50, sma200, ema21, bollinger29 var id: String { rawValue }30 var label: String {31 switch self { case .sma20: "MM 20"; case .sma50: "MM 50"; case .sma200: "MM 200"; case .ema21: "MME 21"; case .bollinger: "Bollinger 20·2" }32 }33 var color: Color {34 switch self {35 case .sma20: VQTokens.DataPalette.categorical[0]36 case .sma50: VQTokens.DataPalette.categorical[6]37 case .sma200: VQTokens.DataPalette.categorical[2]38 case .ema21: VQTokens.DataPalette.categorical[3]39 case .bollinger: VQTokens.Palette.text340 }41 }42}4344enum ChartPanel: String, CaseIterable, Codable, Sendable, Identifiable {45 case volume, rsi, macd46 var id: String { rawValue }47 var label: String {48 switch self { case .volume: "Volume"; case .rsi: "RSI 14"; case .macd: "MACD 12·26·9" }49 }50}5152struct ChartConfiguration: Equatable, Codable, Sendable {53 var kind: ChartKind = .candles54 var overlays: Set<ChartOverlay> = []55 var panels: Set<ChartPanel> = [.volume]56}5758/// Indicator series aligned on the bars. Computed over the whole series, then windowed by the renderer, so the59/// left edge of a zoomed window shows the same values as the full chart.60struct ChartData {61 var count = 062 var overlays: [ChartOverlay: [[Double?]]] = [:]63 var rsi: [Double?] = []64 var macd: Indicators.MACD?6566 init() {}6768 init(bars: [Bar], configuration: ChartConfiguration) {69 count = bars.count70 let closes = bars.map(\.close)71 for o in configuration.overlays {72 switch o {73 case .sma20: overlays[o] = [Indicators.sma(closes, period: 20)]74 case .sma50: overlays[o] = [Indicators.sma(closes, period: 50)]75 case .sma200: overlays[o] = [Indicators.sma(closes, period: 200)]76 case .ema21: overlays[o] = [Indicators.ema(closes, period: 21)]77 case .bollinger:78 let b = Indicators.bollinger(closes, period: 20, width: 2)79 overlays[o] = [b.middle, b.upper, b.lower]80 }81 }82 if configuration.panels.contains(.rsi) { rsi = Indicators.rsi(closes, period: 14) }83 if configuration.panels.contains(.macd) { macd = Indicators.macd(closes) }84 }85}8687/// Keeps the indicator series between renders: a pan or a zoom redraws the window without recomputing them.88@MainActor89final class ChartDataCache {90 private struct Key: Equatable {91 var count: Int92 var first: Date?93 var last: Date?94 var lastClose: Double?95 var overlays: Set<ChartOverlay>96 var panels: Set<ChartPanel>97 }98 private var key: Key?99 private var data = ChartData()100101 func data(for bars: [Bar], configuration: ChartConfiguration) -> ChartData {102 let k = Key(count: bars.count, first: bars.first?.date, last: bars.last?.date, lastClose: bars.last?.close, overlays: configuration.overlays, panels: configuration.panels)103 if k != key { data = ChartData(bars: bars, configuration: configuration); key = k }104 return data105 }106}107108/// Date labels of the charts, in exchange time.109enum ChartDates {110 private static var base: Date.FormatStyle {111 var f = Date.FormatStyle(); f.locale = Fmt.locale; f.timeZone = MarketDate.zone; f.calendar = MarketDate.calendar112 return f113 }114 static func time(_ d: Date) -> String { d.formatted(base.hour(.twoDigits(amPM: .omitted)).minute(.twoDigits)) }115 static func dayMonth(_ d: Date) -> String { d.formatted(base.day().month(.abbreviated)) }116 static func monthYear(_ d: Date) -> String { d.formatted(base.month(.abbreviated).year(.twoDigits)) }117 static func year(_ d: Date) -> String { d.formatted(base.year()) }118 static func full(_ d: Date, intraday: Bool) -> String {119 let day = d.formatted(base.day().month(.abbreviated).year())120 return intraday ? "\(day), \(time(d))" : day121 }122123 /// The axis label format for a window going from `first` to `last`.124 static func axisLabel(_ d: Date, first: Date, last: Date, intraday: Bool) -> String {125 let span = last.timeIntervalSince(first)126 if intraday { return MarketDate.day(first) == MarketDate.day(last) ? time(d) : dayMonth(d) }127 if span > 3 * 366 * 86_400 { return year(d) }128 if span > 120 * 86_400 { return monthYear(d) }129 return dayMonth(d)130 }131}132