using Anubis.Backtest;
using Anubis.Data;
using Anubis.Platform;
using Anubis.Trading;
namespace Anubis.Tests;
///
/// The honest backtests of the base strategies (issue #44; SPEC 8.1, 12.2, 12.3): the Binance history in euro, the
/// protocol on both universes of crypto_trend, the expected path, the report and the outcome files read at the first
/// start.
///
[TestClass]
public sealed class ProtocolTests
{
private static readonly CancellationToken _none = CancellationToken.None;
private static ResearchBar Bar(DateOnly day, decimal close) =>
new(new DateTimeOffset(day.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero), close, close * 1.01m, close * 0.99m, close, 100m) { QuoteVolume = close * 100m };
private static FxRate Usd(DateOnly day, decimal rate) => new("EUR", "USD", day, rate, "ecb");
[TestMethod]
public void UsdtPricesBecomeEurosWithTheLastRateFixedBeforeTheBarOpens()
{
var thursday = new DateOnly(2024, 2, 29);
var friday = thursday.AddDays(1);
var bars = new[] { Bar(friday, 110m), Bar(friday.AddDays(1), 120m), Bar(friday.AddDays(3), 100m), Bar(friday.AddDays(4), 125m) };
var rates = new[] { Usd(thursday, 1.10m), Usd(friday, 1.20m), Usd(friday.AddDays(3), 1.25m) };
var euro = EuroHistory.Convert(bars, rates);
Assert.HasCount(4, euro);
Assert.AreEqual(100m, euro[0].Close, "Friday's bar opens before Friday's rate is fixed: Thursday's");
Assert.AreEqual(100m, euro[1].Close); // Saturday: Friday's rate.
Assert.AreEqual(83.333333333333m, euro[2].Close); // Monday: still Friday's, Monday's comes in the afternoon.
Assert.AreEqual(100m, euro[3].Close); // Tuesday: Monday's.
Assert.AreEqual(100m, euro[0].Volume, "the volume is in coins: it does not change");
Assert.AreEqual(10_000m, euro[0].QuoteVolume);
Assert.AreEqual("BTCEUR", EuroHistory.SymbolOf("BTCUSDT"));
}
[TestMethod]
public void ABarWithoutARecentRateIsDropped()
{
var day = new DateOnly(2024, 3, 1);
var bars = Enumerable.Range(-1, EuroHistory.MaxRateAgeDays + 3).Select(i => Bar(day.AddDays(i), 1m)).ToList();
var euro = EuroHistory.Convert(bars, [Usd(day, 1m)]);
Assert.HasCount(EuroHistory.MaxRateAgeDays, euro, "up to the first rate and after a week without rates there is no price in euro");
Assert.AreEqual(day.AddDays(1), DateOnly.FromDateTime(euro[0].OpensAt.UtcDateTime));
Assert.AreEqual(day.AddDays(EuroHistory.MaxRateAgeDays), DateOnly.FromDateTime(euro[^1].OpensAt.UtcDateTime));
}
[TestMethod]
public void AHoleOfMoreThanAWeekEndsTheSeriesBecauseWhatFollowsIsAnotherCoin()
{
var day = new DateOnly(2022, 5, 10);
var bars = new[] { Bar(day, 1m), Bar(day.AddDays(1), 0.0001m), Bar(day.AddDays(1 + EuroHistory.MaxGapDays + 1), 5m), Bar(day.AddDays(2 + EuroHistory.MaxGapDays + 1), 6m) };
var rates = Enumerable.Range(-1, 21).Select(i => Usd(day.AddDays(i), 1m)).ToList();
var euro = EuroHistory.Convert(bars, rates);
Assert.HasCount(2, euro, "the relaunch under the same ticker is not the coin that collapsed");
Assert.AreEqual(0.0001m, euro[^1].Close);
}
[TestMethod]
public void TheExpectedPathIsTheCompoundedPercentileAndRepeatsWithTheSameSeed()
{
var steady = PathBootstrap.Percentile([0.01, 0.01, 0.01], 3, 0.05, 50, 5, 1);
Assert.AreEqual(0.01, steady[0], 1e-12);
Assert.AreEqual(0.030301, steady[2], 1e-12);
var random = new Random(7);
var returns = Enumerable.Range(0, 500).Select(_ => (random.NextDouble() - 0.48) * 0.06).ToArray();
var p5 = PathBootstrap.Percentile(returns, 100, 0.05, 500, 20, 3);
var median = PathBootstrap.Percentile(returns, 100, 0.50, 500, 20, 3);
CollectionAssert.AreEqual(p5.ToList(), PathBootstrap.Percentile(returns, 100, 0.05, 500, 20, 3).ToList());
Assert.IsTrue(p5.Zip(median).All(x => x.First <= x.Second), "the 5th percentile is never above the median");
Assert.IsLessThan(p5[9], p5[99], "the bad tail widens with time");
}
[TestMethod]
public async Task TheProtocolRunsBothUniversesAndWritesTheReportWhateverTheOutcome()
{
var folder = TempFolder.Create("protocol");
try
{
var store = new ResearchStore(folder);
var random = new Random(11);
var start = new DateOnly(2017, 8, 1);
var end = new DateOnly(2020, 12, 31);
foreach (var coin in new[] { "BTC", "ETH", "SOL", "XRP", "LTC" })
{
var price = 100m;
var bars = new List();
for (var day = start; day <= end; day = day.AddDays(1))
{
price *= 1m + (decimal)((random.NextDouble() - 0.49) * 0.08);
bars.Add(Bar(day, Math.Round(price, 6)) with { Volume = 1_000_000m });
}
await store.WriteAsync(Venues.Binance, coin + "USDT", Timeframes.Day, bars, _none);
}
var rates = new List();
for (var day = start; day <= end; day = day.AddDays(1))
{
if (day.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday))
{
rates.Add(Usd(day, 1.15m));
}
}
var converted = await EuroHistory.ConvertAllAsync(store, rates, _none);
Assert.HasCount(5, converted);
var outcome = await CryptoTrendProtocol.RunAsync(folder, end, new DateTimeOffset(2021, 1, 2, 0, 0, 0, TimeSpan.Zero), null, _none);
Assert.HasCount(2, outcome.Runs);
Assert.HasCount(5, outcome.Runs[0].Report.Instruments);
CollectionAssert.AreEqual(CryptoTrendProtocol.Narrow.ToList(), outcome.Runs[1].Report.Instruments.Select(i => i.Symbol).ToList());
Assert.AreEqual("EUR", outcome.Runs[0].Report.Currency);
Assert.AreEqual(outcome.Runs[0].Report.Variants.Count + outcome.Runs[1].Report.Variants.Count, outcome.Runs[1].Report.TrialsCounted,
"the second universe counts the variants of the first as tries");
Assert.IsTrue(outcome.Runs.All(r => r.Report.LookAhead.Passed), "no decision changes when later data is added");
var q = outcome.Qualification;
Assert.AreEqual(outcome.Runs.All(r => r.Report.Passed), q.Passed);
Assert.AreEqual(CryptoTrendProtocol.ReportPath, q.Report);
Assert.HasCount(CryptoTrendProtocol.PathDays, q.ExpectedPathP5);
Assert.AreEqual(Math.Round((decimal)outcome.Runs[0].Report.Metrics.MaxDrawdown, 4), q.MaxDrawdown);
Assert.IsTrue(q.ExpectedSlippageBp is >= 5m and <= 15m);
Assert.AreEqual(2m * (0.002m + (q.ExpectedSlippageBp / 10_000m)), q.RoundTripCost);
if (!q.Passed)
{
StringAssert.Contains(q.Verdict, "Resta in osservazione");
}
var markdown = ProtocolReport.Markdown(outcome, "crypto_trend", ["dati di prova"], "anubis backtest base");
foreach (var part in new[] { "# Backtest di `crypto_trend`", "## Universo ampio", "## BTC, ETH e SOL", "Comprare e tenere", "×2", "×3", "**Per anno**",
"Deflated Sharpe Ratio", "convertiti in euro con il cambio BCE", "dati di prova" })
{
StringAssert.Contains(markdown, part);
}
var json = BacktestOutcomes.ToJson(q);
Assert.AreEqual(q.Verdict, System.Text.Json.JsonSerializer.Deserialize(json, Qualification.Json)!.Verdict);
StringAssert.Contains(json, "\"expected_path_p5\"");
}
finally
{
TempFolder.Delete(folder);
}
}
[TestMethod]
public void AFailedCheckIsDescribedInPlainItalian()
{
var check = new ReportCheck(ReportCheckKeys.Drawdown, "Calo massimo", 0.382, "≤", 0.25, "percent", false);
Assert.AreEqual("calo massimo 38,2%, serve ≤ 25%", CryptoTrendProtocol.Describe(check, System.Globalization.CultureInfo.GetCultureInfo("it-IT")));
}
[TestMethod]
public void EveryBaseStrategyHasItsOutcomeFileBuiltIn()
{
foreach (var (strategy, _, _) in StrategyNames.Base)
{
var outcome = BacktestOutcomes.Find(strategy);
Assert.IsNotNull(outcome, strategy);
Assert.AreEqual("docs/backtests/" + strategy + ".md", outcome.Report);
Assert.IsTrue(File.Exists(Path.Combine(RepositoryRoot(), outcome.Report)), "the report of " + strategy + " is in the repository");
}
foreach (var strategy in new[] { StrategyNames.EquityMomentum, StrategyNames.EquityMeanrev })
{
var outcome = BacktestOutcomes.Find(strategy)!;
Assert.IsFalse(outcome.Passed, "without the Alpaca history the stock strategies cannot pass");
StringAssert.Contains(outcome.Verdict, "chiavi Alpaca");
}
}
[TestMethod]
public async Task AtTheFirstStartTheStockStrategiesWaitInObservationAndCryptoFollowsItsOutcome()
{
await using var rig = await TradingRig.CreateAsync();
await AccountSetup.EnsureAsync(rig.Db, rig.Clock, alpacaDemo: false, okxDemo: false, _none);
var stages = new StageBook(rig.Db, rig.Clock, TradingConsent.None, (_, _, _) => Task.FromResult(false));
foreach (var (strategy, _, _) in StrategyNames.Base)
{
await stages.ApplyQualificationAsync(strategy, BacktestOutcomes.Find(strategy)!, _none);
}
foreach (var strategy in new[] { StrategyNames.EquityMomentum, StrategyNames.EquityMeanrev })
{
var stage = await stages.GetAsync(strategy, _none);
Assert.AreEqual(StrategyStages.Observation, stage!.Stage);
StringAssert.Contains(stage.Qualification!.Verdict, "chiavi Alpaca");
}
var crypto = (await stages.GetAsync(StrategyNames.CryptoTrend, _none))!;
Assert.AreEqual(BacktestOutcomes.Find(StrategyNames.CryptoTrend)!.Passed ? StrategyStages.Simulation : StrategyStages.Observation, crypto.Stage);
}
private static string RepositoryRoot()
{
var folder = new DirectoryInfo(AppContext.BaseDirectory);
while (folder is not null && !File.Exists(Path.Combine(folder.FullName, "Anubis.slnx")))
{
folder = folder.Parent;
}
return folder?.FullName ?? throw new InvalidOperationException("Repository root not found");
}
}