@inject ShellData Shell @* Equity against the "senza bot" comparison (SPEC 13.4): one scale, 2 px lines, faint grid, direct label on the last point, legend, and the same numbers in a table. Lines are SVG stretched to the box; texts and the end point are HTML placed in percent, so nothing gets distorted at any width. *@ @if (Points.Count >= 2) { var values = Points.Select(p => Value(p.Equity)).ToList(); var bench = Points.Select(p => p.Benchmark is { } b ? Value(b) : (decimal?)null).ToList(); var all = values.Concat(bench.OfType()).ToList(); var (lo, hi, step) = Scale(all.Min(), all.Max()); double X(int i) => i * 1000.0 / (Points.Count - 1); double Y(decimal v) => (double)((hi - v) / (hi - lo)) * 1000.0; string Line(IEnumerable<(int I, decimal V)> pts) => string.Join(" ", pts.Select(p => Fmt(X(p.I)) + "," + Fmt(Y(p.V)))); var equityLine = Line(values.Select((v, i) => (i, v))); var benchLine = Line(bench.Select((v, i) => (i, v)).Where(p => p.v is not null).Select(p => (p.i, p.v!.Value))); var last = values[^1];
@if (bench[^1] is { } lastBench) { // Two labels never overlap: the lower one moves down when they are closer than one line. var gap = Math.Abs(Y(last) - Y(lastBench)) / 10; var benchTop = gap < 7 ? Y(last) / 10 + (Y(lastBench) >= Y(last) ? 7 : -7) : Y(lastBench) / 10; @Text(Points[^1].Benchmark!.Value, 0) } @Text(Points[^1].Equity, 0)
@EquityLabel @if (benchLine.Length > 0) { @BenchmarkLabel }
Vedi i numeri in una tabella
@foreach (var p in Points.AsEnumerable().Reverse()) { }
Giorno@EquityColumn@BenchmarkColumn
@Format.LongDate(p.Day)@Text(p.Equity)@(p.Benchmark is { } b ? Text(b) : "—")
} @code { [Parameter, EditorRequired] public IReadOnlyList Points { get; set; } = []; [Parameter] public string Currency { get; set; } = "EUR"; /// True when the amounts are already in (a backtest report): no conversion from euro. [Parameter] public bool Native { get; set; } [Parameter] public string Label { get; set; } = "Patrimonio nel tempo, con il bot e senza"; [Parameter] public string EquityLabel { get; set; } = "Con Anubis"; [Parameter] public string BenchmarkLabel { get; set; } = "Senza bot: Bitcoin e S&P 500 comprati e tenuti"; [Parameter] public string EquityColumn { get; set; } = "Con il bot"; [Parameter] public string BenchmarkColumn { get; set; } = "Senza bot"; private decimal Value(decimal amount) => Native ? amount : Shell.Convert(amount, Currency); private string Text(decimal amount, int decimals = 2) => Native ? Format.Money(amount, Currency, decimals: decimals) : Shell.Money(amount, Currency, decimals: decimals); private static string Fmt(double v) => v.ToString("0.##", CultureInfo.InvariantCulture); private static string Pct(double v) => v.ToString("0.###", CultureInfo.InvariantCulture) + "%"; // Round steps (1, 2, 2.5, 3, 5 times a power of ten) giving three to five grid lines. internal static (decimal Lo, decimal Hi, decimal Step) Scale(decimal min, decimal max) { if (max <= min) { max = min + 1; } var raw = (double)(max - min) / 4; var power = Math.Pow(10, Math.Floor(Math.Log10(raw))); var step = new[] { 1, 2, 2.5, 3, 5, 10 }.Select(m => m * power).First(s => s >= raw); var stepValue = (decimal)step; var lo = Math.Floor(min / stepValue) * stepValue; var hi = Math.Ceiling(max / stepValue) * stepValue; return (lo, hi == lo ? lo + stepValue : hi, stepValue); } }