// Copyright (c) 2026 fxtradingguide.com // SPDX-License-Identifier: MIT using System.Globalization; using System.Security.Cryptography; namespace FxTradingGuide.Studies; /// The unmodified ECB sample shared by every study. public static class SampleSeries { public const string SeriesKey = "EXR.D.USD.EUR.SP00.A"; public const string SeriesTitle = "US dollar/Euro ECB reference exchange rate"; public const string SeriesDetailPrefix = "ECB reference exchange rate, US dollar/Euro"; public const string ExpectedSha256 = "daf52fd25bac5f1e7568b2e61f787caec95dd506fbe315331cbff17be4531c2c"; public const int ExpectedRows = 1283; public const string FileName = "EXR.D.USD.EUR.SP00.A.csv"; public static IReadOnlyList Load() { var path = FindFile(); var hash = Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path))).ToLowerInvariant(); if (!string.Equals(hash, ExpectedSha256, StringComparison.Ordinal)) { throw new InvalidOperationException($"ECB file hash is {hash}, expected {ExpectedSha256}"); } var lines = File.ReadAllLines(path); if (lines.Length < 2) { throw new InvalidOperationException($"{FileName} has no observations"); } var header = Csv.ParseLine(lines[0]); var columns = header .Select((name, index) => (name, index)) .ToDictionary(item => item.name, item => item.index, StringComparer.Ordinal); foreach (var name in new[] { "TIME_PERIOD", "OBS_VALUE", "KEY", "TITLE", "TITLE_COMPL" }) { if (!columns.ContainsKey(name)) { throw new InvalidOperationException($"{FileName} is missing {name}"); } } var observations = new List(lines.Length - 1); for (var i = 1; i < lines.Length; i++) { if (lines[i].Length == 0) { continue; } var fields = Csv.ParseLine(lines[i]); string Field(string name) => fields[columns[name]]; if (Field("KEY") != SeriesKey) { throw new InvalidOperationException($"unexpected series key {Field("KEY")}"); } if (Field("TITLE") != SeriesTitle) { throw new InvalidOperationException($"unexpected series title {Field("TITLE")}"); } if (!Field("TITLE_COMPL").StartsWith(SeriesDetailPrefix, StringComparison.Ordinal)) { throw new InvalidOperationException("TITLE_COMPL was not parsed as a quoted field"); } var text = Field("OBS_VALUE"); if (text.Length == 0) { throw new InvalidOperationException($"blank rate on {Field("TIME_PERIOD")}"); } observations.Add(new Observation( Field("TIME_PERIOD"), double.Parse(text, CultureInfo.InvariantCulture), text)); } observations.Sort(static (left, right) => string.CompareOrdinal(left.Date, right.Date)); if (observations.Count != ExpectedRows || observations[0].Date != "2020-01-02" || observations[0].RateText != "1.1193" || observations[^1].Date != "2024-12-31" || observations[^1].RateText != "1.0389") { throw new InvalidOperationException("ECB sample does not match the recorded first and last rows"); } return observations; } private static string FindFile() { for (var dir = new DirectoryInfo(AppContext.BaseDirectory); dir is not null; dir = dir.Parent) { var candidate = Path.Combine(dir.FullName, "strategies", "data", FileName); if (File.Exists(candidate)) { return candidate; } candidate = Path.Combine(dir.FullName, "data", FileName); if (File.Exists(candidate) && dir.Name == "strategies") { return candidate; } } throw new FileNotFoundException($"Could not find {FileName} by walking up from {AppContext.BaseDirectory}"); } }