MarketCollector downloads what is missing at startup and after every drop of the live feed, checks every bar and never corrects in silence: suspect bars are stored as suspect and read a second time (same values confirm, different ones replace, impossible ones are dropped), gaps are downloaded again once and what the source still lacks is recorded as unrecoverable. A test detaches the feed for ten minutes and finds every minute recovered. The stock and crypto universes are chosen "as of a date" from the bars up to that date only: funds, ETFs and ETNs are excluded through nasdaqtraded.txt and the security name, stocks delisted later still count when they traded. Corporate actions are kept once per source key and found by ex-date. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
223 lines
12 KiB
C#
223 lines
12 KiB
C#
using System.Threading.Channels;
|
|
using Anubis.Core;
|
|
using Anubis.Data;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>The collector of issue #20: backfill, checks, second readings, live feed, drops and gap recovery.</summary>
|
|
[TestClass]
|
|
public sealed class CollectorTests
|
|
{
|
|
[TestMethod]
|
|
[TestCategory(Category.Integration)]
|
|
public async Task FeedDetachedForTenMinutesIsRecoveredOnReconnection()
|
|
{
|
|
var clock = new FastClock(new DateTimeOffset(2026, 11, 10, 10, 0, 30, TimeSpan.Zero));
|
|
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
|
var bus = new EventBus();
|
|
using var feed = bus.Subscribe<FeedChanged>();
|
|
var freshness = new FreshnessMonitor(clock, bus);
|
|
var btc = await new InstrumentCatalog(test.Db, clock).EnsureAsync(MarketDataStoreTests.Okx("BTC-EUR"), CancellationToken.None).ConfigureAwait(false);
|
|
var source = new FakeMarketSource(clock);
|
|
var quiet = new DateTimeOffset(2026, 11, 10, 9, 30, 0, TimeSpan.Zero);
|
|
source.NoTrades.Add(quiet);
|
|
var collector = new MarketCollector(source, test.Db, clock, bus, freshness);
|
|
var bars = new BarStore(test.Db, clock);
|
|
var issues = new DataIssueStore(test.Db, clock);
|
|
|
|
using var stop = new CancellationTokenSource();
|
|
var live = collector.RunLiveAsync([btc], stop.Token);
|
|
await OkxTests.WaitForAsync(() => source.Connections == 1).ConfigureAwait(false);
|
|
|
|
// Startup: two days of minutes downloaded; the minute without trades is recorded, not invented.
|
|
var start = clock.UtcNow;
|
|
Assert.AreEqual(new DateTimeOffset(2026, 11, 10, 9, 59, 0, TimeSpan.Zero), await bars.LastMinuteAsync(btc.Id, CancellationToken.None).ConfigureAwait(false));
|
|
var startup = await bars.MinuteAsync(btc.Id, start.AddDays(-3), start, CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(2 * 24 * 60 - 1, startup.Count);
|
|
var unrecoverable = (await issues.SinceAsync(start.AddDays(-1), CancellationToken.None).ConfigureAwait(false)).Single();
|
|
Assert.AreEqual((IssueKinds.Gap, IssueStatus.Unrecoverable, quiet), (unrecoverable.Kind, unrecoverable.Status, unrecoverable.StartsAt));
|
|
StringAssert.Contains(unrecoverable.Detail, "senza scambi");
|
|
|
|
// Live: a closed bar arrives and the instrument is fresh.
|
|
clock.Advance(TimeSpan.FromSeconds(31));
|
|
await source.Live!.Writer.WriteAsync(new BarEvent("BTC-EUR", FakeMarketSource.MinuteBar(new DateTimeOffset(2026, 11, 10, 10, 0, 0, TimeSpan.Zero)), true))
|
|
.ConfigureAwait(false);
|
|
await OkxTests.WaitForAsync(() => freshness.IsFresh(btc.Id)).ConfigureAwait(false);
|
|
Assert.IsTrue(collector.Connected);
|
|
|
|
// The network goes down for ten minutes: the feed drops and nothing answers, not even the REST calls.
|
|
var dropAt = clock.UtcNow;
|
|
source.DownUntil = dropAt.AddMinutes(10);
|
|
source.Live.Writer.Complete();
|
|
await OkxTests.WaitForAsync(() => source.Connections == 2).ConfigureAwait(false);
|
|
Assert.IsTrue(clock.UtcNow >= dropAt.AddMinutes(10), "reconnected only after the outage");
|
|
Assert.IsFalse(freshness.IsFresh(btc.Id), "no live datum since the drop: no new entries");
|
|
|
|
// Back: every minute missed is downloaded before the feed resumes.
|
|
var resumed = new DateTimeOffset(clock.UtcNow.UtcTicks - (clock.UtcNow.UtcTicks % TimeSpan.TicksPerMinute), TimeSpan.Zero);
|
|
var recovered = await bars.MinuteAsync(btc.Id, new DateTimeOffset(2026, 11, 10, 10, 0, 0, TimeSpan.Zero), resumed, CancellationToken.None).ConfigureAwait(false);
|
|
Assert.IsTrue(recovered.Count >= 11, $"at least the ten minutes of the outage, got {recovered.Count}");
|
|
CollectionAssert.AreEqual(
|
|
QualityChecks.EveryMinute(new DateTimeOffset(2026, 11, 10, 10, 0, 0, TimeSpan.Zero), resumed).ToList(),
|
|
recovered.Select(b => b.OpensAt).ToList(), "no minute is missing");
|
|
Assert.AreEqual(1, (await issues.SinceAsync(start.AddDays(-1), CancellationToken.None).ConfigureAwait(false)).Count, "no new issue");
|
|
|
|
await source.Live!.Writer.WriteAsync(new TradeEvent("BTC-EUR", clock.UtcNow, 50_010m, 0.01m)).ConfigureAwait(false);
|
|
await OkxTests.WaitForAsync(() => freshness.IsFresh(btc.Id)).ConfigureAwait(false);
|
|
|
|
await stop.CancelAsync().ConfigureAwait(false);
|
|
await live.ConfigureAwait(false);
|
|
var changes = new List<bool>();
|
|
while (feed.TryRead(out var change))
|
|
{
|
|
changes.Add(change!.Connected);
|
|
}
|
|
|
|
CollectionAssert.AreEqual(new[] { true, false, true, false }, changes);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Integration)]
|
|
public async Task SuspectBarsAreReadAgainAndNeverFixedInSilence()
|
|
{
|
|
var day0 = new DateOnly(2026, 6, 1);
|
|
var clock = new FastClock(new DateTimeOffset(day0.AddDays(120).ToDateTime(new TimeOnly(6, 0)), TimeSpan.Zero));
|
|
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
|
|
var bus = new EventBus();
|
|
var btc = await new InstrumentCatalog(test.Db, clock).EnsureAsync(MarketDataStoreTests.Okx("BTC-EUR"), CancellationToken.None).ConfigureAwait(false);
|
|
var walk = MarketDataLogicTests.Walk(120, day0);
|
|
var source = new FakeMarketSource(clock);
|
|
foreach (var bar in walk)
|
|
{
|
|
source.Days[bar.Day] = bar;
|
|
}
|
|
|
|
var spike = day0.AddDays(80);
|
|
var broken = day0.AddDays(90);
|
|
var lost = day0.AddDays(50);
|
|
source.FirstReadings[spike] = new Queue<DailyBar>([walk[80] with { High = walk[80].High * 3, Close = walk[80].Close * 3 }]);
|
|
source.Days[broken] = walk[90] with { Low = -1 };
|
|
source.MissingOnFirstRead.Add(lost);
|
|
|
|
var collector = new MarketCollector(source, test.Db, clock, bus, new FreshnessMonitor(clock, bus));
|
|
var result = await collector.BackfillDailyAsync(btc, day0, CancellationToken.None).ConfigureAwait(false);
|
|
|
|
var bars = new BarStore(test.Db, clock);
|
|
var stored = await bars.DailyAsync(btc.Id, day0, day0.AddDays(200), CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(119, stored.Count, "the impossible day is dropped, the lost one recovered");
|
|
Assert.AreEqual(walk[80].Close, stored.Single(b => b.Day == spike).Close, "the second reading replaced the spike");
|
|
Assert.IsFalse(stored.Any(b => b.Day == broken));
|
|
Assert.IsTrue(stored.Any(b => b.Day == lost));
|
|
|
|
var issues = await new DataIssueStore(test.Db, clock).SinceAsync(DateTimeOffset.MinValue, CancellationToken.None).ConfigureAwait(false);
|
|
var byDay = issues.ToLookup(i => DateOnly.FromDateTime(i.StartsAt.UtcDateTime));
|
|
Assert.AreEqual(IssueStatus.Corrected, byDay[spike].Single(i => i.Kind == IssueKinds.Jump).Status);
|
|
StringAssert.Contains(byDay[spike].Single(i => i.Kind == IssueKinds.Jump).Detail, "invece di");
|
|
Assert.AreEqual(IssueStatus.Unrecoverable, byDay[broken].Single(i => i.Kind == IssueKinds.NonPositive).Status);
|
|
Assert.AreEqual(IssueStatus.Recovered, byDay[lost].Single(i => i.Kind == IssueKinds.Gap).Status);
|
|
Assert.IsTrue(issues.All(i => i.Attempts == 1), "each problem was tried once more");
|
|
Assert.AreEqual(0, (await new DataIssueStore(test.Db, clock).BlockedInstrumentsAsync(CancellationToken.None).ConfigureAwait(false)).Count,
|
|
"once read again, nothing blocks new entries");
|
|
Assert.IsTrue(result.Suspect >= 2 && result.Corrected >= 1 && result.Unrecoverable == 1 && result.Recovered == 1, result.ToString());
|
|
|
|
// A suspect bar the second reading confirms stays, marked as confirmed.
|
|
source.Days[day0.AddDays(120)] = walk[^1] with { Day = day0.AddDays(120), Open = walk[^1].Close, Low = walk[^1].Close * 0.5, Close = walk[^1].Close * 0.5 };
|
|
clock.Advance(TimeSpan.FromDays(1));
|
|
var next = await collector.BackfillDailyAsync(btc, day0, CancellationToken.None).ConfigureAwait(false);
|
|
Assert.AreEqual(1, next.Confirmed);
|
|
Assert.AreEqual(BarQuality.Confirmed, (await bars.DailyAsync(btc.Id, day0.AddDays(120), day0.AddDays(120), CancellationToken.None).ConfigureAwait(false))[0].Quality);
|
|
Assert.AreEqual(CollectionResult.None, await collector.BackfillDailyAsync(btc, day0, CancellationToken.None).ConfigureAwait(false), "nothing new");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A venue for the collector tests: minute bars exist for every past minute except <see cref="NoTrades"/>, daily
|
|
/// bars come from <see cref="Days"/>, and the whole venue can be down until <see cref="DownUntil"/>.
|
|
/// </summary>
|
|
internal sealed class FakeMarketSource(IClock clock) : IMarketDataSource
|
|
{
|
|
public string Venue => Venues.Okx;
|
|
|
|
public HashSet<DateTimeOffset> NoTrades { get; } = [];
|
|
|
|
public Dictionary<DateOnly, DailyBar> Days { get; } = [];
|
|
|
|
/// <summary>What the first reading of a day returns instead of <see cref="Days"/>.</summary>
|
|
public Dictionary<DateOnly, Queue<DailyBar>> FirstReadings { get; } = [];
|
|
|
|
public HashSet<DateOnly> MissingOnFirstRead { get; } = [];
|
|
|
|
public DateTimeOffset? DownUntil { get; set; }
|
|
|
|
public Channel<MarketEvent>? Live { get; private set; }
|
|
|
|
public int Connections;
|
|
|
|
public static Bar MinuteBar(DateTimeOffset t)
|
|
{
|
|
var price = 50_000 + (20 * Math.Sin(t.ToUnixTimeSeconds() / 60.0 / 15));
|
|
return new Bar(t, price, price + 5, price - 5, price + 1, 0.5);
|
|
}
|
|
|
|
public Task<IReadOnlyList<InstrumentSpec>> ListInstrumentsAsync(CancellationToken cancellationToken) =>
|
|
Task.FromResult<IReadOnlyList<InstrumentSpec>>([MarketDataStoreTests.Okx("BTC-EUR")]);
|
|
|
|
public Task<IReadOnlyList<DailyBar>> FetchDailyBarsAsync(string symbol, DateOnly first, DateOnly last, CancellationToken cancellationToken)
|
|
{
|
|
ThrowIfDown();
|
|
var list = new List<DailyBar>();
|
|
for (var day = first; day <= last; day = day.AddDays(1))
|
|
{
|
|
if (FirstReadings.TryGetValue(day, out var queue) && queue.Count > 0)
|
|
{
|
|
list.Add(queue.Dequeue());
|
|
}
|
|
else if (!MissingOnFirstRead.Remove(day) && Days.TryGetValue(day, out var bar))
|
|
{
|
|
list.Add(bar);
|
|
}
|
|
}
|
|
|
|
return Task.FromResult<IReadOnlyList<DailyBar>>(list);
|
|
}
|
|
|
|
public Task<IReadOnlyList<Bar>> FetchMinuteBarsAsync(string symbol, DateTimeOffset start, DateTimeOffset until, CancellationToken cancellationToken)
|
|
{
|
|
ThrowIfDown();
|
|
var now = clock.UtcNow;
|
|
var list = new List<Bar>();
|
|
for (var t = start; t < until && t.AddMinutes(1) <= now; t = t.AddMinutes(1))
|
|
{
|
|
if (!NoTrades.Contains(t))
|
|
{
|
|
list.Add(MinuteBar(t));
|
|
}
|
|
}
|
|
|
|
return Task.FromResult<IReadOnlyList<Bar>>(list);
|
|
}
|
|
|
|
public Task<Quote> FetchQuoteAsync(string symbol, CancellationToken cancellationToken) =>
|
|
Task.FromResult(new Quote(symbol, 49_999m, 50_001m, clock.UtcNow));
|
|
|
|
public async IAsyncEnumerable<MarketEvent> StreamAsync(IReadOnlyCollection<string> symbols,
|
|
[System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
ThrowIfDown();
|
|
Live = Channel.CreateUnbounded<MarketEvent>();
|
|
Interlocked.Increment(ref Connections);
|
|
await foreach (var item in Live.Reader.ReadAllAsync(cancellationToken).ConfigureAwait(false))
|
|
{
|
|
yield return item;
|
|
}
|
|
}
|
|
|
|
private void ThrowIfDown()
|
|
{
|
|
if (DownUntil is { } until && clock.UtcNow < until)
|
|
{
|
|
throw new MarketDataException("rete assente", retryable: true);
|
|
}
|
|
}
|
|
}
|