The laboratory starts with the host (off in demo mode) and runs its queue next to market data and trading. The page lists every trial with status, verdict and headline figures, refreshing itself while one is queued or running, and offers the form for a new test: strategy, data origin and market, instruments, period, initial capital and the grid, with the thresholds stated before the run. The form posts with the antiforgery token; errors come back as a short code. The report page shows the protocol checks, the chosen variant against buy and hold (table and chart), results per year, cost sensitivity, the Deflated Sharpe Ratio, PBO, walk-forward windows, purged CV paths, the look-ahead result, every variant with its zone score, what the backtest does not model, the data used and the trades. A comparison page puts two finished tests side by side and says whether they read the same data. The improvements of 8.7 and the cost estimates of 11.6 have their sections, empty until Phases 9 and 8. The equity chart takes amounts in their own currency and its labels as parameters. The profit factor has no value without a losing trade. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
89 lines
3.9 KiB
C#
89 lines
3.9 KiB
C#
using Anubis.Backtest;
|
||
using Anubis.Core;
|
||
|
||
namespace Anubis.Server;
|
||
|
||
/// <summary>How the laboratory pages write the numbers of a backtest, in plain Italian (SPEC 13.3).</summary>
|
||
internal static class LabText
|
||
{
|
||
public const string Missing = "—";
|
||
|
||
/// <summary>A fraction as a percentage: 0,1234 is "12,3%".</summary>
|
||
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)}";
|
||
|
||
/// <summary>Parameters as the user reads them: "fast=20;slow=100" becomes "fast 20, slow 100".</summary>
|
||
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<string> symbols) =>
|
||
symbols.Count <= 4 ? string.Join(", ", symbols) : string.Join(", ", symbols.Take(3)) + $" e altri {symbols.Count - 3}";
|
||
|
||
/// <summary>The value of a check, written in its unit.</summary>
|
||
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),
|
||
};
|
||
|
||
/// <summary>What the engine did not do as asked, in words.</summary>
|
||
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",
|
||
};
|
||
}
|