using Anubis.Backtest; using Anubis.Core; using Anubis.Learning; using Anubis.Trading; namespace Anubis.Server; /// A row of the balance of the restrictions: this period and since the start. internal sealed record ReviewRestriction(string Name, RestrictionLine? Period, RestrictionLine? Ever); /// A base rate before and after a review. internal sealed record ReviewRate(string Name, string Before, string After); /// How the quarterly review (SPEC 12.2) is written for the user. internal static class ReviewView { public static string Verdict(ReviewLine line) => line.Verdict switch { ReviewVerdicts.Passed => "Superata", ReviewVerdicts.Rejected => "Non superata", ReviewVerdicts.Error => "Non riuscita", _ => line.NoProtocol ? "Non riprovata" : "In attesa dei dati", }; public static string? VerdictClass(ReviewLine line) => line.Verdict switch { ReviewVerdicts.Passed => "gain", ReviewVerdicts.Rejected or ReviewVerdicts.Error => "loss", _ => null, }; /// What changed for the strategy, as a sentence. public static string Action(ReviewLine line) { var text = QuarterlyReview.ActionText(line); return char.ToUpperInvariant(text[0]) + text[1..]; } /// How the strategy did live, in the words of its stage. public static IReadOnlyList Live(ReviewLine line, IReadOnlyDictionary rates) { var lines = new List(); if (line.Stage == StrategyStages.Observation) { lines.Add(line.Signals == 0 ? "Nessun segnale chiuso dal vivo." : $"{Signals(line.Signals)}, in guadagno {LabText.Percent((double)line.Won / line.Signals, 0)}" + (rates.TryGetValue(line.Strategy, out var rate) ? $" (di solito {LabText.Percent(rate, 0)})." : ".")); return lines; } if (line.Live is not { } f) { return ["Niente da confrontare."]; } lines.Add($"Risultato {Fraction(f.Result, sign: true)}" + (line.BacktestOverSameDays is { } b ? $"; il backtest negli stessi giorni circa {LabText.Percent(b, 1, withSign: true)}." : ".")); lines.Add($"Calo massimo {Fraction(f.Fall)}" + (line.ExpectedFall is { } e ? $", atteso {Fraction(e)}." : ".")); lines.Add($"Scostamento dei prezzi {Bp(f.SlippageBp)}" + (line.ExpectedSlippageBp is { } s ? $", atteso {Bp(s)}." : ".")); lines.Add($"Operazioni chiuse {f.Trades}" + (f.OneStep is { } step ? $"; decisioni identiche al backtest del giorno dopo {Fraction(1m - step)}." : ".")); return lines; } private static string Signals(int count) => count == 1 ? "1 segnale chiuso" : $"{count} segnali chiusi"; private static string Fraction(decimal? value, bool sign = false) => value is { } v ? LabText.Percent((double)v, 1, sign) : LabText.Missing; private static string Bp(decimal? value) => value is { } v ? Format.Quantity(Math.Round(v, 1)) + " punti base" : LabText.Missing; public static IReadOnlyList Rates(ReviewReport report) => [.. report.RatesBefore.Keys.Union(report.RatesAfter?.Keys ?? []).Order(StringComparer.Ordinal).Select(s => new ReviewRate(StrategyNames.Label(s, withMarket: true), report.RatesBefore.TryGetValue(s, out var before) ? LabText.Percent(before) : LabText.Missing, (report.RatesAfter ?? report.RatesBefore).TryGetValue(s, out var after) ? LabText.Percent(after) : LabText.Missing))]; public static IReadOnlyList Restrictions(ReviewReport report) => [.. RestrictionBalance.Codes .Select(c => new ReviewRestriction(RestrictionBalance.Label(c), report.Restrictions.Lines.FirstOrDefault(l => l.Code == c), report.RestrictionsEver.Lines.FirstOrDefault(l => l.Code == c))) .Where(r => r.Period is not null || r.Ever is not null)]; /// The cases of a row that gave no amount, in words. public static string Rest(RestrictionLine? line) { if (line is null) { return LabText.Missing; } var parts = new List(); void Add(int count, string one, string many) { if (count > 0) { parts.Add(count == 1 ? "1 " + one : $"{count} {many}"); } } Add(line.Open, "ancora aperta", "ancora aperte"); Add(line.Delayed, "solo rimandata", "solo rimandate"); Add(line.NeverStarted, "mai partita", "mai partite"); Add(line.Increases, "aumento senza esito", "aumenti senza esito"); return parts.Count == 0 ? LabText.Missing : string.Join(", ", parts); } public static string Running(Review review) => review.Status == ReviewStatuses.Preparing ? $"La revisione del {review.Title} aspetta gli archivi dei prezzi fino al {LabText.Day(review.Through)}: ci riprovo ogni notte, fino al {QuarterlyReview.LastDay} del mese." : $"La revisione del {review.Title} รจ in corso: i test sono nell'elenco qui sotto."; }