Files
maraphon-app/tests/Marathon.Application.Tests/UseCases/RunBacktestUseCaseTests.cs
T
alexei.dolgolyov 0d52b7beff feat(backtest): historical strategy backtester
Adds an interactive backtester that replays the SuspensionFlip detector over
all flagged anomalies under a chosen score threshold and staking rule
(flat / percent-of-bankroll / Kelly), and reports the headline numbers a
user needs to judge edge: final bankroll, ROI, max drawdown (peak-to-trough),
win/loss streaks, plus per-bet equity curve.

Domain (pure):
- StakeRule enum + BacktestStrategy params (with validation).
- BacktestSimulator: deterministic function taking strategy + chronological
  candidates → BacktestResult. Implements Kelly with post-flip implied prob
  as p (skipping negative-edge bets), peak-to-trough drawdown tracking, and
  win/loss streak rollups. Mirrors AnomalyOutcomeEvaluator on the 2-way Draw
  guard so tennis data inconsistencies are refused rather than miss-counted.
- Skipped counter split into SkippedByThreshold / SkippedByDataQuality /
  SkippedByBankroll so the UI can distinguish "strategy choice" from
  "data-quality" from "bankroll empty".

Application:
- RunBacktestUseCase: loads anomalies + events + results, parses evidence,
  builds candidates, hands event titles into the simulator so the UI does
  zero repository round-trips of its own.

UI:
- Pages/Anomalies/Backtest.razor: hero, strategy form (MudBlazor — conditional
  sub-field per staking rule), 4-card KPI strip (final bankroll / net profit
  / ROI / max drawdown), counters row, inline-SVG equity curve, trade-trace
  table with per-bet outcome pills and link-back to the source anomaly.
- Nav entry under Analysis. RU + EN i18n.

Tests: +20 (16 simulator math — flat / percent compounding / Kelly +/-
edge / quarter-Kelly / bankroll-exceeded / out-of-order chronology / Draw
favourite / multi-window drawdown / event-title pass-through + 4 use-case
join). All 399 tests pass.

Money rounding switched to MidpointRounding.AwayFromZero throughout the
simulator output for accounting convention.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-16 18:34:42 +03:00

131 lines
5.4 KiB
C#

