Files
Alby96andClaude Opus 5.5 b087dd215b
ci / test (postgres) (push) Successful in 8m20s
ci / test (sqlite) (push) Successful in 4m25s
ci / test (postgres) (pull_request) Successful in 8m13s
ci / test (sqlite) (pull_request) Successful in 4m38s
ci / image (push) Successful in 2m5s
ci / image (pull_request) Successful in 2m6s
Apply the SPEC 10.5 news and calendar rules to new entries (refs #59)
Confirmed serious events hold new purchases of an instrument for 72 hours
and keep its stop where it is; confirmed negative news holds them for 24
hours; an unconfirmed rumour only gets a diary note and a check two hours
later. Coming earnings keep mean reversion out and macro figures hold
entries around their release, now in the live engine too. The strictest
row wins, every row can be switched off in the settings, and windows count
from the fact, so after an absence only effects still in force apply.

A held purchase moves to the end of its restriction while the order is
still valid and keeps its first due time, so the price drift check of
SPEC 5.6 still compares with the decision. Each restriction is written to
the diary once, with its motive. The per-instrument state keeps the tone
over one and three days, the story count against its norm and the active
restriction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:39:26 +01:00

261 lines
16 KiB
C#

using Anubis.Core;
using Anubis.Data;
using Anubis.Trading;
namespace Anubis.Tests;
/// <summary>
/// Recovery after an absence (issue #49, SPEC 5.6): the bot is switched off for 3 hours, 20 hours and 3 days with a
/// simulated clock, and the rows of 5.6 whose jobs exist in this phase are checked at the restart.
/// </summary>
[TestClass]
public sealed class AbsenceTests
{
private static readonly CancellationToken _none = CancellationToken.None;
private static readonly DateOnly LiveFrom = new(2026, 9, 1);
// The bot is switched off the evening before the first live day, at 23:00 UTC.
private static readonly DateTimeOffset Off = new(2026, 8, 31, 23, 0, 0, TimeSpan.Zero);
/// <summary>
/// Off from 23:00 to 02:00 UTC: the crypto decision of 00:05 is made at the restart and its purchases go if the
/// price is within 2% of the one at 00:05; one that moved more is dropped. Both end in the diary with the reason.
/// The missed backup is taken once; the morning summary is not due yet; no notice on the Panoramica.
/// </summary>
[TestMethod]
public async Task ThreeHoursOffRecoverTheCryptoDecisionAndDropAnEntryThatMovedTooFar()
{
await using var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false);
var jobs = await RestartAsync(live.Rig, Off.AddHours(3)).ConfigureAwait(false);
Assert.AreEqual("Sono stato fermo per 3 ore. Ho ripreso i lavori saltati con le regole del recupero.", await LastDiaryAsync(live.Rig, "absence").ConfigureAwait(false));
Assert.IsNull(await new AbsenceBook(live.Rig.Db, live.Rig.Clock).DeclaredAsync(_none).ConfigureAwait(false));
Assert.AreEqual(1, jobs.Backups);
Assert.AreEqual(0, jobs.Summaries);
Assert.AreEqual(0, jobs.Plain, "A job without recovery rules is not run for the time it missed.");
// The data step brings yesterday's bar; SOL has moved 3% from the price of 00:05, ETH has not.
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
live.Quotes(LiveFrom, b => b.Open);
var sol = (decimal)live.Bar("SOL-EUR", LiveFrom).Open * 1.03m;
live.Rig.Quote("SOL-EUR", sol * 0.9999m, sol * 1.0001m, 1000m);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT run_key, mode FROM engine_runs", r => (r.Text("run_key"), r.Text("mode"))).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { ("crypto_trend:2026-08-31", DecisionModes.Full) }, runs.ToList());
var fills = await live.LiveFillsAsync().ConfigureAwait(false);
Assert.IsTrue(fills.Any(f => f.Symbol == "ETH-EUR" && f.Side == OrderSide.Buy), "The recovered purchase goes.");
Assert.IsFalse(fills.Any(f => f.Symbol == "SOL-EUR"), "The purchase whose price moved 3% is dropped.");
var diary = await DiaryAsync(live.Rig).ConfigureAwait(false);
Assert.Contains("Recuperato un lavoro saltato: la chiusura giornaliera crypto (previsto alle 02:05).", diary);
Assert.Contains("Recuperato un lavoro saltato: l'acquisto di ETH/EUR (previsto alle 02:05).", diary);
Assert.IsTrue(diary.Any(d => d.StartsWith("Scartato l'acquisto di SOL/EUR: il prezzo si è mosso del 3", StringComparison.Ordinal)), string.Join("\n", diary));
var planned = await live.Rig.QueryAsync("SELECT outcome FROM planned_orders WHERE status = @s", r => r.Text("outcome"), ("s", PlannedStates.Dropped)).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { LiveReasons.PriceMoved }, planned.ToList());
}
/// <summary>
/// Off for 20 hours, back at 19:00 UTC: the crypto decision is past 12:00 and only exits and stops are updated;
/// the meanrev purchase decided at the last close expired an hour after the open; the morning summary is
/// skipped (past 12:00 in Italy) and said so in the diary; the backup is taken once; no notice on the Panoramica.
/// </summary>
[TestMethod]
public async Task TwentyHoursOffLetTheLateDecisionsUpdateOnlyExitsAndDropExpiredEntries()
{
await using (var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false))
{
var jobs = await RestartAsync(live.Rig, Off.AddHours(20)).ConfigureAwait(false);
Assert.AreEqual(1, jobs.Backups);
Assert.AreEqual(0, jobs.Summaries);
Assert.HasCount(1, jobs.Skipped);
Assert.AreEqual("Saltato: riepilogo del mattino (previsto alle 08:00). Il riavvio è arrivato oltre l'orario utile.",
AbsenceBook.MissedLine(jobs.Skipped[0], Zones.Rome));
Assert.IsNull(await new AbsenceBook(live.Rig.Db, live.Rig.Clock).DeclaredAsync(_none).ConfigureAwait(false));
// The engine pulses before the data step has brought yesterday's bar: the late decision waits for it and
// asks for it, instead of being written off as "no data" at the first pulse.
live.Quotes(LiveFrom, b => b.Close);
await live.MinutesAsync(3).ConfigureAwait(false);
Assert.IsEmpty(await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false), "A late decision waits for its bars.");
await live.StoreBarAsync(LiveFrom.AddDays(-1)).ConfigureAwait(false);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT mode FROM engine_runs", r => r.Text("mode")).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { DecisionModes.ExitsOnly }, runs.ToList());
Assert.IsEmpty(await live.LiveFillsAsync().ConfigureAwait(false), "No purchase after 12:00 UTC: the next close decides.");
var decision = await LastDiaryAsync(live.Rig, "decision").ConfigureAwait(false);
Assert.Contains(ReasonCatalog.Text(LiveReasons.LateExitsOnly), decision!);
}
// The stock side: the bot goes off after the signals of the close and comes back the next day at 12:30 in New
// York, past the hour after the open in which a meanrev purchase is valid.
await using var stocks = await StockRig.CreateAsync(LiveFrom, LiveFrom.AddDays(41)).ConfigureAwait(false);
await stocks.SessionAsync(LiveFrom).ConfigureAwait(false);
var pending = await stocks.Rig.QueryAsync("SELECT id FROM planned_orders WHERE status = @s", r => r.Id("id"), ("s", PlannedStates.Pending)).ConfigureAwait(false);
Assert.HasCount(1, pending, "The close of the first day plans a purchase for the next open.");
stocks.Rig.Clock.Set(new DateTimeOffset(2026, 9, 2, 16, 30, 0, TimeSpan.Zero));
stocks.Quotes(LiveFrom.AddDays(1), b => b.Open);
await stocks.MinutesAsync(3).ConfigureAwait(false);
var expired = await stocks.Rig.QueryAsync("SELECT outcome FROM planned_orders WHERE status = @s", r => r.Text("outcome"), ("s", PlannedStates.Expired)).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { LiveReasons.EntryExpired }, expired.ToList());
Assert.IsEmpty(await stocks.LiveFillsAsync().ConfigureAwait(false));
Assert.IsTrue((await DiaryAsync(stocks.Rig).ConfigureAwait(false)).Any(d => d.StartsWith("Saltato: l'acquisto di BBB. Ingresso scaduto", StringComparison.Ordinal)));
}
/// <summary>
/// Off for 3 days: only the decision of the restart day is made (the missed ones are not replayed), the backup is
/// taken once and not three times, and the Panoramica declares the absence until the user has seen it.
/// </summary>
[TestMethod]
public async Task ThreeDaysOffAreDeclaredUntilSeenAndMissedRunsAreMadeOnce()
{
await using var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false);
var back = Off.AddDays(3);
var jobs = await RestartAsync(live.Rig, back).ConfigureAwait(false);
Assert.AreEqual(1, jobs.Backups, "Three nights missed, one backup at the restart.");
Assert.AreEqual("Sono stato fermo per 3 giorni. Ho ripreso i lavori saltati con le regole del recupero.", await LastDiaryAsync(live.Rig, "absence").ConfigureAwait(false));
var book = new AbsenceBook(live.Rig.Db, live.Rig.Clock);
var declared = await book.DeclaredAsync(_none).ConfigureAwait(false);
Assert.IsNotNull(declared);
Assert.AreEqual(TimeSpan.FromDays(3), declared.Length);
await book.AcknowledgeAsync(declared.Id, _none).ConfigureAwait(false);
Assert.IsNull(await book.DeclaredAsync(_none).ConfigureAwait(false));
for (var day = LiveFrom.AddDays(-1); day <= LiveFrom.AddDays(2); day = day.AddDays(1))
{
await live.StoreBarAsync(day).ConfigureAwait(false);
}
live.Quotes(LiveFrom.AddDays(2), b => b.Close);
await live.MinutesAsync(5).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT run_key FROM engine_runs", r => r.Text("run_key")).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { "crypto_trend:2026-09-02" }, runs.ToList());
}
/// <summary>
/// The row of 5.6 on news and calendar: back after 51 hours, at 02:00 UTC of 3 September, the bot reads the backlog
/// of the news. An effect still in force applies (a serious event on ETH published 44 hours before, 72 hours long);
/// one already over does not (a negative story on SOL 25 hours old, 24 hours long), nor a figure of the day before.
/// The crypto decision of the night is still made, and it buys no ETH.
/// </summary>
[TestMethod]
public async Task AfterTwoDaysOffOnlyTheNewsEffectsStillInForceApply()
{
await using var live = await LiveRig.CreateAsync(LiveFrom, LiveFrom.AddDays(39)).ConfigureAwait(false);
var db = live.Rig.Db;
var clock = live.Rig.Clock;
live.UseGate(new News.EntryGate(db, clock));
var back = Off.AddHours(51);
await RestartAsync(live.Rig, back).ConfigureAwait(false);
foreach (var coin in new[] { "ETH-EUR", "SOL-EUR" })
{
await db.WriteAsync(s => s.ExecuteAsync("INSERT INTO universe_members (id, universe, as_of, instrument_id, rank, created_at) VALUES (@id, @u, '2026-09-02', @i, 1, @now)", _none,
("id", Ids.NewId(clock)), ("u", Universes.Crypto), ("i", live.Instruments[coin].Id), ("now", back)), _none).ConfigureAwait(false);
}
// The backlog, read at the restart.
var eth = live.Instruments["ETH-EUR"].Spec;
var sol = live.Instruments["SOL-EUR"].Spec;
await NewsRulesTests.StoryAsync(db, clock, eth, "Ethereum bridge exploited", back.AddHours(-44), 4, -0.9, "CoinDesk", trusted: true).ConfigureAwait(false);
await NewsRulesTests.StoryAsync(db, clock, sol, "Solana outage halts the network", back.AddHours(-25), 3, -0.8, "CoinDesk", trusted: true).ConfigureAwait(false);
await new CalendarEventStore(db, clock).UpsertAsync(
[new CalendarEvent(CalendarEventKinds.Economic, "Inflazione USA", back.AddHours(-13.5), true, "test", "cpi") { Currency = "USD", Impact = "High" }], _none).ConfigureAwait(false);
Assert.AreEqual(1, await new News.NewsWatcher(db, clock).RunOnceAsync(_none).ConfigureAwait(false));
Assert.AreEqual("Ho messo una restrizione per 72 ore: niente nuovi acquisti di ETH/EUR. Motivo: «Ethereum bridge exploited» (CoinDesk).",
(await DiaryAsync(live.Rig).ConfigureAwait(false))[^1]);
var gate = new News.EntryGate(db, clock);
var held = await gate.HoldAsync(eth, back, _none).ConfigureAwait(false);
Assert.AreEqual(News.NewsReasons.Severe, held!.Code);
Assert.AreEqual(back.AddHours(28), held.Until);
Assert.IsNull(await gate.HoldAsync(sol, back, _none).ConfigureAwait(false), "The 24 hours of SOL ended while the bot was off.");
Assert.IsNull(await gate.HoldAsync(live.Instruments["BTC-EUR"].Spec, back, _none).ConfigureAwait(false), "The figure was yesterday.");
for (var day = LiveFrom.AddDays(-1); day <= LiveFrom.AddDays(1); day = day.AddDays(1))
{
await live.StoreBarAsync(day).ConfigureAwait(false);
}
live.Quotes(LiveFrom.AddDays(2), b => b.Open);
await live.MinutesAsync(10).ConfigureAwait(false);
var runs = await live.Rig.QueryAsync("SELECT run_key, mode FROM engine_runs", r => (r.Text("run_key"), r.Text("mode"))).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { ("crypto_trend:2026-09-02", DecisionModes.Full) }, runs.ToList());
Assert.IsFalse((await live.LiveFillsAsync().ConfigureAwait(false)).Any(f => f is { Symbol: "ETH-EUR", Side: OrderSide.Buy }));
var news = await live.Rig.QueryAsync("SELECT symbol, reasons FROM decisions WHERE stage = @c", r => (Symbol: r.TextOrNull("symbol"), Reasons: r.Text("reasons")),
("c", DecisionStages.Correctives)).ConfigureAwait(false);
Assert.IsFalse(news.Any(n => n.Symbol == "SOL-EUR" && n.Reasons.Contains("news_", StringComparison.Ordinal)), "No rule acts on SOL.");
Assert.IsTrue(news.Any(n => n.Symbol == "ETH-EUR" && n.Reasons.Contains(News.NewsReasons.Severe, StringComparison.Ordinal)), "The purchase of ETH was proposed and held back.");
Assert.IsTrue((await live.LiveFillsAsync().ConfigureAwait(false)).Any(f => f is { Symbol: "SOL-EUR", Side: OrderSide.Buy }), "SOL is bought as the strategy decided.");
}
private sealed class Counts
{
public int Backups { get; set; }
public int Summaries { get; set; }
public int Plain { get; set; }
public List<JobRun> Skipped { get; } = [];
}
/// <summary>
/// The restart: the last sign of life was at <see cref="Off"/>; the clock is now <paramref name="back"/>. The
/// absence is recorded, then the scheduler starts with three jobs that ran last at <see cref="Off"/>: the nightly
/// backup (made once whenever the restart comes), the morning summary (only until 12:00 in Italy) and a job without
/// recovery rules.
/// </summary>
private static async Task<Counts> RestartAsync(TradingRig rig, DateTimeOffset back)
{
await rig.Db.WriteAsync(s => s.ExecuteAsync(
"INSERT INTO jobs (id, name, description, last_due_at, last_started_at, last_finished_at, last_outcome, created_at, updated_at) VALUES (@id, 'motore', 'Motore', @t, @t, @t, 'completed', @t, @t)",
_none, ("id", Ids.NewId(rig.Clock)), ("t", Off)), _none).ConfigureAwait(false);
rig.Clock.Set(back);
await new AbsenceBook(rig.Db, rig.Clock).RecordAsync(_none).ConfigureAwait(false);
var counts = new Counts();
var bus = new EventBus();
using var runs = bus.Subscribe<JobRun>();
var scheduler = new Scheduler(rig.Clock, bus);
scheduler.Add(new ScheduledJob("backup", "Salvataggio del database", Schedule.Daily(new TimeOnly(3, 30), Zones.Rome), _ =>
{
counts.Backups++;
return Task.CompletedTask;
})
{ MaxLateness = TimeSpan.FromHours(2), CatchUpWithin = TimeSpan.FromDays(3650) }, Off);
scheduler.Add(new ScheduledJob("riepilogo", "Riepilogo del mattino", Schedule.Daily(new TimeOnly(8, 0), Zones.Rome), _ =>
{
counts.Summaries++;
return Task.CompletedTask;
})
{ CatchUpWithin = TimeSpan.FromHours(4) }, Off);
scheduler.Add(new ScheduledJob("pulizia", "Pulizia", Schedule.Daily(new TimeOnly(1, 0), Zones.Rome), _ =>
{
counts.Plain++;
return Task.CompletedTask;
}), Off);
await scheduler.RunDueAsync(_none).ConfigureAwait(false);
await scheduler.RunDueAsync(_none).ConfigureAwait(false);
while (runs.TryRead(out var run))
{
if (run!.Missed && run.Outcome == JobOutcome.Skipped)
{
counts.Skipped.Add(run);
}
else if (run.Missed)
{
Assert.AreEqual(JobOutcome.Completed, run.Outcome);
StringAssert.StartsWith(AbsenceBook.MissedLine(run, Zones.Rome), "Recuperato un lavoro saltato: salvataggio del database (previsto alle 03:30)");
}
}
return counts;
}
private static async Task<List<string>> DiaryAsync(TradingRig rig) =>
[.. await rig.QueryAsync("SELECT message FROM journal ORDER BY occurred_at, id", r => r.Text("message")).ConfigureAwait(false)];
private static async Task<string?> LastDiaryAsync(TradingRig rig, string code) =>
(await rig.QueryAsync("SELECT message FROM journal WHERE code = @c ORDER BY occurred_at DESC, id DESC", r => r.Text("message"), ("c", code)).ConfigureAwait(false)) is { Count: > 0 } rows ? rows[0] : null;
}