An independent review found these, now fixed and tested: - Risk limits could be raised through PUT /api/settings without the phrase and the password. The API now refuses risk keys. - The reference equity counted results twice after reduced to real and after a demotion. Each ledger now keeps a baseline, and carried results stay in the ledger's currency until the reference is computed. - An instrument could be bought by a second strategy in the same account (SPEC 8.6), and purchases under way did not hold their cash, so several purchases sent together could exceed it. Both are refused or shrunk now. - After an automatic trip of the switch there was no way to restart from the interface. Rischio now shows the trip of both worlds and the restart. - The fresh password of SPEC 12.4 could be tried without limit. Five wrong attempts in 15 minutes now lock it. - Observation to simulation skipped the 90 days of live signals. - A new build changed the qualification of a strategy with real money, "Ferma tutto" counted as a critical incident, raising risk ignored demotion conditions in progress, and weekends counted as stock sessions. - The 12.4 confirmation now summarises capital, first purchases and limits, and live strategies show the gap with their simulated twin. The backtest converts each bar with the rate fixed before it opens, the day before, and the report now counts how often correctives and risk changed a decision. crypto_trend is still not passed: the broad universe now falls 32.8%. The outcome file also keeps the qualified parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
308 lines
16 KiB
Plaintext
308 lines
16 KiB
Plaintext
@inherits LoadedComponent
|
|
@page "/rischio"
|
|
@inject AnubisHost Host
|
|
@inject ShellData Shell
|
|
@inject IClock Clock
|
|
@using Anubis.Trading
|
|
@using Anubis.Risk
|
|
|
|
<PageTitle>Rischio · Anubis</PageTitle>
|
|
|
|
@{
|
|
var v = _view;
|
|
var c = _currency;
|
|
}
|
|
@if (_message is { } message)
|
|
{
|
|
<p class="notice notice-loss" role="status"><Icon Name="alert" Size="16" />@message</p>
|
|
}
|
|
<div class="risk-top">
|
|
<section class="panel" aria-labelledby="interruttore">
|
|
<div class="panel-head">
|
|
<h2 id="interruttore">Interruttore d'emergenza</h2>
|
|
<span class="link-state @(v.BotState == "active" ? null : "down")"><span class="dot"></span>@BotText(v.BotState)</span>
|
|
</div>
|
|
@if (v.WaitsForRestart)
|
|
{
|
|
<p class="notice notice-attention" role="status"><Icon Name="alert" Size="16" /><span>
|
|
@if (v.Tripped.Count > 0)
|
|
{
|
|
<text>L'interruttore è scattato @string.Join(" e ", v.Tripped.Select(t => Where(t.World))): il calo dal massimo ha superato il @Format.Percent(v.Limits.DrawdownStop * 100m, 0). </text>
|
|
}
|
|
@if (v.BotState == "stopped")
|
|
{
|
|
<text>Il bot non apre posizioni e non vende per le strategie; gli stop di protezione restano attivi.</text>
|
|
}
|
|
else
|
|
{
|
|
<text>Lì non apro posizioni e non vendo per le strategie; gli stop di protezione restano attivi. Il resto continua a lavorare.</text>
|
|
}
|
|
@if (v.CloseAllPending)
|
|
{
|
|
<text> Sta vendendo tutte le posizioni: ciò che è su un mercato chiuso parte all'apertura.</text>
|
|
}
|
|
</span></p>
|
|
@if (v.StopFile)
|
|
{
|
|
<p class="note">C'è il file <code>STOP</code> nella cartella dei dati: toglilo, poi conferma qui la ripartenza.</p>
|
|
}
|
|
@if (!Shell.IsDemo)
|
|
{
|
|
<form method="post" action="/azioni/bot/riattiva" class="form-inline">
|
|
<AntiforgeryToken />
|
|
<input type="hidden" name="ritorno" value="/rischio" />
|
|
<button class="btn" type="submit" disabled="@v.StopFile" data-confirm="@RestartText(v)">Riparti</button>
|
|
</form>
|
|
}
|
|
}
|
|
@if (v.BotState != "stopped")
|
|
{
|
|
<p>Con <strong>Ferma tutto</strong> annullo gli ordini in attesa e non apro nuove posizioni. Gli stop di protezione restano attivi, così quello che è aperto non rimane scoperto. Per ripartire serve la tua conferma, da qui.</p>
|
|
@if (!Shell.IsDemo)
|
|
{
|
|
<div class="choices">
|
|
<form method="post" action="/azioni/bot/ferma"><AntiforgeryToken /><input type="hidden" name="ritorno" value="/rischio" /><button class="btn btn-danger-solid" type="submit" data-confirm="Ferma tutto: annullo gli ordini in attesa e non apro posizioni finché non riparti da qui. Gli stop restano. Confermi?"><Icon Name="stop" Size="18" />Ferma tutto</button></form>
|
|
<form method="post" action="/azioni/bot/chiudi"><AntiforgeryToken /><input type="hidden" name="ritorno" value="/rischio" /><button class="btn btn-danger" type="submit" data-confirm="Ferma e chiudi: fermo tutto e vendo a mercato ogni posizione del bot. Ciò che è su un mercato chiuso parte all'apertura e intanto tiene il suo stop. Non si annulla. Confermi?">Ferma e chiudi tutte le posizioni</button></form>
|
|
</div>
|
|
}
|
|
<p class="note">Anche un file chiamato <code>STOP</code> nella cartella dei dati ferma tutto entro un minuto.</p>
|
|
}
|
|
</section>
|
|
<section class="panel" aria-labelledby="blocchi">
|
|
<h2 id="blocchi">Blocchi automatici</h2>
|
|
@if (!v.AnyBlock)
|
|
{
|
|
<p class="gain status-line"><Icon Name="check" Size="16" />Nessun blocco attivo</p>
|
|
}
|
|
else
|
|
{
|
|
<ul class="blocks">
|
|
@foreach (var t in v.Tripped)
|
|
{
|
|
<li class="loss"><Icon Name="alert" Size="16" />Interruttore scattato @Where(t.World) @(t.State.TrippedAt is { } at ? "il " + Moment(at) : ""): aspetta la tua conferma.</li>
|
|
}
|
|
@if (v.Risk.State.DailyBlockUntil is { } until && until > Clock.UtcNow)
|
|
{
|
|
<li><Icon Name="clock" Size="16" />Perdita del giorno oltre il limite: niente nuovi ingressi fino a @Moment(until).</li>
|
|
}
|
|
@if (v.Risk.State.WeeklyHalvingUntil is { } half && half > Clock.UtcNow)
|
|
{
|
|
<li><Icon Name="clock" Size="16" />Perdita della settimana oltre il limite: rischio dimezzato fino a @Moment(half).</li>
|
|
}
|
|
@if (v.Risk.Measures.Drawdown <= -v.Limits.DrawdownHalf && !v.Risk.State.Tripped)
|
|
{
|
|
<li><Icon Name="alert" Size="16" />Calo dal massimo oltre il @Format.Percent(v.Limits.DrawdownHalf * 100m, 0): rischio dimezzato.</li>
|
|
}
|
|
@if (v.BotState == "paused")
|
|
{
|
|
<li><Icon Name="pause" Size="16" />Bot in pausa: niente nuovi ingressi, stop e uscite proseguono.</li>
|
|
}
|
|
@if (v.BotState == "stopped" && v.Tripped.Count == 0)
|
|
{
|
|
<li class="loss"><Icon Name="stop" Size="16" />Ferma tutto acceso.</li>
|
|
}
|
|
</ul>
|
|
}
|
|
<dl class="block-list">
|
|
<dt>Prossimo blocco in programma</dt>
|
|
@if (v.NextCalendarBlock is { } next)
|
|
{
|
|
<dd><strong>@Capital(Local(next.From, "dddd d MMMM")), dalle @Local(next.From, "HH:mm") alle @Local(next.To, "HH:mm")</strong><span>@next.Title: in quell'ora non apro posizioni.</span></dd>
|
|
}
|
|
else
|
|
{
|
|
<dd><span>Nessun dato economico importante nei prossimi 14 giorni.</span></dd>
|
|
}
|
|
<dt>Ultimo blocco scattato</dt>
|
|
@if (v.LastCalendarBlock is { } last)
|
|
{
|
|
<dd><strong>@Capital(Local(last.From, "dddd d MMMM")), dalle @Local(last.From, "HH:mm") alle @Local(last.To, "HH:mm")</strong><span>@last.Title.</span></dd>
|
|
}
|
|
else
|
|
{
|
|
<dd><span>Nessuno negli ultimi 14 giorni.</span></dd>
|
|
}
|
|
<dt>Protezione day trade (azioni)</dt>
|
|
<dd><span>@(v.PdtGuard ? "Accesa: al massimo 3 operazioni in giornata ogni 5 sedute." : "Spenta. Si accende in Impostazioni se il conto Alpaca vale meno di 25.000 $.")</span></dd>
|
|
</dl>
|
|
</section>
|
|
</div>
|
|
|
|
<section class="panel" aria-labelledby="limiti">
|
|
<div class="panel-head">
|
|
<h2 id="limiti">I limiti <span class="note">Valgono per tutte le strategie insieme@(v.World == Worlds.Real ? ", sui conti veri" : ", nel mondo simulato"). Si cambiano dalle <a href="/impostazioni#rischio">impostazioni</a>, mai da soli.</span></h2>
|
|
</div>
|
|
<div class="table-wrap">
|
|
<table class="table limits">
|
|
<thead>
|
|
<tr><th scope="col">Limite</th><th scope="col" class="num">Adesso</th><th scope="col" class="wide">Quanto ne sto usando</th><th scope="col" class="num">Soglia</th><th scope="col" class="wide">Se lo supero</th></tr>
|
|
</thead>
|
|
<tbody>
|
|
@foreach (var row in v.Rows)
|
|
{
|
|
<tr>
|
|
<th scope="row">@row.Label</th>
|
|
<td class="num nowrap">@row.Now@if (row.Detail is { } d) {<text> </text><span class="muted">(@d)</span>}</td>
|
|
<td class="wide">
|
|
@if (row.Usage is { } u)
|
|
{
|
|
<span class="usage @(u >= 0.8m ? "high" : null)" role="img" aria-label="@Format.Percent(u * 100m, 0) del limite"><span style="width: @Css(u * 100m)%"></span></span>
|
|
}
|
|
</td>
|
|
<td class="num nowrap">@row.Threshold</td>
|
|
<td class="wide">@row.Beyond</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</section>
|
|
|
|
<div class="columns">
|
|
<section class="panel" aria-labelledby="calo">
|
|
<h2 id="calo">Quanto è sceso dal massimo</h2>
|
|
<p class="note">Ogni fotografia del patrimonio@(v.Risk.State.PeakFrom is { } from ? " dal " + Local(from, "d MMMM") : " dall'avvio"). A zero vuol dire che il portafoglio era al suo punto più alto.</p>
|
|
@if (v.Drawdown.Count < 2)
|
|
{
|
|
<p class="note">Ancora troppo poche fotografie per disegnare la curva.</p>
|
|
}
|
|
else
|
|
{
|
|
var worst = v.Drawdown.MinBy(p => p.Drawdown)!;
|
|
var floor = Math.Min(-0.01m, worst.Drawdown * 1.2m);
|
|
var first = v.Drawdown[0].At;
|
|
var span = Math.Max(1d, (v.Drawdown[^1].At - first).TotalSeconds);
|
|
string X(DateTimeOffset at) => Css((decimal)((at - first).TotalSeconds / span) * 560m + 30m);
|
|
string Y(decimal d) => Css(10m + d / floor * 150m);
|
|
var points = string.Join(' ', v.Drawdown.Select(p => X(p.At) + "," + Y(p.Drawdown)));
|
|
<svg class="drawdown" viewBox="0 0 600 190" role="img" aria-label="Calo dal massimo: il peggiore @Format.Percent(worst.Drawdown * 100m, 2) il @Local(worst.At, "d MMMM")">
|
|
<line x1="30" x2="590" y1="10" y2="10" class="axis" />
|
|
<text x="0" y="14" class="tick">0%</text>
|
|
<text x="0" y="164" class="tick">@Format.Percent(floor * 100m, 1)</text>
|
|
<polygon points="30,10 @points @X(v.Drawdown[^1].At),10" class="area" />
|
|
<polyline points="@points" class="line" />
|
|
<circle cx="@X(worst.At)" cy="@Y(worst.Drawdown)" r="4" class="dot" />
|
|
<text x="@X(worst.At)" y="@(Css(Math.Min(180m, 26m + worst.Drawdown / floor * 150m)))" text-anchor="middle" class="label">@Format.Percent(worst.Drawdown * 100m, 2) il @Local(worst.At, "d MMM")</text>
|
|
</svg>
|
|
<p class="note">
|
|
@if (-worst.Drawdown < v.Limits.DrawdownHalf / 5m)
|
|
{
|
|
<text>Le soglie del @Format.Percent(v.Limits.DrawdownHalf * 100m, 0) e del @Format.Percent(v.Limits.DrawdownStop * 100m, 0) sono fuori scala: il calo peggiore è rimasto sotto un quinto del limite.</text>
|
|
}
|
|
else
|
|
{
|
|
<text>A @Format.Percent(-v.Limits.DrawdownHalf * 100m, 0) il rischio si dimezza, a @Format.Percent(-v.Limits.DrawdownStop * 100m, 0) il bot si ferma e aspetta te.</text>
|
|
}
|
|
</p>
|
|
}
|
|
</section>
|
|
<section class="panel" aria-labelledby="scenari">
|
|
<h2 id="scenari">Se succedesse domani</h2>
|
|
<p class="note">Quanto perderei con le posizioni di adesso. Gli scenari si cambiano in <a href="/impostazioni#rischio">Impostazioni</a>.</p>
|
|
@if (!v.HasPositions)
|
|
{
|
|
<p class="note">Nessuna posizione aperta: oggi nessuno scenario costerebbe nulla.</p>
|
|
}
|
|
else
|
|
{
|
|
<div class="table-wrap">
|
|
<table class="table">
|
|
<thead><tr><th scope="col">Scenario</th><th scope="col" class="num">Senza stop</th><th scope="col" class="num">Con gli stop</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var s in v.Scenarios)
|
|
{
|
|
<tr>
|
|
<th scope="row">@s.Scenario.Label</th>
|
|
<td class="num nowrap"><strong>@Shell.Money(s.WithoutStops, c, decimals: 0)</strong> <span class="muted">(@Format.Percent(s.WithoutStopsShare * 100m, 1, withSign: true))</span></td>
|
|
<td class="num nowrap muted">@(s.SameWithStops ? "uguale" : "circa " + Shell.Money(s.WithStops ?? 0m, c, decimals: 0))</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<p class="notice notice-attention"><Icon Name="alert" Size="16" />Gli stop non sono una garanzia: in un crollo improvviso il prezzo può saltarli. E nessuno stop protegge dal cambio.</p>
|
|
}
|
|
</section>
|
|
</div>
|
|
|
|
<div class="columns">
|
|
<section aria-labelledby="mai" class="plain">
|
|
<h2 id="mai">Cosa il bot non può fare, mai</h2>
|
|
<ul class="never">
|
|
<li><Icon Name="ban" Size="16" />Usare soldi presi in prestito o vendere allo scoperto</li>
|
|
<li><Icon Name="ban" Size="16" />Prelevare o spostare denaro: le chiavi dei conti non lo permettono</li>
|
|
<li><Icon Name="ban" Size="16" />Toccare posizioni che non ha aperto lui</li>
|
|
<li><Icon Name="ban" Size="16" />Passare ai soldi veri senza la tua conferma</li>
|
|
<li><Icon Name="ban" Size="16" />Rischiare di più per recuperare una perdita</li>
|
|
<li><Icon Name="ban" Size="16" />Comprare di più perché lo suggerisce una notizia o il modello</li>
|
|
</ul>
|
|
</section>
|
|
<section aria-labelledby="eventi" class="plain">
|
|
<div class="panel-head">
|
|
<h2 id="eventi">Eventi di rischio</h2>
|
|
@if (v.Events.Count == 0)
|
|
{
|
|
<span class="note">Nessun limite superato dall'avvio</span>
|
|
}
|
|
</div>
|
|
@if (v.Events.Count > 0)
|
|
{
|
|
<dl class="events">
|
|
@foreach (var e in v.Events)
|
|
{
|
|
<dt>@Local(e.At, "d MMMM")</dt>
|
|
<dd class="@(e.Severity is RiskSeverities.Critical or RiskSeverities.High ? "loss" : null)">@e.Message</dd>
|
|
}
|
|
</dl>
|
|
<a href="/diario">Vedi tutto nel diario</a>
|
|
}
|
|
</section>
|
|
</div>
|
|
|
|
@code {
|
|
private RiskView _view = null!;
|
|
private string _currency = "EUR";
|
|
private string? _message;
|
|
|
|
[CascadingParameter]
|
|
public HttpContext? HttpContext { get; set; }
|
|
|
|
[SupplyParameterFromQuery(Name = "errore")]
|
|
public string? Errore { get; set; }
|
|
|
|
protected override async Task LoadAsync()
|
|
{
|
|
var ct = HttpContext?.RequestAborted ?? CancellationToken.None;
|
|
var services = Host.Services!;
|
|
_currency = Shell.Effective((await services.Settings.GetAsync(SettingsStore.Currency.Key, ct)).Value);
|
|
_view = await RiskView.LoadAsync(services, Clock, Shell.Zone, ct);
|
|
_message = Errore switch
|
|
{
|
|
"file-stop" => "Il file STOP è ancora nella cartella dei dati: toglilo prima di ripartire.",
|
|
"bot" => "Comando non ammesso nello stato attuale del bot.",
|
|
_ => null,
|
|
};
|
|
}
|
|
|
|
private static string BotText(string state) => state switch
|
|
{
|
|
"active" => "Il bot sta operando",
|
|
"paused" => "In pausa",
|
|
_ => "Fermo",
|
|
};
|
|
|
|
private static string Where(string world) => world == Worlds.Real ? "sui conti veri" : "nel mondo simulato";
|
|
|
|
private static string RestartText(RiskView v) => v.Tripped.Count > 0
|
|
? "Riparti: il massimo di riferimento riparte dal patrimonio di adesso e il bot torna a operare. Confermi?"
|
|
: "Riparti: il bot torna a operare. Confermi?";
|
|
|
|
private static string Css(decimal value) => value.ToString("0.##", CultureInfo.InvariantCulture);
|
|
|
|
private string Local(DateTimeOffset at, string format) => TimeZoneInfo.ConvertTime(at, Shell.Zone).ToString(format, CultureInfo.GetCultureInfo("it-IT"));
|
|
|
|
private string Moment(DateTimeOffset at) => Local(at, "d MMMM 'alle' HH:mm");
|
|
|
|
private static string Capital(string text) => text.Length == 0 ? text : char.ToUpper(text[0], CultureInfo.GetCultureInfo("it-IT")) + text[1..];
|
|
}
|