Fase 7: regole di 10.5 e stato per strumento (#59) #104

Merged
Alby96 merged 1 commits from fase-7 into main 2026-11-17 14:36:44 +01:00
21 changed files with 1170 additions and 30 deletions

No files matched your search

+10 -5
View File
@@ -1,6 +1,6 @@
# Stato del progetto
Aggiornato: 2026-11-15
Aggiornato: 2026-11-16
## Fase corrente
@@ -95,6 +95,12 @@ Aggiornato: 2026-11-15
- ogni storia si collega a strumenti, classi o mercato: simboli, ticker, nomi delle società e delle monete, temi.
- Primo giudizio locale (#57): casi gravi trovati con parole chiave in inglese e in italiano, tono in cinque gradi, importanza da 1 a 4. Ogni articolo e ogni storia lo conservano.
- Classificatore ONNX (#57, ADR 0014 punto 8): il modello Xenova quantizzato, con revisione e hash fissati, scaricato dal bot nella cartella `models` al primo giro delle notizie. Il tokenizzatore sta nella DLL e dà gli stessi identificativi dell'originale. 23–24 ms per titolo su un thread. Senza il modello giudicano le sole parole chiave.
- Stato per strumento e regole di 10.5 (#59, ADR 0014 punti 9–12):
- eventi gravi, notizie negative, trimestrali e dati macro fermano o spostano i nuovi acquisti, contando dal momento del fatto;
- una voce non confermata dà solo una riga nel diario e un nuovo controllo dopo 2 ore;
- ogni restrizione compare nel diario con il suo motivo, e ogni riga si spegne in Impostazioni;
- dopo un'assenza valgono solo le restrizioni ancora in corso;
- lo stato di ogni strumento tiene il tono a 1 e 3 giorni, il numero di storie rispetto alla norma e la restrizione in corso.
## In corso
@@ -103,10 +109,9 @@ Nulla.
## Prossimo passo
1. L'utente scarica lo zip della release `v0.7.0` da Gitea (Release), lo fa girare e poi incolla il rapporto: doppio clic su `Prepara i dati da incollare.cmd`, oppure il pulsante in Diario e sistema. Il rapporto si analizza e si salva in `docs/diagnostica/`; da lì si chiude #55.
2. Intanto la Fase 7 va avanti sul branch `fase-7`. Raccolta (#56) e primo giudizio con il classificatore (#57) sono fatti. Restano:
- l'analista facoltativo (#58);
- lo stato per strumento (#59);
- la schermata (#60).
2. Intanto la Fase 7 va avanti sul branch `fase-7`. Raccolta (#56), primo giudizio con il classificatore (#57) e regole di 10.5 (#59) sono fatti. Restano:
- la schermata Notizie con gli interruttori delle regole (#60);
- l'analista facoltativo (#58).
3. Con le chiavi del conto paper di Alpaca si possono anche fare i backtest azionari (`anubis backtest base`) e chiudere i criteri delle Fasi 2 e 3 in attesa.
## Da sapere
+30 -2
View File
@@ -1,6 +1,6 @@
# 0014 — Scelte della Fase 7: raccolta e collegamento delle notizie
Data: 2026-11-14 · Stato: accettata · Precisa SPEC 4.4, 4.5 e 10.2 (punti 1–4) per le issue #56 e #57. Usa solo i componenti già ammessi dall'ADR 0004
Data: 2026-11-14 · Stato: accettata · Precisa SPEC 4.4, 4.5, 10.2, 10.5 e 5.6 per le issue #56, #57 e #59. Usa solo i componenti già ammessi dall'ADR 0004
## Contesto
@@ -80,8 +80,36 @@ Inoltre:
- Il test sul modello vero gira dove il file è già scaricato (`ANUBIS_TEST_MODEL_DIR`). In CI risulta ignorato: i test non scaricano 83 MB a ogni esecuzione.
- Lo zip per Windows porta in più la libreria nativa di ONNX Runtime per win-x64 (`onnxruntime.dll`, 17 MB) e passa da 3,4 a 10,7 MB. Per questo `Anubis` e `Anubis.Server` dichiarano `win-x64` tra i loro runtime: il file di blocco contiene anche i pacchetti nativi di Windows, e `tasks release-win` non lo modifica più. Che l'immagine Docker carichi la libreria per linux-x64 (SPEC 3, distribuzione Docker) non è ancora provato: si controlla con il primo avvio del classificatore in Docker.
**9. Le regole di 10.5 (#59).**
- Una restrizione vale dal momento del fatto, cioè dalla prima ora tra pubblicazione e arrivo nel bot, non da quando il bot la legge. Così, dopo un'assenza, valgono solo le restrizioni ancora in corso (SPEC 5.6), e una notizia di tre giorni fa non ferma niente oggi.
- Le righe della tabella, come le applica il bot:
- **evento grave**: una storia confermata con importanza 4 (un caso grave di 10.2), qualunque sia il tono. Per 72 ore niente nuovi acquisti dello strumento, lo stop non si allarga e parte un avviso Telegram;
- **notizia negativa**: una storia confermata con importanza 3 e tono non migliore di −0,2. Per 24 ore niente nuovi acquisti;
- **voce non confermata**: una sola fonte non affidabile. Nessuna azione: una riga nel diario e un nuovo controllo dopo 2 ore, che dice se un'altra fonte l'ha confermata;
- **trimestrale in arrivo**: `equity_meanrev` non entra se i conti cadono nelle sessioni della sua finestra (la regola della strategia di 8.3, che ora riceve il calendario anche dal vivo). L'uscita prima dei conti resta sempre, anche con la riga spenta;
- **dato macro**: da 30 minuti prima a 30 dopo, 60 dopo Fed e BCE, come nel backtest. Ora vale anche dal vivo.
- "Confermata" vuol dire una fonte affidabile o almeno due fonti indipendenti (punto 3).
- Quando più righe valgono insieme, vince quella che finisce più tardi; a parità, l'evento grave.
- Ogni riga si spegne in Impostazioni con la sua chiave (`news.rule.severe`, `news.rule.negative`, `news.rule.unconfirmed`, `calendar.rule.earnings`, `calendar.rule.macro`). Sono tutte accese all'inizio; la schermata arriva con #60.
- Queste regole restringono e non bloccano: non chiudono posizioni e non fermano la strategia, tolgono o spostano solo gli acquisti.
**10. Gli acquisti fermati.**
- Un acquisto che cade in una restrizione si sposta alla fine della restrizione, se a quell'ora l'ordine è ancora valido. Altrimenti si toglie, con il motivo.
- Lo spostamento tiene l'ora originale in `first_due_at`. Il controllo del prezzo di SPEC 5.6 confronta sempre con la decisione: se nel frattempo il prezzo si è mosso troppo, l'ordine scade come ogni ordine in ritardo.
- La decisione della strategia resta scritta tra le decisioni con la sua proposta, e accanto c'è la riga del correttivo che l'ha fermata. Gli esempi di 11.2 la trovano lì.
- Ogni restrizione compare nel diario una sola volta, con il suo motivo: il titolo della notizia e la fonte, oppure il nome del dato macro. Le righe del diario seguono l'appendice F.10.
**11. Lo stato per strumento (10.2, punto 6).**
- Tono medio a 1 e a 3 giorni: le storie pesano di meno con l'età, con un tempo di dimezzamento di metà finestra (12 ore e 36 ore).
- Numero di storie delle ultime 24 ore e la norma: le storie al giorno dei 29 giorni precedenti, solo quando ci sono almeno 2 giorni di storico. Prima la norma non c'è e lo stato non dice "più notizie del solito".
- La restrizione più forte in corso, con il suo motivo.
- Il diario parla delle notizie sugli strumenti che il bot tiene e su quelli degli universi più recenti. Gli altri si leggono nella schermata Notizie.
**12. Le etichette d'umore (10.4)** sono quelle della Fase 2 (ADR 0008) e non cambiano.
## Conseguenze
- Le notizie si accumulano già nel database con il loro primo giudizio. Analista, stato per strumento e schermata arrivano con le issue #58–#60.
- Le notizie si accumulano già nel database con il loro primo giudizio e, dal #59, fermano o spostano gli acquisti secondo 10.5. Analista e schermata arrivano con #58 e #60.
- Una parola chiave che sbaglia può fermare gli acquisti di uno strumento per 72 ore. È la direzione prudente: un acquisto in meno, mai uno in più. Se l'uso reale mostra troppe restrizioni inutili, la riga si spegne in Impostazioni e le parole si correggono con un test.
- Le soglie di somiglianza e di età sono costanti nel codice. Se l'uso reale mostra storie unite o divise male, si cambiano con un nuovo ADR e un test che lo mostra.
- I test leggono un archivio sintetico nel formato reale di ogni fonte (`tests/Anubis.Tests/Fixtures/news/`). Non chiamano mai internet.
+8
View File
@@ -23,6 +23,12 @@ public sealed record BacktestOptions
/// <summary>The risk engine of SPEC 9. Until Phase 5 it lets everything through.</summary>
public IIntentStage Risk { get; init; } = PassThroughStage.Risk;
/// <summary>The earnings calendar, live only (SPEC 8.3): a backtest has none and leaves it null.</summary>
public Func<string, DateOnly?>? NextEarnings { get; init; }
/// <summary>Whether coming earnings keep entries out (SPEC 10.5, fourth row).</summary>
public bool EarningsEntries { get; init; } = true;
/// <summary>
/// The strategy's share of the bot's total capital (SPEC 8.6, 9.2): the reference equity is the strategy's equity
/// divided by it. Null means the strategy is the whole bot.
@@ -414,6 +420,8 @@ public static class BacktestEngine
var context = new StrategyContext(day, at, universe, new PortfolioView(_cash, equity, holdings), Bars, s => _data[s]?.Spec)
{
NextSession = _data.NextSession(day),
NextEarnings = _options.NextEarnings,
EarningsEntries = _options.EarningsEntries,
ReferenceEquity = _options.ReferenceShare is > 0m and var share ? equity / share : equity,
PeriodsPerYear = _data.PeriodsPerYear,
History = _history,
+3
View File
@@ -149,6 +149,9 @@ public static class Format
return text;
}
/// <summary>A day in words, for the diary: <c>martedì 13 ottobre</c>.</summary>
public static string Day(DateOnly day) => string.Create(CultureInfo.InvariantCulture, $"{_daysLong[(int)day.DayOfWeek]} {day.Day} {_monthsLong[day.Month - 1]}");
/// <summary>Full date for the top bar: <c>Venerdì 9 ottobre 2026, ore 08:21</c>.</summary>
public static string LongDateTime(DateTimeOffset moment, TimeZoneInfo zone)
{
+1 -1
View File
@@ -45,7 +45,7 @@ public sealed class SettingsStore(Database db, IClock clock, EventBus bus, IRead
public static readonly SettingDefinition TelegramStatus = new("telegram.status", "Stato periodico su Telegram", "never", ["never", "hourly", "daily"]);
public static readonly IReadOnlyList<SettingDefinition> All =
[Theme, Currency, BotState, CentralBankMeetings, TelegramStatus, .. Risk.RiskSettings.All, Risk.Scenarios.Setting];
[Theme, Currency, BotState, CentralBankMeetings, TelegramStatus, .. Risk.RiskSettings.All, Risk.Scenarios.Setting, .. News.NewsRuleSettings.All];
private readonly IReadOnlyDictionary<string, StartupOverride> _overrides = overrides ?? new Dictionary<string, StartupOverride>();
+5 -1
View File
@@ -82,7 +82,11 @@ public sealed partial class MarketDataService : IDisposable
_universes = new UniverseStore(db, clock);
_builder = new UniverseBuilder(db, clock);
_states = new SourceStateStore(db, clock);
News = new News.NewsCollector(_http, db, clock, _logger) { ModelFolder = data is null ? null : Path.Combine(data.Root, "models") };
News = new News.NewsCollector(_http, db, clock, _logger)
{
ModelFolder = data is null ? null : Path.Combine(data.Root, "models"),
Watcher = new News.NewsWatcher(db, clock, settings),
};
}
public OkxClient Okx { get; }
@@ -0,0 +1,17 @@
-- The rules of SPEC 10.5 (issue #59).
-- An entry held back by a restriction is tried again when it ends: due_at moves, first_due_at keeps the time it was
-- first due, so the price check of SPEC 5.6 still compares with the price of the decision.
ALTER TABLE planned_orders ADD COLUMN first_due_at {timestamp};
-- What the diary has already said about a story or an event on one instrument: a restriction, an unconfirmed story,
-- its check two hours later, coming earnings. subject is 'story:<id>' or 'event:<source_key>', target 'venue:symbol';
-- each line is written once.
CREATE TABLE news_notes (
id {uuid} PRIMARY KEY,
subject TEXT NOT NULL,
target TEXT NOT NULL,
kind TEXT NOT NULL,
created_at {timestamp} NOT NULL,
UNIQUE (subject, target, kind)
){strict};
+8
View File
@@ -117,8 +117,16 @@ public sealed partial class NewsCollector(HttpClient http, Database db, IClock c
first = false;
await ReadAsync(source, dictionary, cancellationToken).ConfigureAwait(false);
}
if (Watcher is not null)
{
await Watcher.RunOnceAsync(cancellationToken).ConfigureAwait(false);
}
}
/// <summary>What the diary says of the news on the instruments held or watched (SPEC 10.5), after each round.</summary>
public NewsWatcher? Watcher { get; init; }
/// <summary>Whether a source is due: always while it answers, later and later while it fails.</summary>
public static bool Due(NewsSource source, DateTimeOffset now)
{
+266
View File
@@ -0,0 +1,266 @@
using Anubis.Core;
using Anubis.Data;
using Anubis.Db;
using Anubis.Strategies;
namespace Anubis.News;
/// <summary>
/// The news of one instrument as the rules of SPEC 10.5 read them: its stories, its state (10.2 point 6), the
/// restriction in force. Only links to the instrument itself count: a story on "crypto" or on the market restricts
/// no single coin or stock.
/// </summary>
public sealed class NewsDesk(Database db, IClock clock, SettingsStore? settings = null)
{
/// <summary>The name of an instrument in news_links: <c>venue:symbol</c>.</summary>
public static string Target(InstrumentSpec spec)
{
ArgumentNullException.ThrowIfNull(spec);
return spec.Venue + ":" + spec.Symbol;
}
public Task<NewsRuleSwitches> SwitchesAsync(CancellationToken cancellationToken) => NewsRuleSettings.LoadAsync(settings, cancellationToken);
/// <summary>The stories linked to <paramref name="target"/> seen since <paramref name="since"/>, oldest first.</summary>
public async Task<IReadOnlyList<StoryFact>> StoriesAsync(string target, DateTimeOffset since, CancellationToken cancellationToken) =>
await db.ReadAsync(s => s.QueryAsync(
"SELECT st.id, st.title, st.published_at, st.first_seen_at, st.sources, st.trusted, st.score, st.severe, st.importance, " +
"(SELECT i.publisher FROM news_items i WHERE i.story_id = st.id ORDER BY i.created_at, i.id LIMIT 1) AS publisher " +
"FROM news_stories st JOIN news_links l ON l.story_id = st.id WHERE l.scope = @scope AND l.target = @t AND st.last_seen_at >= @since " +
"ORDER BY st.published_at, st.id",
ReadStory, ("scope", LinkScopes.Instrument), ("t", target), ("since", since)), cancellationToken).ConfigureAwait(false);
/// <summary>The state of 10.2 point 6 of one instrument now.</summary>
public async Task<InstrumentNewsState> StateAsync(string target, CancellationToken cancellationToken)
{
var now = clock.UtcNow;
var stories = await StoriesAsync(target, now - NewsRules.NormWindow, cancellationToken).ConfigureAwait(false);
var first = await db.ReadAsync(s => s.FirstOrDefaultAsync("SELECT MIN(first_seen_at) AS first FROM news_stories", r => r.MomentOrNull("first")), cancellationToken)
.ConfigureAwait(false);
return NewsRules.State([.. stories], now, await SwitchesAsync(cancellationToken).ConfigureAwait(false), first);
}
/// <summary>The restrictions in force on an instrument at <paramref name="at"/>, with the rows the user left on.</summary>
public async Task<IReadOnlyList<NewsRestriction>> RestrictionsAsync(string target, DateTimeOffset at, CancellationToken cancellationToken)
{
var stories = await StoriesAsync(target, at - NewsRules.Longest, cancellationToken).ConfigureAwait(false);
return NewsRules.Active(stories, at, await SwitchesAsync(cancellationToken).ConfigureAwait(false));
}
/// <summary>
/// The next earnings date of each stock from <paramref name="from"/> on, as the calendar of 4.4 has it: the day in
/// New York. <c>equity_meanrev</c> reads it (10.5, fourth row; 8.3 for its exit).
/// </summary>
public async Task<IReadOnlyDictionary<string, DateOnly>> EarningsAsync(DateOnly from, CancellationToken cancellationToken)
{
var start = Zones.At(from, TimeOnly.MinValue, Zones.NewYork);
var events = await new CalendarEventStore(db, clock).BetweenAsync(start, start.AddDays(31), cancellationToken).ConfigureAwait(false);
var next = new Dictionary<string, DateOnly>(StringComparer.Ordinal);
foreach (var e in events.Where(e => e.Kind == CalendarEventKinds.Earnings && e.Symbol is { Length: > 0 }).OrderBy(e => e.OccursAt))
{
next.TryAdd(e.Symbol!, DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(e.OccursAt, Zones.NewYork).DateTime));
}
return next;
}
private static StoryFact ReadStory(DbRow r) => new(r.Id("id"), r.Text("title"), r.TextOrNull("publisher") ?? "", r.Moment("published_at"), r.Moment("first_seen_at"),
(int)r.Number("sources"), r.Bool("trusted"), r.IsNull("score") ? null : r.Measure("score"), r.TextOrNull("severe"), (int)r.Number("importance"));
}
/// <summary>Why an entry cannot go now, and until when (SPEC 10.5). The motive is what the diary says after "Motivo:".</summary>
public sealed record EntryHold(string Code, DateTimeOffset Until, string Motive);
/// <summary>The check the live engine makes before a purchase goes out, and before it lowers a stop.</summary>
public interface IEntryGate
{
/// <summary>The restriction that stops a purchase of the instrument at <paramref name="at"/>, or null.</summary>
Task<EntryHold?> HoldAsync(InstrumentSpec spec, DateTimeOffset at, CancellationToken cancellationToken);
/// <summary>The serious event that keeps the stop of a position where it is, or null.</summary>
Task<EntryHold?> KeepStopAsync(InstrumentSpec spec, DateTimeOffset at, CancellationToken cancellationToken);
}
/// <summary>
/// The rows of 10.5 that act on a single purchase: the news on the instrument (first two rows) and the high-impact
/// figures (fifth row), the strictest of them. The earnings row acts inside <c>equity_meanrev</c>, which knows its
/// sessions; the third row never stops anything.
/// </summary>
public sealed class EntryGate(Database db, IClock clock, SettingsStore? settings = null) : IEntryGate
{
private readonly NewsDesk _desk = new(db, clock, settings);
private readonly CalendarEventStore _calendar = new(db, clock);
public async Task<EntryHold?> HoldAsync(InstrumentSpec spec, DateTimeOffset at, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(spec);
var switches = await _desk.SwitchesAsync(cancellationToken).ConfigureAwait(false);
var holds = new List<EntryHold>();
if (NewsRules.Strictest(await _desk.RestrictionsAsync(NewsDesk.Target(spec), at, cancellationToken).ConfigureAwait(false)) is { } news)
{
holds.Add(new EntryHold(news.Code, news.Until, Motive(news.Story)));
}
if (switches.Macro)
{
var events = await _calendar.BetweenAsync(at - TimeSpan.FromHours(3), at + TimeSpan.FromHours(3), cancellationToken).ConfigureAwait(false);
if (CalendarBlocks.BlockedUntil(at, events) is { } until)
{
var named = events.Where(e => CalendarBlocks.Window(e) is { } w && w.From <= at && w.To > at).OrderByDescending(e => CalendarBlocks.Window(e)!.Value.To).First();
holds.Add(new EntryHold(CorrectiveReasons.CalendarEvent, until, named.Title));
}
}
return holds.OrderByDescending(h => h.Until).FirstOrDefault();
}
public async Task<EntryHold?> KeepStopAsync(InstrumentSpec spec, DateTimeOffset at, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(spec);
return NewsRules.KeepsStop(await _desk.RestrictionsAsync(NewsDesk.Target(spec), at, cancellationToken).ConfigureAwait(false)) is { } severe
? new EntryHold(NewsReasons.KeepStop, severe.Until, Motive(severe.Story))
: null;
}
/// <summary>The story as the diary names it: its title in quotes and who published it.</summary>
public static string Motive(StoryFact story)
{
ArgumentNullException.ThrowIfNull(story);
var title = story.Title.Length > 160 ? story.Title[..157] + "…" : story.Title;
return story.Publisher.Length > 0 ? $"«{title}» ({story.Publisher})" : $"«{title}»";
}
}
/// <summary>
/// What the diary says of the news on the instruments the bot holds or may buy (SPEC 10.5, appendix F.10): a
/// restriction when it starts, with its motive; a story from one untrusted source, and its check two hours later; the
/// earnings coming for a stock. A serious event also goes to Telegram. Each line is written once (news_notes).
/// </summary>
public sealed class NewsWatcher(Database db, IClock clock, SettingsStore? settings = null)
{
private readonly NewsDesk _desk = new(db, clock, settings);
public TimeZoneInfo Zone { get; init; } = Zones.Rome;
/// <summary>One round over the instruments watched now: those held by any strategy and those of the latest universes.</summary>
public async Task<int> RunOnceAsync(CancellationToken cancellationToken)
{
var now = clock.UtcNow;
var switches = await _desk.SwitchesAsync(cancellationToken).ConfigureAwait(false);
var watched = await WatchedAsync(cancellationToken).ConfigureAwait(false);
var written = 0;
foreach (var (target, symbol, held) in watched)
{
var stories = await _desk.StoriesAsync(target, now - NewsRules.Longest, cancellationToken).ConfigureAwait(false);
foreach (var restriction in NewsRules.Active(stories, now, switches))
{
var text = Restricted(restriction, symbol, held);
written += await NoteAsync("story:" + restriction.Story.Id, target, restriction.Code, text, restriction.Code == NewsReasons.Severe, cancellationToken)
.ConfigureAwait(false);
}
if (!switches.Unconfirmed)
{
continue;
}
foreach (var story in stories.Where(NewsRules.Unconfirmed))
{
var subject = "story:" + story.Id;
if (now - story.FirstSeenAt < NewsRules.RecheckAfter)
{
written += await NoteAsync(subject, target, NewsReasons.Unconfirmed,
$"Voce non confermata su {Trading.DiaryText.Shown(symbol)}: {EntryGate.Motive(story)}. Nessuna azione: ricontrollo tra 2 ore.", false, cancellationToken)
.ConfigureAwait(false);
}
else if (now - story.FirstSeenAt < NewsRules.Longest)
{
written += await NoteAsync(subject, target, NewsReasons.Rechecked,
$"Ricontrollata la voce su {Trading.DiaryText.Shown(symbol)}: nessun'altra fonte la conferma, nessuna azione.", false, cancellationToken).ConfigureAwait(false);
}
}
}
if (switches.Earnings)
{
written += await EarningsAsync(watched, now, cancellationToken).ConfigureAwait(false);
}
return written;
}
private static string Restricted(NewsRestriction r, string symbol, bool held)
{
var shown = Trading.DiaryText.Shown(symbol);
if (r.Code == NewsReasons.Severe)
{
var stop = held ? " Lo stop resta dov'è." : "";
return $"Ho messo una restrizione per 72 ore: niente nuovi acquisti di {shown}.{stop} Motivo: {EntryGate.Motive(r.Story)}.";
}
var what = held ? $"non aumento la posizione su {shown}" : $"non compro {shown}";
return $"Ho cambiato una regola per 24 ore: {what}. Motivo: {EntryGate.Motive(r.Story)}.";
}
// Earnings of a watched stock in the next five days (three sessions and a weekend): said once, with the day.
private async Task<int> EarningsAsync(IReadOnlyList<(string Target, string Symbol, bool Held)> watched, DateTimeOffset now, CancellationToken cancellationToken)
{
var stocks = watched.Where(w => w.Target.StartsWith(Venues.Alpaca + ":", StringComparison.Ordinal)).ToDictionary(w => w.Symbol, w => w.Target, StringComparer.Ordinal);
if (stocks.Count == 0)
{
return 0;
}
var written = 0;
var events = await new CalendarEventStore(db, clock).BetweenAsync(now, now.AddDays(5), cancellationToken).ConfigureAwait(false);
foreach (var e in events.Where(e => e.Kind == CalendarEventKinds.Earnings && e.Symbol is { } s && stocks.ContainsKey(s)))
{
var dayText = Format.Day(DateOnly.FromDateTime(TimeZoneInfo.ConvertTime(e.OccursAt, Zones.NewYork).DateTime));
var (when, until) = e.TimeHint switch
{
"bmo" => (" prima dell'apertura", $"fino a {dayText}"),
"dmh" => (" durante la seduta", $"fino a {dayText}"),
"amc" => (" dopo la chiusura", "fino alla seduta dopo i conti"),
_ => ("", "fino alla seduta dopo i conti"),
};
var text = $"Ho messo una restrizione {until}: niente mean reversion su {e.Symbol}. Motivo: {e.Symbol} pubblica i conti {dayText}{when}.";
written += await NoteAsync("event:" + e.SourceKey, stocks[e.Symbol!], NewsReasons.Earnings, text, false, cancellationToken).ConfigureAwait(false);
}
return written;
}
private Task<int> NoteAsync(string subject, string target, string kind, string text, bool telegram, CancellationToken cancellationToken) =>
db.WriteAsync(async s =>
{
var seen = await s.FirstOrDefaultAsync("SELECT id FROM news_notes WHERE subject = @s AND target = @t AND kind = @k", r => r.IdOrNull("id"),
("s", subject), ("t", target), ("k", kind)).ConfigureAwait(false);
if (seen is not null)
{
return 0;
}
await s.ExecuteAsync("INSERT INTO news_notes (id, subject, target, kind, created_at) VALUES (@id, @s, @t, @k, @now)", cancellationToken,
("id", Ids.NewId(clock)), ("s", subject), ("t", target), ("k", kind), ("now", clock.UtcNow)).ConfigureAwait(false);
await Trading.Diary.WriteAsync(s, clock, Trading.DiaryKinds.News, kind, text, new { subject, target }, cancellationToken).ConfigureAwait(false);
if (telegram)
{
await Notifications.NotificationSender.EnqueueAsync(s, clock, kind, text, cancellationToken).ConfigureAwait(false);
}
return 1;
}, cancellationToken);
private async Task<IReadOnlyList<(string Target, string Symbol, bool Held)>> WatchedAsync(CancellationToken cancellationToken)
{
var held = await db.ReadAsync(s => s.QueryAsync("SELECT i.venue, i.symbol, p.quantity FROM positions p JOIN instruments i ON i.id = p.instrument_id",
r => (Venue: r.Text("venue"), Symbol: r.Text("symbol"), Quantity: r.Money("quantity"))), cancellationToken).ConfigureAwait(false);
var members = await db.ReadAsync(s => s.QueryAsync(
"SELECT i.venue, i.symbol FROM universe_members u JOIN instruments i ON i.id = u.instrument_id " +
"WHERE u.as_of = (SELECT MAX(x.as_of) FROM universe_members x WHERE x.universe = u.universe)",
r => (Venue: r.Text("venue"), Symbol: r.Text("symbol"))), cancellationToken).ConfigureAwait(false);
var holding = held.Where(h => h.Quantity > 0m).Select(h => h.Venue + ":" + h.Symbol).ToHashSet(StringComparer.Ordinal);
return [.. held.Where(h => h.Quantity > 0m).Select(h => (h.Venue, h.Symbol)).Concat(members).Distinct()
.Select(m => (m.Venue + ":" + m.Symbol, m.Symbol, holding.Contains(m.Venue + ":" + m.Symbol))).OrderBy(m => m.Item1, StringComparer.Ordinal)];
}
}
+211
View File
@@ -0,0 +1,211 @@
using Anubis.Core;
namespace Anubis.News;
/// <summary>Reason codes of the rules of SPEC 10.5: the decisions table and the diary use them (ReasonCatalog).</summary>
public static class NewsReasons
{
/// <summary>First row: a serious negative event, confirmed. No new entries for 72 hours, the stop is not widened.</summary>
public const string Severe = "news_severe";
/// <summary>Second row: a negative story of importance 3, confirmed. No entries nor increases for 24 hours.</summary>
public const string Negative = "news_negative";
/// <summary>Third row: one untrusted source only. Nothing happens; it is checked again after 2 hours.</summary>
public const string Unconfirmed = "news_unconfirmed";
/// <summary>The check two hours later of a story that was not confirmed.</summary>
public const string Rechecked = "news_rechecked";
/// <summary>A position under a serious event: the strategy wanted a lower stop and it stayed where it was.</summary>
public const string KeepStop = "news_keep_stop";
/// <summary>Fourth row: earnings coming, no entries of <c>equity_meanrev</c>.</summary>
public const string Earnings = "calendar_earnings";
}
/// <summary>What the rules know of a story linked to an instrument (the tables of 0008).</summary>
public sealed record StoryFact(Guid Id, string Title, string Publisher, DateTimeOffset PublishedAt, DateTimeOffset FirstSeenAt, int Sources, bool Trusted, double? Score,
string? Severe, int Importance)
{
/// <summary>A trusted source, or two independent ones (10.5).</summary>
public bool Confirmed => Trusted || Sources >= 2;
/// <summary>
/// When the effect starts: the publication, or the moment the bot read it when a feed dates it in the future. The
/// windows of 10.5 are counted from the fact, so after an absence an effect already over is not applied (5.6).
/// </summary>
public DateTimeOffset Start => PublishedAt < FirstSeenAt ? PublishedAt : FirstSeenAt;
/// <summary>A serious case (importance 4 or 5): the keywords of 10.2 name only negative facts.</summary>
public bool IsSevere => Importance >= 4;
/// <summary>A negative story of importance 3. The slightly negative ones stay below 3 and create no restriction.</summary>
public bool IsNegative => Importance == 3 && Score is not > -0.2;
/// <summary>Whether the story is one that can restrict: serious, or negative of importance 3.</summary>
public bool Matters => IsSevere || IsNegative;
}
/// <summary>An effect of 10.5 on one instrument: what it forbids, from when, until when, and the story behind it.</summary>
public sealed record NewsRestriction(string Code, DateTimeOffset From, DateTimeOffset Until, StoryFact Story)
{
/// <summary>How strict it is when two last as long: the serious event wins, it also keeps the stop.</summary>
public int Rank => Code == NewsReasons.Severe ? 2 : 1;
}
/// <summary>The rows of 10.5 the user left on (Impostazioni → Notizie e calendario). All on by default.</summary>
public sealed record NewsRuleSwitches(bool Severe = true, bool Negative = true, bool Unconfirmed = true, bool Earnings = true, bool Macro = true)
{
public static NewsRuleSwitches AllOn { get; } = new();
}
/// <summary>The state of one instrument after its news (10.2 point 6): the clues of 11.2 and the Notizie screen read it.</summary>
public sealed record InstrumentNewsState(double? Tone1Day, double? Tone3Days, int Stories1Day, double? NormalPerDay, NewsRestriction? Active)
{
/// <summary>The news of the last day compared with the usual: 1 is a normal day, 3 three times as many.</summary>
public double? Busy => NormalPerDay is > 0 and var normal ? Stories1Day / normal : null;
}
/// <summary>
/// The rules of SPEC 10.5 and the state of 10.2 point 6, as pure functions of the stories and the time: the same
/// stories at the same moment always give the same answer. The rules only take away: no row ever buys or sells.
/// </summary>
public static class NewsRules
{
public static TimeSpan SevereWindow { get; } = TimeSpan.FromHours(72);
public static TimeSpan NegativeWindow { get; } = TimeSpan.FromHours(24);
/// <summary>A story from one untrusted source is checked again this long after the bot first read it.</summary>
public static TimeSpan RecheckAfter { get; } = TimeSpan.FromHours(2);
/// <summary>The longest window: older stories cannot restrict anything.</summary>
public static TimeSpan Longest => SevereWindow;
/// <summary>How long the norm of the number of stories is measured on.</summary>
public static TimeSpan NormWindow { get; } = TimeSpan.FromDays(30);
/// <summary>The restrictions in force at <paramref name="now"/> on one instrument, from its stories.</summary>
public static IReadOnlyList<NewsRestriction> Active(IEnumerable<StoryFact> stories, DateTimeOffset now, NewsRuleSwitches switches)
{
ArgumentNullException.ThrowIfNull(stories);
ArgumentNullException.ThrowIfNull(switches);
var active = new List<NewsRestriction>();
foreach (var story in stories)
{
// An unconfirmed story does nothing (third row), whatever it says; once confirmed the start is still the fact.
if (!story.Matters || !story.Confirmed || story.FirstSeenAt > now)
{
continue;
}
var (on, code, window) = story.IsSevere ? (switches.Severe, NewsReasons.Severe, SevereWindow) : (switches.Negative, NewsReasons.Negative, NegativeWindow);
var until = story.Start + window;
if (on && story.Start <= now && now < until)
{
active.Add(new NewsRestriction(code, story.Start, until, story));
}
}
return active;
}
/// <summary>
/// The strictest of the restrictions (10.5: "vale la più restrittiva"): the one that lasts longest, the serious
/// event when two end together. Null when nothing is in force.
/// </summary>
public static NewsRestriction? Strictest(IEnumerable<NewsRestriction> restrictions)
{
ArgumentNullException.ThrowIfNull(restrictions);
return restrictions.OrderByDescending(r => r.Until).ThenByDescending(r => r.Rank).ThenByDescending(r => r.Story.Importance).FirstOrDefault();
}
/// <summary>Whether a serious event is in force: then the stop of a position is not widened for any reason.</summary>
public static NewsRestriction? KeepsStop(IEnumerable<NewsRestriction> restrictions)
{
ArgumentNullException.ThrowIfNull(restrictions);
return restrictions.Where(r => r.Code == NewsReasons.Severe).OrderByDescending(r => r.Until).FirstOrDefault();
}
/// <summary>Stories that would restrict if confirmed, read from one untrusted source only: the third row.</summary>
public static bool Unconfirmed(StoryFact story)
{
ArgumentNullException.ThrowIfNull(story);
return story.Matters && !story.Confirmed;
}
/// <summary>
/// The state of 10.2 point 6 at <paramref name="now"/>. The tone is the mean score of the stories, each weighed
/// by its age with a half-life of half the window (12 hours on one day, 36 on three), so the latest count most and
/// a story leaves the window gently. Stories without a score (Italian, no classifier) are not in the tone. The
/// norm is the stories per day of the 29 days before the last one; null until the first of those days has passed.
/// </summary>
public static InstrumentNewsState State(IReadOnlyCollection<StoryFact> stories, DateTimeOffset now, NewsRuleSwitches switches, DateTimeOffset? firstStoryEver = null)
{
ArgumentNullException.ThrowIfNull(stories);
ArgumentNullException.ThrowIfNull(switches);
var known = stories.Where(s => s.FirstSeenAt <= now).ToList();
var day = TimeSpan.FromDays(1);
var oneDay = known.Count(s => s.Start > now - day && s.Start <= now);
var normFrom = now - NormWindow;
double? normal = null;
if (firstStoryEver is { } first && first <= now - (2 * day))
{
var from = first > normFrom ? first : normFrom;
var days = (now - day - from).TotalDays;
normal = days >= 1 ? known.Count(s => s.Start > from && s.Start <= now - day) / days : null;
}
return new InstrumentNewsState(Tone(known, now, day), Tone(known, now, 3 * day), oneDay, normal, Strictest(Active(known, now, switches)));
}
private static double? Tone(IEnumerable<StoryFact> stories, DateTimeOffset now, TimeSpan window)
{
var halfLife = window.TotalHours / 2;
double sum = 0, weights = 0;
foreach (var story in stories)
{
var age = (now - story.Start).TotalHours;
if (story.Score is not { } score || age < 0 || age >= window.TotalHours)
{
continue;
}
var weight = Math.Pow(0.5, age / halfLife);
sum += weight * score;
weights += weight;
}
return weights > 0 ? Math.Round(sum / weights, 4) : null;
}
}
/// <summary>The switches of the rows of 10.5 (SPEC 10.5: "ogni riga della tabella si può spegnere dalle impostazioni").</summary>
public static class NewsRuleSettings
{
public const string On = "on";
public const string Off = "off";
public static readonly SettingDefinition Severe = Switch("news.rule.severe", "Evento grave: niente nuovi acquisti per 72 ore");
public static readonly SettingDefinition Negative = Switch("news.rule.negative", "Notizia negativa: niente acquisti né aumenti per 24 ore");
public static readonly SettingDefinition Unconfirmed = Switch("news.rule.unconfirmed", "Voce da una sola fonte: annotata e ricontrollata dopo 2 ore");
public static readonly SettingDefinition Earnings = Switch("calendar.rule.earnings", "Trimestrale in arrivo: niente mean reversion su quel titolo");
public static readonly SettingDefinition Macro = Switch("calendar.rule.macro", "Dato macro importante: niente ingressi da 30 minuti prima a 30 dopo");
public static readonly IReadOnlyList<SettingDefinition> All = [Severe, Negative, Unconfirmed, Earnings, Macro];
public static async Task<NewsRuleSwitches> LoadAsync(SettingsStore? settings, CancellationToken cancellationToken)
{
if (settings is null)
{
return NewsRuleSwitches.AllOn;
}
async Task<bool> IsOn(SettingDefinition d) => (await settings.GetAsync(d.Key, cancellationToken).ConfigureAwait(false)).Value != Off;
return new NewsRuleSwitches(await IsOn(Severe).ConfigureAwait(false), await IsOn(Negative).ConfigureAwait(false), await IsOn(Unconfirmed).ConfigureAwait(false),
await IsOn(Earnings).ConfigureAwait(false), await IsOn(Macro).ConfigureAwait(false));
}
private static SettingDefinition Switch(string key, string label) => new(key, label, On, [On, Off]);
}
+1 -1
View File
@@ -486,7 +486,7 @@ public sealed class EquityMeanrev(EquityMeanrevSettings? settings = null) : IStr
/// </summary>
private bool EarningsAhead(StrategyContext context, string symbol)
{
if (context.NextEarnings?.Invoke(symbol) is not { } earnings || context.NextSession is not { } entry)
if (!context.EarningsEntries || context.NextEarnings?.Invoke(symbol) is not { } earnings || context.NextSession is not { } entry)
{
return false;
}
+6
View File
@@ -269,6 +269,12 @@ public sealed class StrategyContext(
/// <summary>A restriction from the news on the instrument (SPEC 10.5, Phase 7); null in a backtest.</summary>
public Func<string, bool>? Restricted { get; init; }
/// <summary>
/// Whether coming earnings keep <c>equity_meanrev</c> out (SPEC 10.5, fourth row, which the user can switch off).
/// The exit before the earnings of 8.3 is a rule of the strategy and stays.
/// </summary>
public bool EarningsEntries { get; init; } = true;
/// <summary>
/// The strategy's equity at each earlier close, up to and including this one: what the risk limits of SPEC 9.2
/// measure losses and the fall from the high on.
+3
View File
@@ -23,6 +23,9 @@ public static class DecisionOutcomes
public const string Passed = "passed";
public const string Reduced = "reduced";
public const string Refused = "refused";
/// <summary>Held back by a rule of SPEC 10.5 and tried again when it ends.</summary>
public const string Postponed = "postponed";
public const string Same = "same";
public const string Different = "different";
}
+11
View File
@@ -147,6 +147,17 @@ public static class DiaryText
public static string Recovered(string what, DateTimeOffset due, TimeZoneInfo zone) =>
$"Recuperato un lavoro saltato: {what} (previsto alle {TimeZoneInfo.ConvertTime(due, zone):HH:mm}).";
/// <summary>A purchase a rule of SPEC 10.5 put off: it goes when the restriction ends.</summary>
public static string EntryPostponed(string symbol, DateTimeOffset until, TimeZoneInfo zone, string reason, string motive) =>
$"Ho rimandato l'acquisto di {Shown(symbol)} alle {TimeZoneInfo.ConvertTime(until, zone):HH:mm}. {reason} Motivo: {motive}.";
/// <summary>A purchase a rule of SPEC 10.5 left out: the restriction lasts past its deadline (SPEC 5.6).</summary>
public static string EntryRestricted(string symbol, string reason, string motive) => $"Non compro {Shown(symbol)}. {reason} Motivo: {motive}.";
/// <summary>A stop the strategy wanted lower, kept where it was by a serious event (SPEC 10.5, first row).</summary>
public static string StopKept(string symbol, decimal level, string currency, string motive) =>
$"Lo stop di {Shown(symbol)} resta a {Level(level, currency)}: con un evento grave in corso non lo allargo. Motivo: {motive}.";
/// <summary>A missed job left out at the restart, with its reason (SPEC 5.6).</summary>
public static string Skipped(string what, string reason) => $"Saltato: {what}. {reason}";
+155 -18
View File
@@ -4,6 +4,7 @@ using Anubis.Backtest;
using Anubis.Core;
using Anubis.Data;
using Anubis.Db;
using Anubis.News;
using Anubis.Risk;
using Anubis.Strategies;
using Microsoft.Extensions.Logging;
@@ -47,6 +48,9 @@ public static class DecisionModes
public const string NoData = "no_data";
}
/// <summary>The earnings dates a decision sees, and whether the fourth row of 10.5 is on (Impostazioni).</summary>
public sealed record EarningsView(IReadOnlyDictionary<string, DateOnly> Next, bool BlockEntries);
/// <summary>The times of SPEC 5.5 and the limits of SPEC 5.6.</summary>
public sealed record LiveEngineOptions
{
@@ -138,6 +142,18 @@ public sealed partial class LiveEngine : IDisposable
/// <summary>Asks the data service for the bars a decision is waiting for (the history step). Called at most every few minutes.</summary>
public Func<CancellationToken, Task>? WantData { get; init; }
/// <summary>
/// The rules of SPEC 10.5 that stop a single purchase (news on the instrument, high-impact figures) and keep the
/// stop of a position under a serious event. Null: nothing is held back.
/// </summary>
public IEntryGate? Gate { get; init; }
/// <summary>
/// The earnings calendar of the stocks for a decision day (SPEC 10.5, fourth row; 8.3): null when the filter is
/// off, because the Finnhub key is missing or its answers are old.
/// </summary>
public Func<DateOnly, CancellationToken, Task<EarningsView?>>? Earnings { get; init; }
/// <summary>The time zone of the diary's times: Italian time.</summary>
public TimeZoneInfo Zone { get; init; } = Zones.Rome;
@@ -477,6 +493,15 @@ public sealed partial class LiveEngine : IDisposable
var strategy = CreateStrategy(m.Strategy, stage.Qualification?.Parameters);
var data = loaded.Data;
var options = StrategyPipeline.For(m.Strategy, data, new BacktestOptions());
if (Earnings is not null && m.Strategy != StrategyNames.CryptoTrend && await Earnings(m.Day, cancellationToken).ConfigureAwait(false) is { } earnings)
{
options = options with
{
NextEarnings = symbol => earnings.Next.TryGetValue(symbol, out var date) ? date : null,
EarningsEntries = earnings.BlockEntries,
};
}
DecisionRecord record;
if (observation)
{
@@ -500,6 +525,7 @@ public sealed partial class LiveEngine : IDisposable
plan = plan with { Working = await WorkingAsync(account, m.Strategy, cancellationToken).ConfigureAwait(false) };
}
plan = await GateAsync(m, mode, plan, data, cancellationToken).ConfigureAwait(false);
var oneStep = observation ? null : await OneStepAsync(m.Strategy, CreateStrategy(m.Strategy, stage.Qualification?.Parameters), data, cancellationToken).ConfigureAwait(false);
var specs = data.Instruments.ToDictionary(i => i.Symbol, i => i.Spec, StringComparer.Ordinal);
await WriteDecisionAsync(m, stage, account, mode, record, plan, book, specs, oneStep, cancellationToken).ConfigureAwait(false);
@@ -564,10 +590,67 @@ public sealed partial class LiveEngine : IDisposable
DecisionSummary Summary, decimal Equity)
{
public IReadOnlySet<Guid> Working { get; init; } = new HashSet<Guid>();
/// <summary>Purchases a rule of 10.5 held back: put off to when it ends, or left out when that is past their deadline.</summary>
public IReadOnlyList<(string Symbol, EntryHold Hold, bool Dropped)> Holds { get; init; } = [];
/// <summary>Stops the strategy wanted lower and a serious event kept where they were (10.5, first row).</summary>
public IReadOnlyList<(string Symbol, decimal Level, EntryHold Hold)> KeptStops { get; init; } = [];
}
private sealed record PlannedOrder(Guid InstrumentId, string Symbol, OrderSide Side, OrderPurpose Purpose, decimal Quantity, bool ExitAll, decimal? StopLevel, decimal? StopOffset,
decimal Reference, string Reason, DateTimeOffset Due, DateTimeOffset? ValidUntil, bool Crypto);
decimal Reference, string Reason, DateTimeOffset Due, DateTimeOffset? ValidUntil, bool Crypto)
{
/// <summary>The time the order was first due, when a restriction put it off.</summary>
public DateTimeOffset? FirstDue { get; init; }
}
/// <summary>
/// The rules of 10.5 on what was planned: a purchase under a restriction waits for its end, within the limits of
/// 5.6 (its deadline), or is left out; a stop is not lowered while a serious event lasts. The decision itself, and
/// so the example of 11.2, keeps what the strategy proposed.
/// </summary>
private async Task<PlanResult> GateAsync(Moment m, string mode, PlanResult plan, BacktestDataset data, CancellationToken cancellationToken)
{
if (Gate is null)
{
return plan;
}
var orders = new List<PlannedOrder>();
var holds = new List<(string, EntryHold, bool)>();
foreach (var order in plan.Orders)
{
if (order.Side != OrderSide.Buy || data[order.Symbol]?.Spec is not { } spec || await Gate.HoldAsync(spec, m.Now, cancellationToken).ConfigureAwait(false) is not { } hold)
{
orders.Add(order);
continue;
}
var dropped = order.ValidUntil is { } until && hold.Until > until;
holds.Add((order.Symbol, hold, dropped));
if (!dropped)
{
orders.Add(hold.Until > order.Due ? order with { Due = hold.Until, FirstDue = order.Due } : order);
}
}
var stops = new List<(string, decimal, decimal)>();
var kept = new List<(string, decimal, EntryHold)>();
foreach (var (symbol, from, to) in plan.Stops)
{
if (mode != DecisionModes.Observation && from > 0m && to < from && data[symbol]?.Spec is { } spec
&& await Gate.KeepStopAsync(spec, m.Now, cancellationToken).ConfigureAwait(false) is { } keep)
{
kept.Add((symbol, from, keep));
continue;
}
stops.Add((symbol, from, to));
}
return plan with { Orders = orders, Stops = stops, Holds = holds, KeptStops = kept };
}
/// <summary>
/// The intents become orders as the backtest plans them (BacktestEngine.Plan): target quantity from the weight and
@@ -721,6 +804,27 @@ public sealed partial class LiveEngine : IDisposable
}
}
foreach (var (symbol, hold, dropped) in plan.Holds)
{
await DecisionLog.WriteAsync(s, _clock, new DecisionRow(decisionId, m.Strategy, account, symbol, DecisionStages.Correctives,
dropped ? DecisionOutcomes.Refused : DecisionOutcomes.Postponed, [hold.Code], new Dictionary<string, object?> { ["until"] = hold.Until, ["motive"] = hold.Motive }, now),
cancellationToken).ConfigureAwait(false);
if (!observation)
{
var text = dropped ? DiaryText.EntryRestricted(symbol, ReasonCatalog.Text(hold.Code), hold.Motive)
: DiaryText.EntryPostponed(symbol, hold.Until, Zone, ReasonCatalog.Text(hold.Code), hold.Motive);
await Diary.WriteAsync(s, _clock, DiaryKind(hold.Code), hold.Code, text, new { decision = decisionId, symbol }, cancellationToken).ConfigureAwait(false);
}
}
foreach (var (symbol, level, hold) in plan.KeptStops)
{
await DecisionLog.WriteAsync(s, _clock, new DecisionRow(decisionId, m.Strategy, account, symbol, DecisionStages.Correctives, DecisionOutcomes.Refused, [hold.Code],
new Dictionary<string, object?> { ["stop"] = level, ["motive"] = hold.Motive }, now), cancellationToken).ConfigureAwait(false);
await Diary.WriteAsync(s, _clock, DiaryKinds.News, hold.Code, DiaryText.StopKept(symbol, level, currency, hold.Motive), new { decision = decisionId, symbol },
cancellationToken).ConfigureAwait(false);
}
foreach (var (symbol, reason) in plan.Notes)
{
await DecisionLog.WriteAsync(s, _clock, new DecisionRow(decisionId, m.Strategy, account, symbol, DecisionStages.Order, DecisionOutcomes.Refused, [reason], null, now),
@@ -764,11 +868,11 @@ public sealed partial class LiveEngine : IDisposable
var id = Ids.NewId(_clock);
await s.ExecuteAsync(
"INSERT INTO planned_orders (id, decision_id, strategy, account_id, instrument_id, side, purpose, quantity, exit_all, stop_level, stop_offset, reference_price, reason, due_at, valid_until, status, created_at, updated_at) " +
"VALUES (@id, @d, @s, @a, @i, @side, @p, @q, @all, @stop, @off, @ref, @r, @due, @until, @st, @now, @now)", cancellationToken,
"INSERT INTO planned_orders (id, decision_id, strategy, account_id, instrument_id, side, purpose, quantity, exit_all, stop_level, stop_offset, reference_price, reason, due_at, valid_until, first_due_at, status, created_at, updated_at) " +
"VALUES (@id, @d, @s, @a, @i, @side, @p, @q, @all, @stop, @off, @ref, @r, @due, @until, @first, @st, @now, @now)", cancellationToken,
("id", id), ("d", decisionId), ("s", m.Strategy), ("a", account), ("i", order.InstrumentId), ("side", Wire.Text(order.Side)), ("p", Wire.Text(order.Purpose)),
("q", DbValues.Money(order.Quantity)), ("all", order.ExitAll), ("stop", order.StopLevel), ("off", order.StopOffset), ("ref", order.Reference), ("r", order.Reason),
("due", order.Due), ("until", order.ValidUntil), ("st", PlannedStates.Pending), ("now", now)).ConfigureAwait(false);
("due", order.Due), ("until", order.ValidUntil), ("first", order.FirstDue), ("st", PlannedStates.Pending), ("now", now)).ConfigureAwait(false);
await DecisionLog.WriteAsync(s, _clock, new DecisionRow(decisionId, m.Strategy, account, order.Symbol, DecisionStages.Sizing, DecisionOutcomes.Passed, [],
new Dictionary<string, object?> { ["side"] = Wire.Text(order.Side), ["quantity"] = order.Quantity, ["equity"] = plan.Equity, ["close"] = order.Reference, ["planned"] = id },
now), cancellationToken).ConfigureAwait(false);
@@ -810,6 +914,9 @@ public sealed partial class LiveEngine : IDisposable
private static bool IsCorrective(string code) => code is CorrectiveReasons.CryptoRegimeAverage or CorrectiveReasons.CryptoRegimeVolatility or CorrectiveReasons.CalendarEvent;
// The news rows go with the news in the diary, the calendar ones with the decisions.
private static string DiaryKind(string code) => code is NewsReasons.Severe or NewsReasons.Negative ? DiaryKinds.News : DiaryKinds.Decisions;
private sealed record ProposedIntent(string Symbol, decimal? Weight, decimal? Stop);
// ---- One-step comparison (SPEC 12.3) ------------------------------------------------------------------------
@@ -846,7 +953,7 @@ public sealed partial class LiveEngine : IDisposable
// ---- Releasing planned orders -------------------------------------------------------------------------------
private sealed record Planned(Guid Id, Guid DecisionId, string Strategy, Guid AccountId, Guid InstrumentId, OrderSide Side, OrderPurpose Purpose, decimal Quantity, bool ExitAll,
decimal? StopLevel, decimal? StopOffset, decimal Reference, string Reason, DateTimeOffset Due, DateTimeOffset? ValidUntil);
decimal? StopLevel, decimal? StopOffset, decimal Reference, string Reason, DateTimeOffset Due, DateTimeOffset? ValidUntil, DateTimeOffset? FirstDue = null);
/// <summary>
/// The planned orders whose time has come go to the order manager, sales first (SPEC 5.5). An entry past its
@@ -856,12 +963,8 @@ public sealed partial class LiveEngine : IDisposable
/// </summary>
private async Task ReleaseAsync(DateTimeOffset now, CancellationToken cancellationToken)
{
var due = await _db.ReadAsync(s => s.QueryAsync(
"SELECT id, decision_id, strategy, account_id, instrument_id, side, purpose, quantity, exit_all, stop_level, stop_offset, reference_price, reason, due_at, valid_until " +
"FROM planned_orders WHERE status = @p AND due_at <= @now ORDER BY due_at, id",
r => new Planned(r.Id("id"), r.Id("decision_id"), r.Text("strategy"), r.Id("account_id"), r.Id("instrument_id"), Wire.Side(r.Text("side")), Wire.Purpose(r.Text("purpose")),
r.Money("quantity"), r.Bool("exit_all"), r.MoneyOrNull("stop_level"), r.MoneyOrNull("stop_offset"), r.Money("reference_price"), r.Text("reason"), r.Moment("due_at"),
r.MomentOrNull("valid_until")), ("p", PlannedStates.Pending), ("now", now)), cancellationToken).ConfigureAwait(false);
var due = await _db.ReadAsync(s => s.QueryAsync("SELECT " + PlannedColumns + " FROM planned_orders p WHERE p.status = @p AND p.due_at <= @now ORDER BY p.due_at, p.id",
ReadPlanned, ("p", PlannedStates.Pending), ("now", now)), cancellationToken).ConfigureAwait(false);
foreach (var order in due.OrderBy(o => o.Side == OrderSide.Buy).ThenBy(o => o.Due))
{
try
@@ -912,6 +1015,12 @@ public sealed partial class LiveEngine : IDisposable
return;
}
if (order.Side == OrderSide.Buy && Gate is not null && await Gate.HoldAsync(instrument.Spec, now, cancellationToken).ConfigureAwait(false) is { } hold)
{
await HoldAsync(order, instrument.Symbol, hold, now, cancellationToken).ConfigureAwait(false);
return;
}
var quantity = order.Quantity;
if (order.Side == OrderSide.Sell)
{
@@ -926,7 +1035,8 @@ public sealed partial class LiveEngine : IDisposable
}
}
var late = now - order.Due > Options.Late;
// An entry put off by a restriction is a recovered one: its price is checked against the decision's (SPEC 5.6).
var late = now - (order.FirstDue ?? order.Due) > Options.Late;
decimal? stopLevel = order.StopLevel;
if (order.Side == OrderSide.Buy)
{
@@ -959,7 +1069,8 @@ public sealed partial class LiveEngine : IDisposable
StopLevel = stopLevel,
StartAggressive = aggressive,
}, cancellationToken).ConfigureAwait(false);
var recovered = late ? DiaryText.Recovered(OrderWords(order, instrument.Symbol), order.Due, Zone) : null;
// A purchase put off by a restriction already said why in the diary; only one late because the bot was off is "recovered".
var recovered = late && order.FirstDue is null ? DiaryText.Recovered(OrderWords(order, instrument.Symbol), order.Due, Zone) : null;
await CloseAsync(order, refusal is null ? PlannedStates.Submitted : PlannedStates.Refused, refusal, execution, recovered, cancellationToken).ConfigureAwait(false);
}
@@ -997,11 +1108,11 @@ public sealed partial class LiveEngine : IDisposable
await s.ExecuteAsync(
"INSERT INTO planned_orders (id, decision_id, strategy, account_id, instrument_id, side, purpose, quantity, exit_all, stop_level, stop_offset, reference_price, " +
"reason, due_at, valid_until, status, outcome, execution_id, created_at, updated_at) VALUES (@id, @d, @s, @a, @i, @side, @p, @q, @x, @sl, @so, @r, @reason, " +
"@due, @until, @st, NULL, NULL, @now, @now)", cancellationToken,
"reason, due_at, valid_until, first_due_at, status, outcome, execution_id, created_at, updated_at) VALUES (@id, @d, @s, @a, @i, @side, @p, @q, @x, @sl, @so, @r, " +
"@reason, @due, @until, @first, @st, NULL, NULL, @now, @now)", cancellationToken,
("id", Ids.NewId(_clock)), ("d", order.DecisionId), ("s", order.Strategy), ("a", order.AccountId), ("i", order.InstrumentId), ("side", Wire.Text(order.Side)),
("p", Wire.Text(order.Purpose)), ("q", Math.Max(0m, order.Quantity - filled)), ("x", order.ExitAll), ("sl", order.StopLevel), ("so", order.StopOffset),
("r", order.Reference), ("reason", order.Reason), ("due", sale ? now : order.Due), ("until", order.ValidUntil), ("st", PlannedStates.Pending), ("now", now))
("r", order.Reference), ("reason", order.Reason), ("due", sale ? now : order.Due), ("until", order.ValidUntil), ("first", sale ? null : order.FirstDue), ("st", PlannedStates.Pending), ("now", now))
.ConfigureAwait(false);
await Diary.WriteAsync(s, _clock, DiaryKinds.Orders, LiveReasons.Restart, DiaryText.RestartedOrder(sale, symbol), new { planned = order.Id, execution },
cancellationToken).ConfigureAwait(false);
@@ -1015,12 +1126,12 @@ public sealed partial class LiveEngine : IDisposable
private sealed record Interrupted(Planned Order, decimal Filled, bool Unlinked);
private const string PlannedColumns =
"p.id, p.decision_id, p.strategy, p.account_id, p.instrument_id, p.side, p.purpose, p.quantity, p.exit_all, p.stop_level, p.stop_offset, p.reference_price, p.reason, p.due_at, p.valid_until";
"p.id, p.decision_id, p.strategy, p.account_id, p.instrument_id, p.side, p.purpose, p.quantity, p.exit_all, p.stop_level, p.stop_offset, p.reference_price, p.reason, p.due_at, p.valid_until, p.first_due_at";
private static Planned ReadPlanned(DbRow r) =>
new(r.Id("id"), r.Id("decision_id"), r.Text("strategy"), r.Id("account_id"), r.Id("instrument_id"), Wire.Side(r.Text("side")), Wire.Purpose(r.Text("purpose")),
r.Money("quantity"), r.Bool("exit_all"), r.MoneyOrNull("stop_level"), r.MoneyOrNull("stop_offset"), r.Money("reference_price"), r.Text("reason"), r.Moment("due_at"),
r.MomentOrNull("valid_until"));
r.MomentOrNull("valid_until"), r.MomentOrNull("first_due_at"));
/// <summary>
/// The planned order behind a canceled execution: the one that points to it, or, when the process stopped between
@@ -1049,6 +1160,32 @@ public sealed partial class LiveEngine : IDisposable
return unlinked is null ? (Interrupted?)null : new Interrupted(unlinked, info.Filled, true);
}, cancellationToken);
/// <summary>A purchase a rule of 10.5 stops now: put off to its end when that is within its deadline, otherwise left out.</summary>
private async Task HoldAsync(Planned order, string symbol, EntryHold hold, DateTimeOffset now, CancellationToken cancellationToken)
{
if (order.ValidUntil is { } until && hold.Until > until)
{
await CloseAsync(order, PlannedStates.Expired, hold.Code, null, DiaryText.EntryRestricted(symbol, ReasonCatalog.Text(hold.Code), hold.Motive), cancellationToken)
.ConfigureAwait(false);
return;
}
await _db.WriteAsync(async s =>
{
var moved = await s.ExecuteAsync("UPDATE planned_orders SET due_at = @u, first_due_at = @f, updated_at = @now WHERE id = @id AND status = @p", cancellationToken,
("u", hold.Until), ("f", order.FirstDue ?? order.Due), ("now", now), ("id", order.Id), ("p", PlannedStates.Pending)).ConfigureAwait(false);
if (moved == 0)
{
return;
}
await DecisionLog.WriteAsync(s, _clock, new DecisionRow(order.DecisionId, order.Strategy, order.AccountId, symbol, DecisionStages.Order, DecisionOutcomes.Postponed,
[hold.Code], new Dictionary<string, object?> { ["planned"] = order.Id, ["until"] = hold.Until, ["motive"] = hold.Motive }, now), cancellationToken).ConfigureAwait(false);
await Diary.WriteAsync(s, _clock, DiaryKind(hold.Code), hold.Code, DiaryText.EntryPostponed(symbol, hold.Until, Zone, ReasonCatalog.Text(hold.Code), hold.Motive),
new { planned = order.Id }, cancellationToken).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
}
private static string OrderWords(Planned order, string symbol) =>
(order.Side == OrderSide.Buy ? "l'acquisto di " : "la vendita di ") + DiaryText.Shown(symbol);
+7
View File
@@ -36,6 +36,13 @@ public static class ReasonCatalog
["crypto_regime_below_average"] = "Il mercato crypto era sotto la sua media: acquisti ridotti.",
["crypto_regime_high_volatility"] = "Il mercato crypto era molto agitato: acquisti ridotti.",
["calendar_event"] = "Un dato economico importante era vicino: niente nuove posizioni in quell'ora.",
// News and calendar (SPEC 10.5).
["news_severe"] = "Un evento grave e negativo su questo strumento: niente nuovi acquisti per 72 ore.",
["news_negative"] = "Una notizia negativa importante su questo strumento: niente acquisti né aumenti per 24 ore.",
["news_unconfirmed"] = "Una voce da una sola fonte non affidabile: nessuna azione, la ricontrollo dopo 2 ore.",
["news_rechecked"] = "La voce non è stata confermata da un'altra fonte: nessuna azione.",
["news_keep_stop"] = "Con un evento grave in corso lo stop non si allarga.",
["calendar_earnings"] = "Trimestrale in arrivo: niente mean reversion su questo titolo fino alla pubblicazione.",
// Risk.
["risk_reduction"] = "Ordine rimpicciolito dai limiti di rischio.",
["daily_loss_block"] = "Perdita del giorno oltre il limite: niente nuovi ingressi per 24 ore.",
+14
View File
@@ -187,6 +187,8 @@ public sealed partial class TradingService : IDisposable
Engine = new LiveEngine(db, clock, Ledger, Stages, ManagerOf, Quotes.GetAsync, market?.UsCalendar, null, _logger)
{
WantData = market is null ? null : _ => market.WakeWorker(),
Gate = new News.EntryGate(db, clock, settings),
Earnings = market is null ? null : (day, ct) => EarningsAsync(market, day, ct),
Ready = () => _started,
AccountReady = AccountReady,
};
@@ -194,6 +196,18 @@ public sealed partial class TradingService : IDisposable
private volatile bool _started;
// The earnings of SPEC 10.5 (fourth row) and 8.3: only while the Finnhub calendar is fresh, as the strategies expect.
private async Task<EarningsView?> EarningsAsync(MarketDataService market, DateOnly day, CancellationToken cancellationToken)
{
if (!await market.References.EarningsFilterActiveAsync(cancellationToken).ConfigureAwait(false))
{
return null;
}
var dates = await new News.NewsDesk(_db, _clock, _settings).EarningsAsync(day, cancellationToken).ConfigureAwait(false);
return new EarningsView(dates, (await News.NewsRuleSettings.LoadAsync(_settings, cancellationToken).ConfigureAwait(false)).Earnings);
}
/// <summary>The startup steps of SPEC 9.4 are done and the loop runs.</summary>
public bool IsStarted => _started;
+56
View File
@@ -132,6 +132,62 @@ public sealed class AbsenceTests
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; }
+11 -2
View File
@@ -97,11 +97,20 @@ internal sealed class StockRig : IAsyncDisposable
Paper = Rig.Paper(PaperOptions.Alpaca(), Alpaca.AccountId);
await Paper.DepositAsync(capital * Rate, "USD", none).ConfigureAwait(false);
Manager = Rig.Manager(Paper, Alpaca);
var stages = new StageBook(Rig.Db, Rig.Clock, TradingConsent.None, (_, _, _) => Task.FromResult(false));
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar);
_stages = new StageBook(Rig.Db, Rig.Clock, TradingConsent.None, (_, _, _) => Task.FromResult(false));
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar);
Equity = new EquityBook(Rig.Db, Rig.Clock, Rig.Ledger, fx);
}
private StageBook _stages = null!;
/// <summary>The engine again, reading the earnings calendar (SPEC 10.5, fourth row).</summary>
public void UseEarnings(Func<DateOnly, CancellationToken, Task<EarningsView?>> earnings)
{
Engine.Dispose();
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar) { Earnings = earnings };
}
/// <summary>
/// A slow rise with noise and, every 17 sessions, three sessions down of 2.5% each: RSI(2) falls under 10 and the
/// close under its 5-session average, so equity_meanrev buys and sells a few sessions later.
+12
View File
@@ -109,9 +109,21 @@ internal sealed class LiveRig : IAsyncDisposable
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, Stages, id => id == Okx.AccountId ? Manager : null, Rig.QuoteAsync)
{
AccountReady = _ => AccountStarted,
Gate = Gate,
};
}
/// <summary>The rules of SPEC 10.5 the engine applies to purchases and stops; none unless a test sets them.</summary>
public News.IEntryGate? Gate { get; private set; }
/// <summary>The engine again, with the rules of 10.5 read from the rig's database.</summary>
public void UseGate(News.IEntryGate gate)
{
Gate = gate;
Engine.Dispose();
Build();
}
/// <summary>
/// The process dies and starts again: a new simulator on its saved book, a new order manager and engine on the same
/// database, the account reconciled before the first pulse as at a real start (SPEC 9.4).
+335
View File
@@ -0,0 +1,335 @@
using Anubis.Core;
using Anubis.Data;
using Anubis.Db;
using Anubis.News;
using Anubis.Strategies;
using Anubis.Trading;
namespace Anubis.Tests;
/// <summary>
/// The rules of SPEC 10.5 and the state of 10.2 point 6 (issue #59): which news restrict what and for how long, the
/// strictest of them, the switches, the diary lines, and the purchases the live engine holds back.
/// </summary>
[TestClass]
public sealed class NewsRulesTests
{
private static readonly CancellationToken _none = CancellationToken.None;
private static readonly DateTimeOffset Now = new(2026, 11, 16, 10, 0, 0, TimeSpan.Zero);
private static int _ids;
private static StoryFact Story(double hoursAgo, int importance, double? score = null, bool trusted = true, int sources = 1, double? seenHoursAgo = null) =>
new(new Guid(Interlocked.Increment(ref _ids), 0, 0, new byte[8]), "Titolo", "CoinDesk", Now.AddHours(-hoursAgo), Now.AddHours(-(seenHoursAgo ?? hoursAgo)), sources,
trusted, score, importance >= 4 ? SevereKinds.Cyberattack : null, importance);
[TestMethod]
public void OnlyConfirmedNegativeNewsRestrictAndOnlyForTheirWindow()
{
NewsRestriction? One(StoryFact story, NewsRuleSwitches? switches = null) => NewsRules.Active([story], Now, switches ?? NewsRuleSwitches.AllOn).SingleOrDefault();
var severe = One(Story(71, 4))!;
Assert.AreEqual(NewsReasons.Severe, severe.Code);
Assert.AreEqual(Now.AddHours(1), severe.Until, "72 hours from the fact, not from when the bot read it.");
Assert.IsNull(One(Story(73, 4)), "An effect already over is not applied (SPEC 5.6).");
var negative = One(Story(23, 3, -0.8))!;
Assert.AreEqual(NewsReasons.Negative, negative.Code);
Assert.AreEqual(Now.AddHours(1), negative.Until);
Assert.IsNull(One(Story(25, 3, -0.8)));
Assert.IsNull(One(Story(1, 2, -0.4)), "Slightly negative news create no restriction.");
Assert.IsNull(One(Story(1, 3, 0.5)), "An importance of 3 that is not negative restricts nothing.");
// One untrusted source: nothing happens until a second independent source confirms (third row).
var rumour = Story(1, 4, trusted: false);
Assert.IsNull(One(rumour));
Assert.IsTrue(NewsRules.Unconfirmed(rumour));
Assert.AreEqual(NewsReasons.Severe, One(rumour with { Sources = 2 })!.Code);
Assert.IsFalse(NewsRules.Unconfirmed(rumour with { Sources = 2 }));
Assert.IsFalse(NewsRules.Unconfirmed(Story(1, 2, -0.4, trusted: false)), "Only what would restrict is a rumour to check.");
Assert.IsNull(One(Story(1, 4, seenHoursAgo: -1)), "A story the bot has not read yet at that moment does nothing.");
var dated = One(Story(-5, 4, seenHoursAgo: 2))!;
Assert.AreEqual(Now.AddHours(70), dated.Until, "A feed dated in the future counts from when the bot read it.");
Assert.IsNull(One(Story(1, 4), new NewsRuleSwitches(Severe: false)), "Each row can be switched off.");
Assert.IsNull(One(Story(1, 3, -0.9), new NewsRuleSwitches(Negative: false)));
}
[TestMethod]
public void TheStrictestRestrictionWinsAndOnlyASeriousEventKeepsTheStop()
{
var severeEnding = Story(71, 4);
var negativeLonger = Story(4, 3, -0.7);
var active = NewsRules.Active([severeEnding, negativeLonger], Now, NewsRuleSwitches.AllOn);
Assert.HasCount(2, active);
var strictest = NewsRules.Strictest(active)!;
Assert.AreEqual(NewsReasons.Negative, strictest.Code, "The one that holds the purchases longer.");
Assert.AreEqual(Now.AddHours(20), strictest.Until);
Assert.AreEqual(severeEnding.Id, NewsRules.KeepsStop(active)!.Story.Id, "The stop stays while the serious event lasts.");
var tie = NewsRules.Active([Story(48, 4), Story(0, 3, -0.7)], Now, NewsRuleSwitches.AllOn);
Assert.AreEqual(NewsReasons.Severe, NewsRules.Strictest(tie)!.Code, "Ending together, the serious event is the stricter one.");
Assert.IsNull(NewsRules.KeepsStop(NewsRules.Active([negativeLonger], Now, NewsRuleSwitches.AllOn)));
Assert.IsNull(NewsRules.Strictest([]));
}
[TestMethod]
public void TheStateWeighsTheToneByAgeAndComparesTheCountWithTheNorm()
{
// With a half-life of 12 hours a story 13 hours old weighs half of one an hour old: (−0.8 + 0.4 / 2) / 1.5.
List<StoryFact> stories = [Story(1, 3, -0.8), Story(2, 1), Story(13, 1, 0.4), Story(40, 1, 0.6)];
for (var day = 3; day <= 10; day++)
{
stories.Add(Story((24 * day) - 12, 1));
}
var state = NewsRules.State(stories, Now, NewsRuleSwitches.AllOn, Now.AddDays(-10));
Assert.AreEqual(-0.4, state.Tone1Day!.Value, 1e-9);
Assert.IsTrue(state.Tone3Days > state.Tone1Day && state.Tone3Days < 0.6, $"{state.Tone3Days}");
Assert.AreEqual(3, state.Stories1Day, "A story without a score is counted, not in the tone.");
Assert.AreEqual(1.0, state.NormalPerDay!.Value, 1e-9, "Nine stories in the nine days before the last one.");
Assert.AreEqual(3.0, state.Busy!.Value, 1e-9);
Assert.AreEqual(NewsReasons.Negative, state.Active!.Code, "The most serious event in force.");
var young = NewsRules.State(stories, Now, NewsRuleSwitches.AllOn, Now.AddHours(-30));
Assert.IsNull(young.NormalPerDay, "No norm before two days of news.");
Assert.IsNull(young.Busy);
var quiet = NewsRules.State([Story(2, 1)], Now, NewsRuleSwitches.AllOn, null);
Assert.IsNull(quiet.Tone1Day, "No scored story, no tone.");
Assert.IsNull(quiet.Active);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task TheGateHoldsPurchasesForNewsAndFiguresAndEveryRowCanBeSwitchedOff()
{
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
var clock = new ManualClock(Now);
var eth = MarketDataStoreTests.Okx("ETH-EUR");
var sol = MarketDataStoreTests.Okx("SOL-EUR");
var btc = MarketDataStoreTests.Okx("BTC-EUR");
await StoryAsync(test.Db, clock, eth, "Exchange hacked, ETH drained", Now.AddHours(-10), 4, -0.9, "CoinDesk", trusted: true).ConfigureAwait(false);
await StoryAsync(test.Db, clock, sol, "Solana validator exploited", Now.AddHours(-1), 4, -0.9, "The Block", trusted: false).ConfigureAwait(false);
await new CalendarEventStore(test.Db, clock).UpsertAsync(
[new CalendarEvent(CalendarEventKinds.Economic, "Inflazione USA", Now.AddMinutes(20), true, "test", "cpi") { Currency = "USD", Impact = "High" }], _none)
.ConfigureAwait(false);
var settings = new SettingsStore(test.Db, clock, new EventBus());
var gate = new EntryGate(test.Db, clock, settings);
var news = (await gate.HoldAsync(eth, Now, _none).ConfigureAwait(false))!;
Assert.AreEqual(NewsReasons.Severe, news.Code);
Assert.AreEqual(Now.AddHours(62), news.Until, "The news outlasts the figure: the strictest wins.");
Assert.AreEqual("«Exchange hacked, ETH drained» (CoinDesk)", news.Motive);
Assert.AreEqual(NewsReasons.KeepStop, (await gate.KeepStopAsync(eth, Now, _none).ConfigureAwait(false))!.Code);
var figure = (await gate.HoldAsync(sol, Now, _none).ConfigureAwait(false))!;
Assert.AreEqual(CorrectiveReasons.CalendarEvent, figure.Code, "The rumour on SOL does nothing; the figure still holds every purchase.");
Assert.AreEqual(Now.AddMinutes(50), figure.Until);
Assert.AreEqual("Inflazione USA", figure.Motive);
Assert.IsNull(await gate.KeepStopAsync(sol, Now, _none).ConfigureAwait(false));
Assert.IsNull(await gate.HoldAsync(btc, Now.AddMinutes(50), _none).ConfigureAwait(false), "At the end of the window the purchase goes.");
Assert.IsNull(await gate.HoldAsync(btc, Now.AddMinutes(-31), _none).ConfigureAwait(false));
var state = await new NewsDesk(test.Db, clock, settings).StateAsync(NewsDesk.Target(eth), _none).ConfigureAwait(false);
Assert.AreEqual(1, state.Stories1Day);
Assert.AreEqual(-0.9, state.Tone1Day!.Value, 1e-9);
Assert.AreEqual(NewsReasons.Severe, state.Active!.Code);
await settings.SetAsync(NewsRuleSettings.Severe.Key, NewsRuleSettings.Off, "test", _none).ConfigureAwait(false);
Assert.AreEqual(CorrectiveReasons.CalendarEvent, (await gate.HoldAsync(eth, Now, _none).ConfigureAwait(false))!.Code);
Assert.IsNull(await gate.KeepStopAsync(eth, Now, _none).ConfigureAwait(false));
await settings.SetAsync(NewsRuleSettings.Macro.Key, NewsRuleSettings.Off, "test", _none).ConfigureAwait(false);
Assert.IsNull(await gate.HoldAsync(eth, Now, _none).ConfigureAwait(false), "With both rows off nothing holds the purchase.");
Assert.IsFalse((await NewsRuleSettings.LoadAsync(settings, _none).ConfigureAwait(false)).Severe);
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task TheDiarySaysEachRestrictionOnceAndChecksARumourTwoHoursLater()
{
await using var test = await DatabaseTests.MigratedAsync().ConfigureAwait(false);
var clock = new ManualClock(Now);
var eth = MarketDataStoreTests.Okx("ETH-EUR");
var sol = MarketDataStoreTests.Okx("SOL-EUR");
var ada = MarketDataStoreTests.Okx("ADA-EUR");
var jpm = new InstrumentSpec(Venues.Alpaca, "JPM", "JPMorgan Chase & Co. Common Stock", AssetClasses.Equity, "USD");
var catalog = new InstrumentCatalog(test.Db, clock);
await catalog.SyncAsync(Venues.Okx, [eth, sol, ada], complete: false, _none).ConfigureAwait(false);
await catalog.SyncAsync(Venues.Alpaca, [jpm], complete: false, _none).ConfigureAwait(false);
foreach (var (universe, spec) in new[] { (Universes.Crypto, eth), (Universes.Crypto, sol), (Universes.Equity, jpm) })
{
var id = (await catalog.ListAsync(spec.Venue, _none).ConfigureAwait(false)).Single(i => i.Symbol == spec.Symbol).Id;
await test.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO universe_members (id, universe, as_of, instrument_id, rank, created_at) VALUES (@id, @u, '2026-11-16', @i, 1, @now)",
_none, ("id", Ids.NewId(clock)), ("u", universe), ("i", id), ("now", Now)), _none).ConfigureAwait(false);
}
await StoryAsync(test.Db, clock, eth, "Exchange hacked, ETH drained", Now.AddHours(-3), 4, -0.9, "CoinDesk", trusted: true).ConfigureAwait(false);
await StoryAsync(test.Db, clock, sol, "Solana validator exploited", Now.AddMinutes(-30), 4, -0.9, "The Block", trusted: false).ConfigureAwait(false);
await StoryAsync(test.Db, clock, ada, "Cardano exchange halts withdrawals", Now.AddHours(-1), 4, -0.9, "CoinDesk", trusted: true).ConfigureAwait(false);
await new CalendarEventStore(test.Db, clock).UpsertAsync(
[new CalendarEvent(CalendarEventKinds.Earnings, "Trimestrale JPM", new DateTimeOffset(2026, 11, 18, 11, 0, 0, TimeSpan.Zero), false, "test", "earnings:JPM") { Symbol = "JPM", TimeHint = "bmo" }],
_none).ConfigureAwait(false);
var watcher = new NewsWatcher(test.Db, clock);
Assert.AreEqual(3, await watcher.RunOnceAsync(_none).ConfigureAwait(false));
var diary = await DiaryAsync(test.Db).ConfigureAwait(false);
CollectionAssert.AreEquivalent(new[]
{
"Ho messo una restrizione per 72 ore: niente nuovi acquisti di ETH/EUR. Motivo: «Exchange hacked, ETH drained» (CoinDesk).",
"Voce non confermata su SOL/EUR: «Solana validator exploited» (The Block). Nessuna azione: ricontrollo tra 2 ore.",
"Ho messo una restrizione fino a mercoledì 18 novembre: niente mean reversion su JPM. Motivo: JPM pubblica i conti mercoledì 18 novembre prima dell'apertura.",
}, diary, "ADA is neither held nor a candidate: its restriction holds its purchases without a line in the diary.");
var queued = await test.Db.ReadAsync(s => s.QueryAsync("SELECT message FROM notifications", r => r.Text("message")), _none).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { diary.Single(d => d.Contains("ETH/EUR", StringComparison.Ordinal)) }, queued, "Only the serious event goes to Telegram.");
Assert.AreEqual(0, await watcher.RunOnceAsync(_none).ConfigureAwait(false), "Each line is written once.");
clock.Set(Now.AddHours(2));
Assert.AreEqual(1, await watcher.RunOnceAsync(_none).ConfigureAwait(false));
Assert.AreEqual("Ricontrollata la voce su SOL/EUR: nessun'altra fonte la conferma, nessuna azione.", (await DiaryAsync(test.Db).ConfigureAwait(false))[^1]);
// A position held: the diary says the stop stays where it is.
var account = Ids.NewId(clock);
var ethId = (await catalog.ListAsync(Venues.Okx, _none).ConfigureAwait(false)).Single(i => i.Symbol == "ETH-EUR").Id;
await test.Db.WriteAsync(async s =>
{
await s.ExecuteAsync("INSERT INTO accounts (id, name, broker, environment, base_currency, created_at) VALUES (@id, 'okx', 'okx', 'demo', 'EUR', @now)", _none,
("id", account), ("now", Now)).ConfigureAwait(false);
await s.ExecuteAsync("INSERT INTO positions (id, account_id, instrument_id, strategy, quantity, average_price, opened_at, created_at, updated_at) " +
"VALUES (@id, @a, @i, 'crypto_trend', @q, @p, @now, @now, @now)", _none, ("id", Ids.NewId(clock)), ("a", account), ("i", ethId), ("q", DbValues.Money(0.5m)),
("p", DbValues.Money(2000m)), ("now", Now)).ConfigureAwait(false);
}, _none).ConfigureAwait(false);
await StoryAsync(test.Db, clock, eth, "Ethereum foundation under investigation", Now.AddHours(1), 4, -0.9, "CNBC", trusted: true).ConfigureAwait(false);
Assert.AreEqual(1, await watcher.RunOnceAsync(_none).ConfigureAwait(false));
Assert.AreEqual("Ho messo una restrizione per 72 ore: niente nuovi acquisti di ETH/EUR. Lo stop resta dov'è. Motivo: «Ethereum foundation under investigation» (CNBC).",
(await DiaryAsync(test.Db).ConfigureAwait(false))[^1]);
}
/// <summary>
/// The crypto close of the first live day buys ETH and SOL (AbsenceTests). A serious event on ETH and a negative
/// story on SOL, both confirmed, hold the purchases past 12:00 UTC: they are left out, the decision keeps what the
/// strategy proposed, and the diary says which rule and which story.
/// </summary>
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ARestrictionLeavesOutThePurchaseAndTheDiarySaysWhy()
{
var day = new DateOnly(2026, 9, 1);
await using var live = await LiveRig.CreateAsync(day, day.AddDays(39)).ConfigureAwait(false);
live.UseGate(new EntryGate(live.Rig.Db, live.Rig.Clock));
await StoryAsync(live.Rig.Db, live.Rig.Clock, live.Instruments["ETH-EUR"].Spec, "Ethereum exchange hacked", new DateTimeOffset(2026, 8, 31, 20, 0, 0, TimeSpan.Zero), 4, -0.9,
"CoinDesk", trusted: true).ConfigureAwait(false);
await StoryAsync(live.Rig.Db, live.Rig.Clock, live.Instruments["SOL-EUR"].Spec, "Solana upgrade delayed by a week", new DateTimeOffset(2026, 8, 31, 23, 30, 0, TimeSpan.Zero),
3, -0.8, "CoinDesk", trusted: true).ConfigureAwait(false);
await live.DayAsync(day).ConfigureAwait(false);
var fills = await live.LiveFillsAsync().ConfigureAwait(false);
Assert.IsFalse(fills.Any(f => f.Symbol is "ETH-EUR" or "SOL-EUR"), string.Join(", ", fills));
var diary = await DiaryAsync(live.Rig.Db).ConfigureAwait(false);
Assert.Contains("Non compro ETH/EUR. " + ReasonCatalog.Text(NewsReasons.Severe) + " Motivo: «Ethereum exchange hacked» (CoinDesk).", diary);
Assert.Contains("Non compro SOL/EUR. " + ReasonCatalog.Text(NewsReasons.Negative) + " Motivo: «Solana upgrade delayed by a week» (CoinDesk).", diary);
var rows = await live.Rig.QueryAsync("SELECT symbol, stage, outcome, reasons FROM decisions WHERE stage IN (@c, @st)",
r => (Symbol: r.TextOrNull("symbol"), Stage: r.Text("stage"), Outcome: r.Text("outcome"), Reasons: r.Text("reasons")), ("c", DecisionStages.Correctives),
("st", DecisionStages.Strategy)).ConfigureAwait(false);
Assert.IsTrue(rows.Any(r => r is { Symbol: "ETH-EUR", Stage: DecisionStages.Strategy }), "The proposal stays: it is an example of 11.2.");
Assert.IsTrue(rows.Any(r => r is { Symbol: "ETH-EUR", Stage: DecisionStages.Correctives, Outcome: DecisionOutcomes.Refused } && r.Reasons.Contains(NewsReasons.Severe,
StringComparison.Ordinal)));
Assert.IsTrue(rows.Any(r => r is { Symbol: "SOL-EUR", Stage: DecisionStages.Correctives, Outcome: DecisionOutcomes.Refused } && r.Reasons.Contains(NewsReasons.Negative,
StringComparison.Ordinal)));
Assert.IsEmpty(await live.Rig.QueryAsync("SELECT id FROM planned_orders WHERE side = 'buy'", r => r.Id("id")).ConfigureAwait(false), "Nothing left to send.");
}
/// <summary>
/// A high-impact US figure at 00:10 UTC blocks entries from 23:40 to 00:40: the purchases decided at 00:05 wait
/// and go at 00:40, still within the crypto deadline, with the price check of a recovered entry.
/// </summary>
[TestMethod]
[TestCategory(Category.Integration)]
public async Task AFigureDelaysThePurchasesToTheEndOfItsWindow()
{
var day = new DateOnly(2026, 9, 1);
await using var live = await LiveRig.CreateAsync(day, day.AddDays(39)).ConfigureAwait(false);
live.UseGate(new EntryGate(live.Rig.Db, live.Rig.Clock));
await new CalendarEventStore(live.Rig.Db, live.Rig.Clock).UpsertAsync(
[new CalendarEvent(CalendarEventKinds.Economic, "Inflazione USA", new DateTimeOffset(2026, 9, 1, 0, 10, 0, TimeSpan.Zero), true, "test", "cpi") { Currency = "USD", Impact = "High" }],
_none).ConfigureAwait(false);
var bought = new List<string>();
await live.DayAsync(day, async minute =>
{
if (minute == 30)
{
bought.AddRange((await live.LiveFillsAsync().ConfigureAwait(false)).Where(f => f.Side == OrderSide.Buy).Select(f => f.Symbol));
}
}, morning: 60).ConfigureAwait(false);
Assert.IsEmpty(bought, "Nothing goes inside the window.");
var fills = await live.LiveFillsAsync().ConfigureAwait(false);
Assert.IsTrue(fills.Any(f => f is { Symbol: "ETH-EUR", Side: OrderSide.Buy }), string.Join(", ", fills));
var diary = await DiaryAsync(live.Rig.Db).ConfigureAwait(false);
Assert.Contains("Ho rimandato l'acquisto di ETH/EUR alle 02:40. " + ReasonCatalog.Text(CorrectiveReasons.CalendarEvent) + " Motivo: Inflazione USA.", diary);
Assert.IsFalse(diary.Any(d => d.StartsWith("Recuperato un lavoro saltato: l'acquisto", StringComparison.Ordinal)), "A purchase put off is not a missed job.");
var first = await live.Rig.QueryAsync("SELECT first_due_at FROM planned_orders WHERE side = 'buy'", r => r.MomentOrNull("first_due_at")).ConfigureAwait(false);
Assert.IsTrue(first.Count > 0 && first.All(f => f == new DateTimeOffset(2026, 9, 1, 0, 5, 0, TimeSpan.Zero)), "The time it was first due is kept for the price check.");
}
/// <summary>
/// The close of 1 September plans a mean reversion purchase of BBB (AbsenceTests). With its earnings on 3 September,
/// before the open, the strategy stays out; with the fourth row of 10.5 switched off it buys as before.
/// </summary>
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ComingEarningsKeepMeanReversionOutUnlessTheRowIsOff()
{
var day = new DateOnly(2026, 9, 1);
foreach (var on in new[] { true, false })
{
await using var stocks = await StockRig.CreateAsync(day, day.AddDays(41)).ConfigureAwait(false);
await new CalendarEventStore(stocks.Rig.Db, stocks.Rig.Clock).UpsertAsync(
[new CalendarEvent(CalendarEventKinds.Earnings, "Trimestrale BBB", new DateTimeOffset(2026, 9, 3, 11, 0, 0, TimeSpan.Zero), false, "test", "earnings:BBB")
{ Symbol = "BBB", TimeHint = "bmo" }], _none).ConfigureAwait(false);
var desk = new NewsDesk(stocks.Rig.Db, stocks.Rig.Clock);
stocks.UseEarnings(async (d, ct) => new EarningsView(await desk.EarningsAsync(d, ct).ConfigureAwait(false), on));
await stocks.SessionAsync(day).ConfigureAwait(false);
var planned = await stocks.Rig.QueryAsync(
"SELECT i.symbol FROM planned_orders p JOIN instruments i ON i.id = p.instrument_id WHERE p.side = 'buy' AND p.status = @s", r => r.Text("symbol"),
("s", PlannedStates.Pending)).ConfigureAwait(false);
if (on)
{
Assert.IsEmpty(planned, "Earnings within three sessions of the entry: no mean reversion.");
}
else
{
CollectionAssert.AreEqual(new[] { "BBB" }, planned.ToList(), "With the row off the purchase is planned as before.");
}
}
}
/// <summary>A story linked to one instrument, with one article from the named publisher.</summary>
internal static async Task<Guid> StoryAsync(Database db, IClock clock, InstrumentSpec spec, string title, DateTimeOffset published, int importance, double? score,
string publisher, bool trusted, int sources = 1, DateTimeOffset? seen = null)
{
var store = new NewsStore(db, clock);
await store.EnsureDefaultsAsync(_none).ConfigureAwait(false);
var source = (await store.ListSourcesAsync(_none).ConfigureAwait(false))[0];
var id = Ids.NewId(clock);
var at = seen ?? clock.UtcNow;
var severe = importance >= 4 ? SevereKinds.Cyberattack : null;
await db.WriteAsync(async s =>
{
await s.ExecuteAsync("INSERT INTO news_stories (id, title, title_key, published_at, first_seen_at, last_seen_at, sources, trusted, score, severe, importance, created_at, updated_at) " +
"VALUES (@id, @t, @k, @p, @seen, @seen, @n, @tr, @sc, @sv, @imp, @seen, @seen)", _none, ("id", id), ("t", title), ("k", title.ToLowerInvariant()), ("p", published),
("seen", at), ("n", sources), ("tr", trusted), ("sc", score), ("sv", severe), ("imp", importance)).ConfigureAwait(false);
await s.ExecuteAsync("INSERT INTO news_items (id, story_id, source_id, url, url_key, publisher, title, summary, language, published_at, score, severe, importance, created_at) " +
"VALUES (@id, @st, @src, @u, @u, @pub, @t, NULL, 'en', @p, @sc, @sv, @imp, @seen)", _none, ("id", Ids.NewId(clock)), ("st", id), ("src", source.Id),
("u", "https://example.test/" + id), ("pub", publisher), ("t", title), ("p", published), ("sc", score), ("sv", severe), ("imp", importance), ("seen", at)).ConfigureAwait(false);
await s.ExecuteAsync("INSERT INTO news_links (id, story_id, scope, target, via, created_at) VALUES (@id, @st, @sc, @t, @v, @seen)", _none, ("id", Ids.NewId(clock)),
("st", id), ("sc", LinkScopes.Instrument), ("t", NewsDesk.Target(spec)), ("v", LinkVia.Name), ("seen", at)).ConfigureAwait(false);
}, _none).ConfigureAwait(false);
return id;
}
internal static async Task<List<string>> DiaryAsync(Database db) =>
await db.ReadAsync(s => s.QueryAsync("SELECT message FROM journal ORDER BY occurred_at, created_at, id", r => r.Text("message")), _none).ConfigureAwait(false);
}