@inherits LoadedComponent @page "/conti" @inject AnubisHost Host @inject ShellData Shell @inject IClock Clock @using Anubis.Trading @using Anubis.Data Conti e posizioni · Anubis @{ var v = _view; var c = _currency; } @if (!v.HasAccounts) { Il bot crea da solo i due simulatori interni, OKX e Alpaca, al primo avvio del motore di trading. I conti di prova dei broker compaiono quando ne aggiungi le chiavi in Impostazioni. } else { @if (_message is { } message) {

@message

} var shares = Format.Shares([.. v.Slices.Select(s => s.Eur), v.CashEur]); var winners = v.Positions.Count(p => p.ResultEur >= 0);
Totale dei conti @Shell.Money(v.TotalEur, c) @Shell.Money(v.ResultEur, c, withSign: true) dall'avvio
Investito @Shell.Money(v.InvestedEur, c) @Format.Percent(Part(v.InvestedEur, v.TotalEur)) del totale
Liquidità @Shell.Money(v.CashEur, c) @Format.Percent(Part(v.CashEur, v.TotalEur)) del totale
Risultato delle posizioni aperte @Shell.Money(v.OpenResultEur, c, withSign: true) @(v.Positions.Count == 0 ? "Nessuna posizione aperta" : $"{winners} in guadagno, {v.Positions.Count - winners} in perdita")
@foreach (var a in v.Accounts) { var state = a.State; var up = a.Result >= 0; var usd = a.Currency == "USD";
@if (state.IsPrudent) {
@if (state.ModeReason == Reconciler.KeyReason) {

La chiave di questo conto permette i prelievi: niente nuovi acquisti finché non la sostituisci con una senza prelievo in Impostazioni. Vendite e stop proseguono.

} else {

Modalità prudente dalle @Time(state.ModeSince): niente nuovi acquisti, vendite e stop proseguono.

    @foreach (var d in state.Differences) {
  • @d.Message
  • }
@if (state.WaitsForUser(Clock.UtcNow, TimeSpan.FromMinutes(15)) && !Shell.IsDemo) {

Sono passati 15 minuti e la differenza non si è spiegata da sola. Scegli tu:

} else {

Se entro 15 minuti si spiega da sola il conto torna normale; altrimenti qui potrai scegliere se allineare il registro o lasciare fuori la differenza.

} }
}
}
var shownPositions = v.Positions.Where(p => _filter is null || p.Broker == _filter).ToList();

Posizioni aperte

Esporta CSV
@if (shownPositions.Count == 0) {

Nessuna posizione aperta@(_filter is null ? "" : " su questo conto").

} else {
@foreach (var p in shownPositions) { var gain = p.ResultEur >= 0; }
Strumento Conto Strategia Quantità Prezzo medio Prezzo ora Valore Risultato Stop Aperta da
@p.Symbol @p.Name @(p.Broker == Venues.Alpaca ? "Alpaca" : "OKX") @StrategyNames.Label(p.Strategy) @Format.Quantity(p.Quantity) @Format.Money(p.Average, p.Currency) @Format.Money(p.Last, p.Currency) @Shell.Money(p.ValueEur, c) @Shell.Money(p.ResultEur, c, withSign: true) (@Format.Percent(p.Percent, 2, withSign: true)) @(p.Stop is { } stop ? Format.Money(stop, p.Currency, decimals: AccountsView.StopDecimals(stop)) : "—") @Age(p.OpenedAt)
Totale @Shell.Money(shownPositions.Sum(p => p.ValueEur), c) @Shell.Money(shownPositions.Sum(p => p.ResultEur), c, withSign: true)
}

Ordini in attesa

@(v.PendingOthers == 0 ? (v.Pending.Count == 0 ? "Nessun ordine aperto" : "Solo stop di protezione: nessun acquisto in coda") : $"{v.PendingOthers} {(v.PendingOthers == 1 ? "ordine" : "ordini")} in corso oltre agli stop")
@if (v.Pending.Count > 0) {
@foreach (var o in v.Pending) { }
OrdineDoveScatta aStato
@o.Action @o.Subject @o.Where @o.Level @if (o.Waiting) {} else {}@o.Status
} @if (v.AlpacaStops) {

Le azioni intere hanno uno stop permanente sul broker, che resta attivo anche a bot spento. Sulle frazioni il broker accetta solo ordini che valgono un giorno: quelli li rimetto a ogni apertura e, a mercato chiuso, li sorveglio io.

}

In quale valuta sono i soldi

@{ var dollars = v.Accounts.Where(a => a.Currency == "USD").Sum(a => a.Eur(a.Equity)); var euros = v.TotalEur - dollars; var parts = Format.Shares([dollars, euros]); }
Dollari (conto Alpaca)
@Shell.Money(dollars, c) @Format.Percent(parts[0])
Euro e crypto (conto OKX)
@Shell.Money(euros, c) @Format.Percent(parts[1])

@RateText(v)@(v.FxEffect is { } fx && fx != 0 ? $" Effetto del cambio sul risultato in euro: {Format.Money(fx, "EUR", withSign: true)}." : "")

@if (dollars > 0) {

Se il dollaro perde il 5% sull'euro, il totale scende di circa @Format.Money(Math.Round(dollars * 0.05m), "EUR", decimals: 0) anche a prezzi fermi.

}
@if (v.Residues.Count > 0) {

Residui sotto il minimo vendibile

Restano sul conto ma non sono più del bot: tolti dal registro al valore di mercato
@foreach (var r in v.Residues) { }
StrumentoContoQuantitàValoreDal
@r.Symbol@r.Broker@Format.Quantity(r.Quantity)@Format.Money(r.Value, r.Currency)@Format.ShortDate(r.At, Shell.Now, Shell.Zone, withTime: false)
} } @code { private AccountsView _view = null!; private string _currency = "EUR"; private string? _filter; private string? _message; private bool _ok; private Dictionary? _latency; [CascadingParameter] public HttpContext? HttpContext { get; set; } [SupplyParameterFromQuery(Name = "conto")] public string? Conto { get; set; } [SupplyParameterFromQuery(Name = "ok")] public string? Ok { 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 AccountsView.LoadAsync(services, Clock, Shell.Zone, ct); _filter = Conto is Venues.Okx or Venues.Alpaca ? Conto : null; (_message, _ok) = (Ok, Errore) switch { ("allinea", _) => ("Fatto: il registro ha preso i numeri del conto e il conto è tornato normale.", true), ("escludi", _) => ("Fatto: la differenza resta fuori dal registro e il conto è tornato normale.", true), (_, "motore") => ("Il motore di trading non è attivo: riprova tra poco.", false), (_, "chiave") => ("Una chiave che permette i prelievi non si allinea: va sostituita in Impostazioni.", false), (_, "presto") => ("La scelta si fa dopo 15 minuti di modalità prudente: prima la differenza può ancora spiegarsi da sola.", false), (_, { }) => ("Non è stato possibile completare la scelta: riprova.", false), _ => (null, false), }; using var aggregates = Shell.Aggregates(); if (aggregates is not null) { var names = aggregates.RootElement.GetProperty("conti").EnumerateArray().Select(x => x.GetProperty("risposta_ms").GetInt32()).ToList(); _latency = _view.Accounts.Select((a, i) => (a.Id, i)).Where(x => x.i < names.Count).ToDictionary(x => x.Id, x => names[x.i]); } } private string Connection(AccountCard a) { if (a.State.LastOutcome == ReconcileOutcomes.Unreachable) { return "Il broker non risponde"; } if (_latency?.GetValueOrDefault(a.Id) is > 0 and var ms) { return $"Connesso, risposta in {ms} ms"; } return a.Environment == TradingEnvironments.Simulation ? "Attivo, nel simulatore" : "Connesso"; } private static string Holdings(AccountCard a) => (a.Broker, a.Holdings) switch { (_, 0) => "nulla", (Venues.Alpaca, 1) => "1 azione", (Venues.Alpaca, var n) => $"{n} azioni", (_, 1) => "1 moneta", (_, var n) => $"{n} monete", }; private static string RateText(AccountsView v, bool withSource = true) => v.UsdRate is { } rate ? $"Cambio usato: 1 € = {Format.Quantity(rate)} $" + (withSource ? $" (BCE, {AccountsView.DayMonth(new DateTimeOffset(v.RateDay!.Value.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero))})." : ".") : "Manca ancora il cambio euro/dollaro della BCE: i dollari restano nella loro valuta."; private static decimal Part(decimal part, decimal total) => total == 0 ? 0 : part / total * 100m; private static string Css(decimal percent) => percent.ToString("0.###", CultureInfo.InvariantCulture); private string Time(DateTimeOffset? moment) => moment is { } m ? TimeZoneInfo.ConvertTime(m, Shell.Zone).ToString("HH:mm", CultureInfo.InvariantCulture) : "—"; private string Age(DateTimeOffset opened) { var days = (int)Math.Floor((Shell.Now - opened).TotalDays); return days switch { < 1 => "oggi", 1 => "1 giorno", _ => $"{days} giorni", }; } }