using Anubis.Backtest; using Anubis.Data; using Anubis.Strategies; using Anubis.Trading; namespace Anubis.Tests; /// /// The golden reports of the trial judge (Phase 12, issue #118). They were written before the seam between the judge and the /// engines, so the seam must leave every byte of the report as it was. Elapsed seconds are the only field set to zero. /// To rewrite them on purpose: ANUBIS_WRITE_GOLDEN=1, then read the diff. /// [TestClass] public sealed class TrialSeamTests { private static readonly CancellationToken _none = CancellationToken.None; private static readonly DateTimeOffset Now = new(2027, 1, 5, 2, 10, 0, TimeSpan.Zero); private static readonly Guid Id = new("6d2f3a51-0c4e-4b7a-9a61-3f1e2d0c9b12"); private static readonly DateOnly Through = new(2026, 12, 31); [TestMethod] [TestCategory(Category.Golden)] public void TheReportOfTheAverageCrossDoesNotMove() { var data = BacktestData.One(BacktestData.Btc(), BacktestData.Walk(1500, 7, start: 20_000, drift: 0.0008, volatility: 0.03)); var plan = Laboratory.BuildPlan(new TrialRequest(MovingAverageCrossFamily.FamilyId, TrialSources.Database, Venues.Okx, ["BTC-EUR"], new DateOnly(2018, 6, 1), new DateOnly(2022, 2, 8), 10_000m), Now).Plan; var prior = new PriorTrials(2, 18, [0.4, 0.9, -0.1]); var report = TrialRunner.Run(Id, plan, TrialPlan.Hash(plan.ToJson()), MovingAverageCrossFamily.Instance, data, prior, Now).Report; Same("sma_cross", report); } [TestMethod] [TestCategory(Category.Golden)] public async Task TheReportOfTheCryptoTrendProtocolDoesNotMove() { Same("crypto_trend", await CryptoTrendAsync(null).ConfigureAwait(false)); } [TestMethod] [TestCategory(Category.Golden)] public async Task TheReportOfAJudgedVariantOutsideTheGridDoesNotMove() { Same("crypto_trend_judged", await CryptoTrendAsync("coin_vol=12;max_stop=15").ConfigureAwait(false)); } private static async Task CryptoTrendAsync(string? judged) { var folder = TempFolder.Create("golden-report"); try { var store = new ResearchStore(folder); var seed = 21u; foreach (var coin in new[] { "BTC", "ETH", "SOL", "XRP" }) { var bars = LiveRig.Synthetic(Through.AddDays(-799), 800, 100, seed++).Select(b => new ResearchBar(new DateTimeOffset(b.Day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero), (decimal)b.Open, (decimal)b.High, (decimal)b.Low, (decimal)b.Close, (decimal)b.Volume)).ToList(); await store.WriteAsync(EuroHistory.Venue, coin + "EUR", Timeframes.Day, bars, _none).ConfigureAwait(false); } var plan = Laboratory.BuildPlan(new TrialRequest(StrategyNames.CryptoTrend, TrialSources.Research, EuroHistory.Venue, CryptoTrendProtocol.Narrow, CryptoTrendProtocol.From, Through, CryptoTrendProtocol.Capital, null), Now).Plan with { Judged = judged }; var data = await Laboratory.LoadResearchAsync(folder, plan.Data, _none).ConfigureAwait(false); return TrialRunner.Run(Id, plan, TrialPlan.Hash(plan.ToJson()), StrategyCatalog.Find(StrategyNames.CryptoTrend)!, data, PriorTrials.None, Now).Report; } finally { TempFolder.Delete(folder); } } private static void Same(string name, BacktestReport report) { var json = (report with { Seconds = 0 }).ToJson(); var relative = Path.Combine("golden", name + ".report.json"); if (Environment.GetEnvironmentVariable("ANUBIS_WRITE_GOLDEN") == "1") { var target = Path.Combine(Repo.Root.FullName, "tests", "Anubis.Tests", "Fixtures", relative); Directory.CreateDirectory(Path.GetDirectoryName(target)!); File.WriteAllText(target, json); } Assert.AreEqual(Fixture.Text(relative), json, $"The report of {name} changed."); } }