Add the Conti screen, account keys and entrusted capital (refs #32 #29 #31)

The Conti e posizioni screen follows mockup 02: totals split by strategy,
one card per account with key permissions and the last reconciliation,
positions with a per-account filter and CSV export, pending stops (Alpaca
grouped into permanent and day stops), residues, and exposure by currency.
It shows one world only, real accounts when there are any, so simulated
and real money never add up. Position values are rounded to the cent per
row, as the table shows them, so the figures add up on screen.

Settings gain "Conti e chiavi": practice keys are asked to the broker
before they are saved and refused when they may withdraw or cannot trade;
they are never shown again and the engine restarts to use them. The
capital entrusted to each broker is set there in euro. Until any capital
is set, the overview points to that section as the first step.

The overview now reads one world, nets capital flows out of its results
and uses the time-weighted index; open orders exclude protective stops.

The demo seed gains cash rows, closed trades, stops, the ECB rate and the
reconciliation state, so the ledger itself computes the mockup's figures.
Screenshots now capture the whole page and include Conti.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 21:16:42 +01:00
1 parent d2441df592
commit 4c4f848d80
18 files changed
+1472 -33

No files matched your search

+163
View File
@@ -30,6 +30,10 @@ internal static partial class Forms
signed.MapPost("/impostazioni/{key}", SetSettingAsync);
signed.MapPost("/bot/{command}", BotAsync);
signed.MapPost("/salvataggio", BackupAsync);
signed.MapPost("/conti/{id:guid}/{choice}", ResolveAccountAsync);
signed.MapPost("/chiavi/{broker}", SaveKeysAsync);
signed.MapPost("/chiavi/{broker}/togli", RemoveKeysAsync);
signed.MapPost("/capitale/{broker}", SetCapitalAsync);
}
private static string Back(IFormCollection form, string fallback)
@@ -172,6 +176,165 @@ internal static partial class Forms
return Results.Redirect(ok ? back : With(back, "errore", "bot"));
}
/// <summary>The user's choice for an account in prudent mode after the 15 minutes (SPEC 7.4): align or leave out.</summary>
private static async Task<IResult> ResolveAccountAsync(Guid id, string choice, AnubisHost host, HttpContext context, CancellationToken ct)
{
if (choice is not ("allinea" or "escludi"))
{
return Results.Redirect("/conti?errore=scelta");
}
if (host.IsDemo || host.Services?.Trading is not { } trading || trading.ManagerOf(id) is not { } manager)
{
return Results.Redirect("/conti?errore=motore");
}
var state = await trading.Reconciler.StateAsync(id, ct).ConfigureAwait(false);
if (state.ModeReason == Anubis.Trading.Reconciler.KeyReason)
{
return Results.Redirect("/conti?errore=chiave");
}
await trading.Reconciler.ResolveAsync(manager, choice == "allinea" ? Anubis.Trading.ReconcileChoice.Align : Anubis.Trading.ReconcileChoice.Exclude,
context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
return Results.Redirect("/conti?ok=" + choice);
}
/// <summary>
/// New practice keys for a broker (SPEC 13.5): asked to the broker first, kept only if they may trade and may not
/// withdraw (SPEC 2.2), saved encrypted, never shown again. The engine then restarts to use them.
/// </summary>
private static async Task<IResult> SaveKeysAsync(string broker, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
{
const string back = "/impostazioni";
if (broker is not (Anubis.Data.Venues.Okx or Anubis.Data.Venues.Alpaca))
{
return Results.Redirect(back + "?errore=conto#conti");
}
if (host.IsDemo || host.Services is not { Trading: { } trading } services)
{
return Results.Redirect(back + "?errore=motore#conti");
}
var fields = Anubis.Trading.TradingService.KeyFields(broker);
foreach (var (name, _) in fields)
{
if ((await services.Secrets.StatusAsync(name, ct).ConfigureAwait(false)).FromEnvironment)
{
return Results.Redirect(back + "?errore=ambiente#conti");
}
}
var values = fields.Select(f => form[f.Name].ToString().Trim()).ToList();
if (values.Any(v => v.Length == 0 || v.Length > 512))
{
return Results.Redirect(back + "?errore=chiavi-vuote#conti");
}
Anubis.Trading.KeyPermissions permissions;
try
{
permissions = await trading.TestKeysAsync(broker, values, ct).ConfigureAwait(false);
}
catch (Anubis.Trading.BrokerException)
{
return Results.Redirect(back + "?errore=chiavi-rifiutate#conti");
}
if (permissions.CanWithdraw)
{
return Results.Redirect(back + "?errore=prelievo#conti");
}
if (!permissions.CanTrade)
{
return Results.Redirect(back + "?errore=sola-lettura#conti");
}
var who = context.User.Identity?.Name ?? "utente";
for (var i = 0; i < fields.Count; i++)
{
await services.Secrets.SetAsync(fields[i].Name, values[i], who, ct).ConfigureAwait(false);
}
await host.RestartServicesAsync(ct).ConfigureAwait(false);
await WaitBrieflyAsync(host).ConfigureAwait(false);
return Results.Redirect(back + "?ok=chiavi#conti");
}
private static async Task<IResult> RemoveKeysAsync(string broker, AnubisHost host, HttpContext context, CancellationToken ct)
{
const string back = "/impostazioni";
if (broker is not (Anubis.Data.Venues.Okx or Anubis.Data.Venues.Alpaca) || host.IsDemo || host.Services is not { } services)
{
return Results.Redirect(back + "?errore=conto#conti");
}
var who = context.User.Identity?.Name ?? "utente";
foreach (var (name, _) in Anubis.Trading.TradingService.KeyFields(broker))
{
await services.Secrets.RemoveAsync(name, who, ct).ConfigureAwait(false);
}
await host.RestartServicesAsync(ct).ConfigureAwait(false);
await WaitBrieflyAsync(host).ConfigureAwait(false);
return Results.Redirect(back + "?ok=chiavi-tolte#conti");
}
/// <summary>The capital entrusted to a broker, in euro (SPEC 7.1, 8.6): only the user sets it.</summary>
private static async Task<IResult> SetCapitalAsync(string broker, IFormCollection form, AnubisHost host, HttpContext context, CancellationToken ct)
{
const string back = "/impostazioni";
if (broker is not (Anubis.Data.Venues.Okx or Anubis.Data.Venues.Alpaca))
{
return Results.Redirect(back + "?errore=conto#conti");
}
if (host.IsDemo || host.Services?.Trading is not { } trading)
{
return Results.Redirect(back + "?errore=motore#conti");
}
if (ParseEuro(form["euro"].ToString()) is not { } amount || amount < 0m || amount > 100_000_000m)
{
return Results.Redirect(back + "?errore=importo#conti");
}
try
{
await trading.SetCapitalAsync(broker, amount, context.User.Identity?.Name ?? "utente", ct).ConfigureAwait(false);
}
catch (Anubis.Trading.CapitalException ex) when (ex.Message.StartsWith("Manca il cambio", StringComparison.Ordinal))
{
return Results.Redirect(back + "?errore=cambio#conti");
}
catch (Anubis.Trading.CapitalException)
{
return Results.Redirect(back + "?errore=capitale#conti");
}
return Results.Redirect(back + "?ok=capitale#conti");
}
/// <summary>An amount as people type it: "1500", "1.500", "1500,50", "1.500,50", "1500.50".</summary>
internal static decimal? ParseEuro(string text)
{
var clean = (text ?? "").Replace(" ", "", StringComparison.Ordinal).Replace("€", "", StringComparison.Ordinal);
if (clean.Contains(',', StringComparison.Ordinal))
{
clean = clean.Replace(".", "", StringComparison.Ordinal).Replace(',', '.');
}
else if (clean.LastIndexOf('.') is var dot and >= 0 && clean.Length - dot - 1 == 3)
{
clean = clean.Replace(".", "", StringComparison.Ordinal);
}
return decimal.TryParse(clean, System.Globalization.NumberStyles.AllowDecimalPoint, System.Globalization.CultureInfo.InvariantCulture, out var value)
? Math.Round(value, 2, MidpointRounding.ToZero)
: null;
}
private static async Task<IResult> BackupAsync(IFormCollection form, AnubisHost host, ILogger<AnubisHost> logger, CancellationToken ct)
{
var services = host.Services!;