using Anubis.Backtest; using Anubis.Core; namespace Anubis.Server; /// How the laboratory pages write the numbers of a backtest, in plain Italian (SPEC 13.3). internal static class LabText { public const string Missing = "—"; /// A fraction as a percentage: 0,1234 is "12,3%". public static string Percent(double? fraction, int decimals = 1, bool withSign = false) => fraction is { } x && double.IsFinite(x) ? Format.Percent((decimal)(x * 100), decimals, withSign) : Missing; public static string Number(double? value, int decimals = 2) => value is { } x && double.IsFinite(x) ? Format.Number((decimal)x, decimals) : Missing; public static string Money(decimal value, string currency, bool withSign = false) => Format.Money(value, currency, withSign); public static string Day(DateOnly? day) => day is { } d ? d.ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture) : Missing; public static string Period(DateOnly from, DateOnly to) => $"{Day(from)} – {Day(to)}"; /// Parameters as the user reads them: "fast=20;slow=100" becomes "fast 20, slow 100". public static string Parameters(string text) => string.IsNullOrEmpty(text) ? "nessun parametro" : string.Join(", ", text.Split(';').Select(p => p.Replace('=', ' ').Replace('.', ','))); public static string Status(Trial trial, string? progress) => trial.Status switch { TrialStatuses.Planned => "In coda", TrialStatuses.Running => progress is null ? "In corso" : "In corso: " + progress, TrialStatuses.Failed => "Non riuscito", _ => "Completato", }; public static string Verdict(string? verdict) => verdict switch { TrialVerdicts.Passed => "Superato", TrialVerdicts.Rejected => "Non superato", _ => Missing, }; public static string Source(TrialData data) => data.Source == TrialSources.Research ? "storico di ricerca " + Venue(data.Venue) : "barre raccolte di " + Venue(data.Venue); public static string Venue(string venue) => venue switch { "okx" => "OKX", "alpaca" => "Alpaca", "binance" => "Binance", _ => venue, }; public static string Symbols(IReadOnlyList symbols) => symbols.Count <= 4 ? string.Join(", ", symbols) : string.Join(", ", symbols.Take(3)) + $" e altri {symbols.Count - 3}"; /// The value of a check, written in its unit. public static string CheckValue(ReportCheck check, string currency) => check.Value is not { } v ? "non si applica" : check.Unit switch { "money" => Format.Money((decimal)v, currency, withSign: true), "percent" => Percent(v), "count" => Format.Number((decimal)v), _ => Number(v), }; public static string CheckThreshold(ReportCheck check, string currency) => check.Comparison + " " + check.Unit switch { "money" => Format.Money((decimal)check.Threshold, currency), "percent" => Percent(check.Threshold, 0), "count" => Format.Number((decimal)check.Threshold), _ => Number(check.Threshold), }; /// What the engine did not do as asked, in words. public static string Note(string reason) => reason switch { BacktestReasons.NoCash => "Acquisti ridotti per mancanza di liquidità", BacktestReasons.NoPrice => "Ordini senza prezzo di apertura", BacktestReasons.UnknownSymbol => "Strumenti chiesti dalla strategia ma assenti dai dati", BacktestReasons.NoShortSelling => "Vendite allo scoperto rifiutate", BacktestReasons.WrittenOff => "Residui sotto il minimo lasciati dopo un'uscita", BacktestReasons.Dividend => "Dividendi incassati", _ => "Ordini sotto il minimo dello strumento, non inviati", }; }