News, the economic calendar and earnings dates no longer change any decision (ADR 0023, choices in ADR 0027). The outside rules are measured in the shadow; a switch in the settings turns them back on. refs #116, refs #117 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
347 lines
18 KiB
Plaintext
347 lines
18 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.ProtectedRestart is { } restart)
|
|
{
|
|
<div class="notice notice-attention" role="status"><Icon Name="alert" Size="16" /><div>
|
|
<p><strong>Ripartenza protetta.</strong> Il database è cambiato il @Moment(restart.Since) (ripristino, importazione o copia più vecchia del conto): prima di ripartire guarda cosa ho ricostruito.</p>
|
|
<ul>
|
|
@foreach (var line in restart.Rebuilt)
|
|
{
|
|
<li>@line</li>
|
|
}
|
|
@if (restart.Working)
|
|
{
|
|
<li>Sto ancora leggendo i conti.</li>
|
|
}
|
|
else if (restart.Waiting.Count > 0)
|
|
{
|
|
<li>Non ho ancora letto: @string.Join(", ", restart.Waiting). Riprovo ogni minuto; se una chiave è scaduta, cambiala nelle Impostazioni.</li>
|
|
}
|
|
else if (restart.Rebuilt.Count == 0)
|
|
{
|
|
<li>Sui conti non c'era niente che il database non conoscesse già.</li>
|
|
}
|
|
@if (restart.ToReconfirm.Count > 0)
|
|
{
|
|
<li>Da riconfermare nella pagina Strategie, altrimenti restano in sola uscita: @string.Join(", ", restart.ToReconfirm).</li>
|
|
}
|
|
</ul>
|
|
</div></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 || v.ProtectedRestart is { CanRestart: false })" data-confirm="@RestartText(v)">Riparti</button>
|
|
</form>
|
|
@if (v.BotState == "stopped" && !v.CloseAllPending && v.ProtectedRestart is not { CanRestart: false })
|
|
{
|
|
<p class="note">Se preferisci uscire da tutto, puoi vendere le posizioni adesso, senza ripartire.</p>
|
|
<form method="post" action="/azioni/bot/chiudi"><AntiforgeryToken /><input type="hidden" name="ritorno" value="/rischio" /><button class="btn btn-danger" type="submit" data-confirm="Vendo a mercato ogni posizione del bot, che resta fermo. Ciò che è su un mercato chiuso parte all'apertura e intanto tiene il suo stop. Non si annulla. Confermi?">Chiudi tutte le posizioni</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>@(v.RulesMeasured ? "Prossimo dato macro (solo registrato)" : "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: @(v.RulesMeasured ? "con le regole attive in quell'ora non aprirei posizioni; ora le apro come sempre." : "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.RulesMeasured)
|
|
{
|
|
<dd><span>Nessun blocco: le regole delle notizie e del calendario sono solo misurate.</span></dd>
|
|
}
|
|
else 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 Alpaca applica ancora al tuo conto la regola sulle operazioni in giornata.")</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.ProtectedRestart is not null
|
|
? "Riparti dopo il ripristino: hai controllato posizioni e operazioni ricostruite? Il bot torna a operare; le strategie con soldi veri restano in sola uscita finché non le riconfermi. Confermi?"
|
|
: 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..];
|
|
}
|