using Anubis.Backtest; using Anubis.Baskets; using Anubis.Data; namespace Anubis.Tests; /// /// Builders of hand-made market data for the basket engine (Phase 14): daily bars on the currency weekdays, 15-minute bars /// with explicit bid and ask, the 28 crosses of seven synthetic rates, and log series that revert or walk. /// internal static class BasketSynthetic { /// A Monday in winter, far from any change of the clocks. public static readonly DateOnly Monday = new(2026, 1, 5); /// The first currency weekdays from (Monday to Friday). public static IReadOnlyList Weekdays(DateOnly start, int count) { var days = new List(count); for (var day = start; days.Count < count; day = day.AddDays(1)) { if (day.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday)) { days.Add(day); } } return days; } /// /// Daily bars like the ECB's (open = high = low = close, at 00:00 UTC of each weekday from ), one /// reference price a day and no ask: the engine builds bid and ask with the venue's spread. /// public static BarDataset Daily(IReadOnlyDictionary closes, DateOnly? start = null) { var length = closes.Values.Max(c => c.Length); var days = Weekdays(start ?? Monday, length); var bars = closes.ToDictionary( p => p.Key, p => (IReadOnlyList)[.. p.Value.Select((c, i) => Flat(Midnight(days[i]), c))], StringComparer.Ordinal); return BarDataset.Build(Timeframes.Day, BarCalendars.Fx, Midnight(days[0]), Midnight(days[^1]).AddDays(1), bars); } /// /// 15-minute bars from , one per entry of , with the ask given beside each /// bid (open, high, low, close of both sides). /// public static BarDataset Quarters(DateTimeOffset first, IReadOnlyDictionary quotes) { var bids = new Dictionary>(StringComparer.Ordinal); var asks = new Dictionary>(StringComparer.Ordinal); var length = 0; foreach (var (symbol, rows) in quotes) { bids[symbol] = [.. rows.Select((r, i) => r.Bid.Bar(first.AddMinutes(15 * i)))]; asks[symbol] = [.. rows.Select((r, i) => r.Ask.Bar(first.AddMinutes(15 * i)))]; length = Math.Max(length, rows.Length); } return BarDataset.Build(Timeframes.Minutes15, BarCalendars.Fx, first, first.AddMinutes(15 * length), bids, asks); } /// A quote whose four prices are one mid, with the bid half a spread below and the ask half above. public static (QuoteSide Bid, QuoteSide Ask) Mid(decimal mid, decimal spread) => (QuoteSide.Flat(mid - (spread / 2)), QuoteSide.Flat(mid + (spread / 2))); /// The 28 ECB-like crosses of seven synthetic rates against the euro, one fixing a weekday. public static BarDataset Crosses(int days, int seed, double halfLife = 50, double volatility = 0.005, DateOnly? start = null) { var dates = Weekdays(start ?? new DateOnly(2020, 1, 6), days); var levels = new Dictionary(StringComparer.Ordinal) { ["GBP"] = 0.86, ["AUD"] = 1.62, ["NZD"] = 1.78, ["USD"] = 1.10, ["CAD"] = 1.48, ["CHF"] = 0.97, ["JPY"] = 150, }; var rates = new List(); var k = 0; foreach (var (currency, level) in levels) { var path = Reverting(days, Synthetic.Phi(halfLife), seed + (k++ * 7919), volatility); rates.AddRange(dates.Select((d, i) => new FxRate(Currencies.Eur, currency, d, Math.Round((decimal)(level * Math.Exp(path[i])), 6), "ecb"))); } var crosses = EcbCrosses.Build(rates); return BarDataset.Build(Timeframes.Day, BarCalendars.Fx, Midnight(dates[0]), Midnight(dates[^1]).AddDays(1), crosses); } /// A log series that reverts to zero: s(t) = φ·s(t−1) + σ·ε, from zero. public static double[] Reverting(int bars, double phi, int seed, double volatility = 0.01) { var random = new Gen(seed); var s = new double[bars]; for (var t = 1; t < bars; t++) { s[t] = (phi * s[t - 1]) + (volatility * random.Normal()); } return s; } /// A log series that walks: s(t) = s(t−1) + drift + σ·ε, from zero. public static double[] Walk(int bars, int seed, double drift = 0, double volatility = 0.01) { var random = new Gen(seed); var s = new double[bars]; for (var t = 1; t < bars; t++) { s[t] = s[t - 1] + drift + (volatility * random.Normal()); } return s; } /// Prices that alternate between two levels for bars: a window with a known spread. public static IEnumerable Alternate(double low, double high, int count) => Enumerable.Range(0, count).Select(i => i % 2 == 0 ? low : high); public static DateTimeOffset Midnight(DateOnly day) => new(day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); private static ResearchBar Flat(DateTimeOffset at, double close) { var price = (decimal)close; return new ResearchBar(at, price, price, price, price, 0m); } } /// Open, high, low and close of one side of a quote. internal readonly record struct QuoteSide(decimal Open, decimal High, decimal Low, decimal Close) { public static QuoteSide Flat(decimal price) => new(price, price, price, price); public ResearchBar Bar(DateTimeOffset at) => new(at, Open, High, Low, Close, 0m); }