using FluentAssertions;
using Marathon.Application.Abstractions;
using Marathon.Application.UseCases;
using Marathon.Domain.Backtesting;
using Marathon.Domain.Entities;
using Marathon.Domain.Enums;
using Marathon.Domain.ValueObjects;
using Microsoft.Extensions.Logging.Abstractions;
using NSubstitute;
namespace Marathon.Application.Tests.UseCases;
/// <summary>
/// Tests the orchestration: anomaly + event + result join + parse + delegate to
/// the simulator. The simulator's own correctness is covered in
/// Marathon.Domain.Tests.
/// </summary>
public sealed class RunBacktestUseCaseTests
{
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
private static readonly DateTimeOffset BaseTime =
new(2026, 5, 10, 18, 0, 0, MoscowOffset);
private readonly IAnomalyRepository _anomalies = Substitute.For<IAnomalyRepository>();
private readonly IEventRepository _events = Substitute.For<IEventRepository>();
private readonly IResultRepository _results = Substitute.For<IResultRepository>();
private RunBacktestUseCase CreateSut() =>
new(_anomalies, _events, _results, NullLogger<RunBacktestUseCase>.Instance);
private const string FlipEvidence = """
{
"suspensionGapSeconds": 90,
"preSuspension": {
"capturedAt": "2026-05-10T18:00:00+03:00",
"p1": 0.55, "pDraw": 0.20, "p2": 0.25,
"rate1": 1.8, "rateDraw": 4.5, "rate2": 4.0
},
"postSuspension": {
"capturedAt": "2026-05-10T18:02:30+03:00",
"p1": 0.25, "pDraw": 0.20, "p2": 0.55,
"rate1": 4.0, "rateDraw": 4.5, "rate2": 1.8
}
}
""";
private static Anomaly MakeAnomaly(EventId eventId, decimal score = 0.55m, string? evidence = null) =>
new(Guid.NewGuid(), eventId, BaseTime, AnomalyKind.SuspensionFlip,
score, evidence ?? FlipEvidence);
private static Event MakeEvent(EventId id) =>
new(id, new SportCode(11), "BY", "L1", "Cat", BaseTime, "Team A", "Team B");
private static BacktestStrategy DefaultStrategy(decimal minScore = 0.30m) =>
new(StartingBankroll: 1000m, MinScore: minScore,
StakeRule: StakeRule.Flat,
FlatStake: 100m, PercentOfBankroll: 0.02m, KellyFraction: 0.25m);
[Fact]
public async Task Should_ReturnEmptyResult_When_NoAnomaliesExist()
{
_anomalies.ListAsync(Arg.Any<CancellationToken>())
.Returns(Array.Empty<Anomaly>().ToList().AsReadOnly());
var result = await CreateSut().ExecuteAsync(DefaultStrategy(), CancellationToken.None);
result.BetsPlaced.Should().Be(0);
result.FinalBankroll.Should().Be(1000m);
}
[Fact]
public async Task Should_SimulateBet_When_AnomalyHasResult()
{
// Anomaly with result, Side2 (post-flip favourite) wins → +100.
var id = new EventId("event-1");
_anomalies.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { MakeAnomaly(id, score: 0.55m) }.ToList().AsReadOnly());
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id));
_results.GetAsync(id, Arg.Any<CancellationToken>())
.Returns(new EventResult(id, 0, 2, Side.Side2, DateTimeOffset.UtcNow));
var result = await CreateSut().ExecuteAsync(DefaultStrategy(), CancellationToken.None);
result.BetsPlaced.Should().Be(1);
result.Wins.Should().Be(1);
result.NetProfit.Should().Be(80m,
"stake 100 at rate 1.8 — win pays 180, profit 80");
}
[Fact]
public async Task Should_FilterOut_AnomaliesWithoutResults()
{
var graded = new EventId("graded");
var ungraded = new EventId("ungraded");
_anomalies.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { MakeAnomaly(graded), MakeAnomaly(ungraded) }.ToList().AsReadOnly());
_events.GetAsync(graded, Arg.Any<CancellationToken>()).Returns(MakeEvent(graded));
_events.GetAsync(ungraded, Arg.Any<CancellationToken>()).Returns(MakeEvent(ungraded));
_results.GetAsync(graded, Arg.Any<CancellationToken>())
.Returns(new EventResult(graded, 0, 2, Side.Side2, DateTimeOffset.UtcNow));
_results.GetAsync(ungraded, Arg.Any<CancellationToken>())
.Returns((EventResult?)null);
var result = await CreateSut().ExecuteAsync(DefaultStrategy(), CancellationToken.None);
// Only the graded anomaly should reach the simulator — the ungraded one is filtered out
// before the simulator, so it does NOT appear in result.Skipped.
(result.BetsPlaced + result.Skipped).Should().Be(1);
}
[Fact]
public async Task Should_FilterOut_AnomaliesWithMalformedEvidence()
{
var id = new EventId("bad-evidence");
_anomalies.ListAsync(Arg.Any<CancellationToken>())
.Returns(new[] { MakeAnomaly(id, evidence: "{not json") }.ToList().AsReadOnly());
_events.GetAsync(id, Arg.Any<CancellationToken>()).Returns(MakeEvent(id));
_results.GetAsync(id, Arg.Any<CancellationToken>())
.Returns(new EventResult(id, 0, 2, Side.Side2, DateTimeOffset.UtcNow));
var result = await CreateSut().ExecuteAsync(DefaultStrategy(), CancellationToken.None);
result.BetsPlaced.Should().Be(0);
result.Skipped.Should().Be(0,
"malformed evidence is filtered before the simulator — not counted as a strategy skip");
}
}