diff --git a/docs/adr/0009-scelte-fase-3.md b/docs/adr/0009-scelte-fase-3.md index 47b8adf..a5ade1b 100644 --- a/docs/adr/0009-scelte-fase-3.md +++ b/docs/adr/0009-scelte-fase-3.md @@ -32,16 +32,20 @@ La Fase 3 collega Anubis ai conti: un'interfaccia comune per i broker, il simula **8. Gli stop.** - Ogni posizione ha uno stop sul broker. Se il prezzo non si conosce, è vecchio o il mercato è chiuso, lo stop si mette comunque: meglio uno stop in più che una posizione scoperta. - Alpaca: stop permanente (`gtc`) sulle azioni intere; sulle frazioni il broker accetta solo ordini `day`, che il gestore rimette dopo la scadenza. A mercato chiuso restano "in coda per l'apertura" e intanto li sorveglia Anubis. -- Un ordine OKX annullato e sconosciuto al broker da più di 2 ore si considera annullato. +- Un ordine OKX annullato e sconosciuto al broker da più di 2 ore si considera annullato. Conta come "sconosciuto" solo la risposta 51603 ("l'ordine non esiste"): ogni altro errore si riprova, così un ordine perso non parte due volte. +- Se manca il prezzo, una vendita aspetta con gli stop ancora sul broker: si tolgono solo un attimo prima che parta. Un acquisto senza prezzo si lascia cadere. +- Su Alpaca un acquisto toglie gli stop di tutte le strategie sullo stesso titolo, perché Alpaca rifiuta l'acquisto se c'è un qualsiasi stop di vendita, e li rimette quando l'acquisto finisce. +- Una vendita rifiutata non si abbandona, ma si riprova con pause crescenti: 1, 2, 4, 8 minuti, poi ogni 15. La riconciliazione cerca la causa. **9. Contabilità per strategia, in sola aggiunta.** - `fills`, `lots` e `cash_ledger` non si modificano mai: un errore si corregge con una riga di storno. Un test cerca `UPDATE` e `DELETE` su queste tabelle in tutto il codice. -- Il capitale affidato si decide in euro per broker e si divide tra le strategie secondo le quote. Alpaca lo converte al cambio BCE del giorno e da lì tiene il registro in dollari. +- Il capitale affidato si decide in euro per broker e si divide tra le strategie secondo le quote. Alpaca lo converte al cambio BCE del giorno e da lì tiene il registro in dollari. Quando l'importo cambia, si converte solo la differenza in euro: i dollari già affidati restano quelli, così un movimento del cambio non diventa mai un versamento o un prelievo. Salvare lo stesso importo non cambia nulla. - Ridurre il capitale sotto la liquidità libera di una strategia è rifiutato: il bot non vende per restituire capitale. -- I dividendi vanno a chi aveva il titolo alla chiusura prima della data di stacco, calcolato dalle esecuzioni. Caso limite noto: un frazionamento tra l'acquisto e lo stacco. +- I dividendi vanno a chi aveva il titolo alla chiusura prima della data di stacco, calcolato dalle esecuzioni. Caso limite noto: un frazionamento tra l'acquisto e lo stacco. Ogni pagamento ha il suo segno di "già fatto" nella stessa scrittura, così un errore a metà non paga due volte. +- Un frazionamento moltiplica solo le azioni tenute prima della data di stacco: quelle comprate dopo sono già azioni nuove. Un frazionamento vecchio, di quando la strategia non aveva il titolo, non cambia nulla. Il simulatore applica lo stesso frazionamento al suo libro, una volta, insieme al registro. **10. Patrimonio e rendimenti.** -- Ogni 5 minuti una fotografia per strategia, per conto e per mondo (circa 7 righe). +- Ogni 5 minuti una fotografia per strategia, per conto e per mondo (circa 7 righe). Prima di ogni fotografia le posizioni prendono il prezzo di mercato (il medio di una quotazione recente); senza quotazione resta l'ultimo prezzo noto. - Mondo simulato e mondo reale non si sommano mai: Panoramica e Conti mostrano un solo mondo, quello reale se esiste un conto reale, altrimenti quello simulato. - I risultati tolgono i versamenti e i prelievi di capitale; la percentuale è ponderata nel tempo (TWR). - Le carte dei conti in dollari usano l'ultimo cambio; l'effetto del cambio si mostra a parte. @@ -50,14 +54,14 @@ La Fase 3 collega Anubis ai conti: un'interfaccia comune per i broker, il simula **11. Riconciliazione.** - Ogni 5 minuti registro e conto si confrontano. Sul simulatore si controllano solo la coerenza interna e gli stop. - Una differenza che si spiega da sola (esecuzione in ritardo, frazionamento, stop da rimettere) si sistema. Una che resta porta il conto in modalità prudente: niente nuovi acquisti, vendite e stop proseguono. -- Dopo 15 minuti l'utente sceglie: **allinea** (il registro prende i numeri del conto, con una riga di rettifica che conta come movimento di capitale, non come risultato) o **escludi** (la differenza resta fuori dal registro e non si segnala più). -- Una chiave che permette i prelievi tiene il conto in modalità prudente finché non viene sostituita: non si allinea né si esclude. +- Dopo 15 minuti l'utente sceglie: **allinea** (il registro prende i numeri del conto, con una riga di rettifica che conta come movimento di capitale, non come risultato) o **escludi** (la differenza resta fuori dal registro e non si segnala più). Prima di agire la scelta rifà il confronto: si applica solo alle differenze che esistono in quel momento. Se dopo i 15 minuti la differenza sparisce, il conto aspetta comunque la scelta, ma senza differenze vecchie da copiare. +- Una chiave che permette i prelievi tiene il conto in modalità prudente finché non viene sostituita: non si allinea né si esclude. Sostituita con una chiave senza prelievo, il conto torna normale. **12. Chiavi inserite dall'interfaccia.** - Impostazioni → Conti e chiavi accetta le chiavi dei conti di prova. Prima di salvarle Anubis chiede al broker cosa possono fare. Le rifiuta se permettono i prelievi o se non possono fare ordini. - Salvate, sono cifrate (SPEC 15.4), non si rimostrano mai e il motore riparte da solo per usarle. - Le chiavi nelle variabili d'ambiente valgono di più e si cambiano solo lì. -- Finché non c'è capitale affidato, la Panoramica indica questo passo come il primo. +- Chiavi e capitale non entrano nella procedura del primo avvio, che resta corta (password e database) e deve funzionare anche senza chiavi. Al primo accesso, finché non c'è capitale affidato, la Panoramica indica questo passo come il primo e porta a Impostazioni → Conti e chiavi. **13. Batteria di conformità dei broker.** - Una classe astratta di prove, eseguita sul simulatore, sulle risposte d'esempio della documentazione di Alpaca e OKX e, con le chiavi, sui veri conti di prova (categoria `LiveDemo`). @@ -70,6 +74,8 @@ La Fase 3 collega Anubis ai conti: un'interfaccia comune per i broker, il simula **15. Niente operazioni nella modalità dimostrativa.** Il motore di trading è spento nella demo: la demo mostra l'appendice F e non manda nulla. +**16. Il flusso IEX delle azioni passa alla Fase 6.** L'ADR 0008 lo prevedeva in questa fase, per seguire le posizioni azionarie. Senza le chiavi Alpaca non ci sono né dati azionari né posizioni da seguire. E le posizioni non dipendono dal flusso: lo stop sta sul broker e la riconciliazione controlla ogni 5 minuti. Il flusso (al massimo 30 simboli) arriva con il motore dal vivo della Fase 6, che sceglie quali titoli seguire. + ## Conseguenze - Tutto il percorso dell'ordine è provato senza rete. Le prove con i veri conti di prova aspettano le chiavi dell'utente. diff --git a/src/Anubis.Server/Components/Pages/Conti.razor b/src/Anubis.Server/Components/Pages/Conti.razor index a93ed04..9aa295e 100644 --- a/src/Anubis.Server/Components/Pages/Conti.razor +++ b/src/Anubis.Server/Components/Pages/Conti.razor @@ -355,6 +355,7 @@ else ("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), }; diff --git a/src/Anubis.Server/Forms.cs b/src/Anubis.Server/Forms.cs index 52969c2..816fd1e 100644 --- a/src/Anubis.Server/Forms.cs +++ b/src/Anubis.Server/Forms.cs @@ -177,7 +177,8 @@ internal static partial class Forms } /// The user's choice for an account in prudent mode after the 15 minutes (SPEC 7.4): align or leave out. - private static async Task ResolveAccountAsync(Guid id, string choice, AnubisHost host, HttpContext context, CancellationToken ct) + // The form is bound even when unused: binding it is what makes the antiforgery check run on the route. + private static async Task ResolveAccountAsync(Guid id, string choice, IFormCollection form, AnubisHost host, IClock clock, HttpContext context, CancellationToken ct) { if (choice is not ("allinea" or "escludi")) { @@ -195,6 +196,12 @@ internal static partial class Forms return Results.Redirect("/conti?errore=chiave"); } + if (!state.WaitsForUser(clock.UtcNow, trading.Reconciler.Grace)) + { + // The choice exists only after the 15 minutes (SPEC 7.4): before, the difference may still explain itself. + return Results.Redirect("/conti?errore=presto"); + } + 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); @@ -263,7 +270,7 @@ internal static partial class Forms return Results.Redirect(back + "?ok=chiavi#conti"); } - private static async Task RemoveKeysAsync(string broker, AnubisHost host, HttpContext context, CancellationToken ct) + private static async Task RemoveKeysAsync(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) || host.IsDemo || host.Services is not { } services) diff --git a/src/Anubis/Trading/Capital.cs b/src/Anubis/Trading/Capital.cs index fae9253..66ba715 100644 --- a/src/Anubis/Trading/Capital.cs +++ b/src/Anubis/Trading/Capital.cs @@ -96,6 +96,13 @@ public sealed class Capital(Database db, IClock clock, Ledger ledger, FxStore fx } var currency = Strategies.CurrencyOf(broker); + var previous = await EntrustedAsync(broker, cancellationToken).ConfigureAwait(false); + if (previous is not null && previous.AmountEur == amountEur) + { + // The same amount again changes nothing, whatever the exchange rate did meanwhile. + return previous; + } + var rate = 1m; if (currency != "EUR") { @@ -105,7 +112,12 @@ public sealed class Capital(Database db, IClock clock, Ledger ledger, FxStore fx ?? throw new CapitalException($"Manca il cambio EUR/{currency}: lo scarico ogni giorno dalla BCE, riprova più tardi."); } - var entrusted = new EntrustedCapital(broker, amountEur, currency, rate, Math.Round(amountEur * rate, 2, MidpointRounding.ToZero), changedBy, clock.UtcNow); + // Only the change in euro is converted at today's rate: the dollars already given stay as they are, so a + // move of the exchange rate is never booked as a deposit or a withdrawal. The rate kept is the overall one. + var amount = Math.Round((previous?.Amount ?? 0m) + ((amountEur - (previous?.AmountEur ?? 0m)) * rate), 2, MidpointRounding.ToZero); + amount = amountEur == 0m ? 0m : Math.Max(amount, 0m); + var overall = amountEur > 0m && currency != "EUR" ? Math.Round(amount / amountEur, 6) : rate; + var entrusted = new EntrustedCapital(broker, amountEur, currency, overall, amount, changedBy, clock.UtcNow); var plan = await PlanAsync(broker, entrusted.Amount, null, cancellationToken).ConfigureAwait(false); await db.WriteAsync(s => s.ExecuteAsync( "INSERT INTO capital_changes (id, broker, amount_eur, currency, fx_rate, amount, changed_by, created_at) VALUES (@id, @b, @e, @c, @r, @a, @by, @now)", cancellationToken, diff --git a/src/Anubis/Trading/Ledger.cs b/src/Anubis/Trading/Ledger.cs index caa22be..ffc58f7 100644 --- a/src/Anubis/Trading/Ledger.cs +++ b/src/Anubis/Trading/Ledger.cs @@ -232,8 +232,27 @@ public sealed class Ledger(Database db, IClock clock) } /// A cash dividend for a strategy, recorded on the ex-date (SPEC 7.5). - public Task RecordDividendAsync(Guid accountId, string strategy, decimal amount, string currency, DateTimeOffset exDate, string note, CancellationToken cancellationToken) => - db.WriteAsync(s => AddCashAsync(s, accountId, strategy, currency, amount, LedgerKinds.Dividend, null, note, exDate, cancellationToken), cancellationToken); + /// + /// A cash dividend for a strategy. With the payment and its mark are one write, and a + /// second call with the same key does nothing. + /// + public Task RecordDividendAsync(Guid accountId, string strategy, decimal amount, string currency, DateTimeOffset exDate, string note, CancellationToken cancellationToken, + string? appliedKey = null) => + db.WriteAsync(async s => + { + if (appliedKey is not null) + { + if (await s.CountAsync("SELECT COUNT(*) FROM applied_actions WHERE account_id = @a AND source_key = @k", ("a", accountId), ("k", appliedKey)).ConfigureAwait(false) > 0) + { + return; + } + + await s.ExecuteAsync("INSERT INTO applied_actions (id, account_id, source_key, created_at) VALUES (@id, @a, @k, @now)", cancellationToken, + ("id", Ids.NewId(clock)), ("a", accountId), ("k", appliedKey), ("now", clock.UtcNow)).ConfigureAwait(false); + } + + await AddCashAsync(s, accountId, strategy, currency, amount, LedgerKinds.Dividend, null, note, exDate, cancellationToken).ConfigureAwait(false); + }, cancellationToken); /// /// Writes a residue off the ledger at market value (SPEC 7.2): a sale at that price that brings no money, whose @@ -286,31 +305,55 @@ public sealed class Ledger(Database db, IClock clock) }, cancellationToken); /// - /// A split in an account's ledger (SPEC 6.10): quantities times the ratio, average prices, stops and open lots - /// divided by it; values and results do not change. False when the action was already applied. + /// A split in an account's ledger (SPEC 6.10). Only what was held before the ex-date is multiplied: shares bought + /// on or after it are already new shares. The cost of each position stays the same, so values and results do not + /// change. Returns how many old shares were held across the strategies (0 when none), or null when the action was + /// already applied. /// - public Task ApplySplitAsync(Guid accountId, Guid instrumentId, string sourceKey, decimal ratio, CancellationToken cancellationToken) + public Task ApplySplitAsync(Guid accountId, Guid instrumentId, string sourceKey, decimal ratio, DateTimeOffset exDate, CancellationToken cancellationToken) { ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(ratio, 0m); - return db.WriteAsync(async s => + return db.WriteAsync(async s => { if (await s.CountAsync("SELECT COUNT(*) FROM applied_actions WHERE account_id = @a AND source_key = @k", ("a", accountId), ("k", sourceKey)).ConfigureAwait(false) > 0) { - return false; + return null; } var now = clock.UtcNow; + var (baseCurrency, _) = await CurrenciesAsync(s, instrumentId).ConfigureAwait(false); var positions = (await s.QueryAsync("SELECT account_id, instrument_id, strategy, quantity, average_price, last_price, stop_price, opened_at FROM positions WHERE account_id = @a AND instrument_id = @i", ReadPosition, ("a", accountId), ("i", instrumentId)).ConfigureAwait(false)).ToList(); - foreach (var p in positions) + var oldShares = 0m; + foreach (var p in positions.Where(p => p.Quantity > 0m)) { + // What the fills since the ex-date added or removed is in new shares already; the rest is old. + var since = await s.QueryAsync("SELECT side, quantity, fee, fee_currency FROM fills WHERE account_id = @a AND instrument_id = @i AND strategy = @s AND executed_at >= @at", + r => (Side: Wire.Side(r.Text("side")), Quantity: r.Money("quantity"), Fee: r.Money("fee"), FeeCurrency: r.Text("fee_currency")), + ("a", accountId), ("i", instrumentId), ("s", p.Strategy), ("at", exDate)).ConfigureAwait(false); + var after = since.Sum(r => + { + var inKind = baseCurrency is not null && r.FeeCurrency == baseCurrency ? r.Fee : 0m; + return r.Side == OrderSide.Buy ? r.Quantity - inKind : -(r.Quantity + inKind); + }); + var old = p.Quantity - after; + if (old <= Dust) + { + continue; + } + + oldShares += old; + var quantity = p.Quantity + (old * (ratio - 1m)); + var unchanged = since.Count == 0; await s.ExecuteAsync("UPDATE positions SET quantity = @q, average_price = @p, last_price = @l, stop_price = @st, updated_at = @now WHERE account_id = @a AND instrument_id = @i AND strategy = @s", - cancellationToken, ("q", p.Quantity * ratio), ("p", DbValues.Money(p.AveragePrice / ratio)), ("l", p.LastPrice / ratio), ("st", p.StopPrice / ratio), ("now", now), + cancellationToken, ("q", quantity), ("p", DbValues.Money(p.Quantity * p.AveragePrice / quantity)), + ("l", unchanged ? p.LastPrice / ratio : p.LastPrice), ("st", unchanged ? p.StopPrice / ratio : p.StopPrice), ("now", now), ("a", accountId), ("i", instrumentId), ("s", p.Strategy)).ConfigureAwait(false); - var lots = await s.QueryAsync("SELECT id, fill_id, quantity, price, closes_id FROM lots WHERE account_id = @a AND instrument_id = @i AND strategy = @s", - r => (Id: r.Id("id"), Fill: r.Id("fill_id"), Quantity: r.Money("quantity"), Price: r.Money("price"), Closes: r.IdOrNull("closes_id")), + var lots = await s.QueryAsync( + "SELECT l.id, l.fill_id, l.quantity, l.price, l.closes_id, f.executed_at FROM lots l JOIN fills f ON f.id = l.fill_id WHERE l.account_id = @a AND l.instrument_id = @i AND l.strategy = @s", + r => (Id: r.Id("id"), Fill: r.Id("fill_id"), Quantity: r.Money("quantity"), Price: r.Money("price"), Closes: r.IdOrNull("closes_id"), At: r.Moment("executed_at")), ("a", accountId), ("i", instrumentId), ("s", p.Strategy)).ConfigureAwait(false); - foreach (var lot in lots.Where(l => l.Closes is null && l.Quantity > 0m)) + foreach (var lot in lots.Where(l => l.Closes is null && l.Quantity > 0m && l.At < exDate)) { var remaining = lot.Quantity + lots.Where(c => c.Closes == lot.Id).Sum(c => c.Quantity); if (remaining <= Dust) @@ -328,16 +371,16 @@ public sealed class Ledger(Database db, IClock clock) if (await OpenTradeAsync(s, accountId, instrumentId, p.Strategy).ConfigureAwait(false) is { } trade) { - var row = await s.FirstOrDefaultAsync("SELECT bought_quantity, sold_quantity FROM trades WHERE id = @id", r => (B: r.Money("bought_quantity"), S: r.Money("sold_quantity")), ("id", trade)) - .ConfigureAwait(false); - await s.ExecuteAsync("UPDATE trades SET bought_quantity = @b, sold_quantity = @s, updated_at = @now WHERE id = @id", cancellationToken, - ("b", row.B * ratio), ("s", row.S * ratio), ("now", now), ("id", trade)).ConfigureAwait(false); + // The old shares count as bought again in new shares: bought minus sold still equals the position. + var row = await s.FirstOrDefaultAsync("SELECT bought_quantity FROM trades WHERE id = @id", r => r.Money("bought_quantity"), ("id", trade)).ConfigureAwait(false); + await s.ExecuteAsync("UPDATE trades SET bought_quantity = @b, updated_at = @now WHERE id = @id", cancellationToken, + ("b", row + (old * (ratio - 1m))), ("now", now), ("id", trade)).ConfigureAwait(false); } } await s.ExecuteAsync("INSERT INTO applied_actions (id, account_id, source_key, created_at) VALUES (@id, @a, @k, @now)", cancellationToken, ("id", Ids.NewId(clock)), ("a", accountId), ("k", sourceKey), ("now", now)).ConfigureAwait(false); - return true; + return oldShares; }, cancellationToken); } diff --git a/src/Anubis/Trading/OkxTrading.cs b/src/Anubis/Trading/OkxTrading.cs index 656bd27..d434674 100644 --- a/src/Anubis/Trading/OkxTrading.cs +++ b/src/Anubis/Trading/OkxTrading.cs @@ -165,8 +165,11 @@ public sealed class OkxTrading : IBrokerAccount var (code, _, data) = Envelope(answer); if (code != "0" || data.Count == 0) { - // 51603 "Order does not exist"; a canceled order without fills also disappears after two hours. - return code is "0" or "51603" or "51000" or "1" ? null : throw _http.Failure(url, answer); + // Only 51603 "Order does not exist" (also as the sCode of a failed call) means unknown; a canceled order + // without fills also disappears after two hours. Any other failure is an error, retried later: a generic + // failure taken for "never arrived" would let a lost order be sent twice. + var sCode = data.Count > 0 ? Text(data[0], "sCode") : null; + return code is "0" or "51603" || sCode == "51603" ? null : throw _http.Failure(url, answer); } return stop ? ReadAlgo(data[0]) : ReadOrder(data[0]); diff --git a/src/Anubis/Trading/OrderManager.Protection.cs b/src/Anubis/Trading/OrderManager.Protection.cs index 9f50528..cd7ef7e 100644 --- a/src/Anubis/Trading/OrderManager.Protection.cs +++ b/src/Anubis/Trading/OrderManager.Protection.cs @@ -140,12 +140,20 @@ public sealed partial class OrderManager return; } - var applied = await _ledger.ApplySplitAsync(_account.AccountId, instrument.Id, split.SourceKey, split.Ratio!.Value, cancellationToken).ConfigureAwait(false); - if (!applied) + var exDate = new DateTimeOffset(split.ExDate.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); + var oldShares = await _ledger.ApplySplitAsync(_account.AccountId, instrument.Id, split.SourceKey, split.Ratio!.Value, exDate, cancellationToken).ConfigureAwait(false); + if (oldShares is not > 0m) { + // Already applied, or nothing was held before the ex-date: the shares held are new shares already. return; } + if (_broker is PaperBroker paper) + { + // The simulator is the broker of this account: its book follows the same split, once. + await paper.ApplySplitAsync(instrument.Symbol, split.Ratio.Value, oldShares.Value, exDate, cancellationToken).ConfigureAwait(false); + } + foreach (var position in (await _ledger.PositionsAsync(_account.AccountId, cancellationToken).ConfigureAwait(false)).Where(p => p.InstrumentId == instrument.Id)) { await CancelStopsOfAsync(instrument.Id, position.Strategy, instrument, cancellationToken).ConfigureAwait(false); @@ -173,11 +181,14 @@ public sealed partial class OrderManager private async Task AlignPositionAsync(Guid instrumentId, string strategy, Instrument instrument, CancellationToken cancellationToken) { - var working = await _db.ReadAsync(s => s.CountAsync("SELECT COUNT(*) FROM executions WHERE account_id = @a AND instrument_id = @i AND strategy = @s AND status = @w", - ("a", _account.AccountId), ("i", instrumentId), ("s", strategy), ("w", ExecutionStates.Working)), cancellationToken).ConfigureAwait(false); + // An execution in progress owns the stops of its position until it ends. Where the broker refuses a purchase + // while any sell stop rests on the symbol (Alpaca), a purchase of any strategy holds every stop on it. + var sql = "SELECT COUNT(*) FROM executions WHERE account_id = @a AND instrument_id = @i AND status = @w AND (strategy = @s" + + (_broker.Capabilities.OppositeSideBlocked ? " OR side = @buy)" : ")"); + var working = await _db.ReadAsync(s => s.CountAsync(sql, + ("a", _account.AccountId), ("i", instrumentId), ("s", strategy), ("w", ExecutionStates.Working), ("buy", Wire.Text(OrderSide.Buy))), cancellationToken).ConfigureAwait(false); if (working > 0) { - // An execution in progress owns the stops of its position until it ends. return; } @@ -264,6 +275,21 @@ public sealed partial class OrderManager } } + /// Cancels the open stops of every strategy on an instrument (Alpaca blocks the whole symbol). True when none is left. + private async Task ClearSymbolStopsCoreAsync(Guid instrumentId, Instrument instrument, CancellationToken cancellationToken) + { + var strategies = await _db.ReadAsync(s => s.QueryAsync( + "SELECT DISTINCT strategy FROM orders WHERE account_id = @a AND instrument_id = @i AND purpose = 'stop' AND status IN ('created', 'submitted', 'partially_filled')", + r => r.Text("strategy"), ("a", _account.AccountId), ("i", instrumentId)), cancellationToken).ConfigureAwait(false); + var clear = true; + foreach (var strategy in strategies) + { + clear &= await ClearStopsCoreAsync(instrumentId, strategy, instrument, cancellationToken).ConfigureAwait(false); + } + + return clear; + } + /// Cancels the open stops of a position and confirms it with the broker. True when none is left open. private async Task ClearStopsCoreAsync(Guid instrumentId, string strategy, Instrument instrument, CancellationToken cancellationToken) { diff --git a/src/Anubis/Trading/OrderManager.cs b/src/Anubis/Trading/OrderManager.cs index c9410c8..a72cece 100644 --- a/src/Anubis/Trading/OrderManager.cs +++ b/src/Anubis/Trading/OrderManager.cs @@ -174,12 +174,22 @@ public sealed partial class OrderManager : IDisposable { await RefreshOpenOrdersAsync(cancellationToken).ConfigureAwait(false); await SyncFillsCoreAsync(cancellationToken).ConfigureAwait(false); + // One execution that fails must not leave the others, or the stops of the whole account, behind: the + // first failure is raised again only after the stops are aligned. + Exception? failure = null; var plans = new List<(ExecRow Exec, int Priority)>(); foreach (var exec in await WorkingAsync(cancellationToken).ConfigureAwait(false)) { - if (await AdvanceAsync(exec, cancellationToken).ConfigureAwait(false) is { } priority) + try { - plans.Add((exec, priority)); + if (await AdvanceAsync(exec, cancellationToken).ConfigureAwait(false) is { } priority) + { + plans.Add((exec, priority)); + } + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + failure ??= ex; } } @@ -191,11 +201,22 @@ public sealed partial class OrderManager : IDisposable continue; } - await SendStepAsync(exec, cancellationToken).ConfigureAwait(false); + try + { + await SendStepAsync(exec, cancellationToken).ConfigureAwait(false); + } + catch (Exception ex) when (ex is not OperationCanceledException) + { + failure ??= ex; + } } await SyncFillsCoreAsync(cancellationToken).ConfigureAwait(false); await AlignStopsCoreAsync(cancellationToken).ConfigureAwait(false); + if (failure is not null) + { + System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(failure); + } } finally { @@ -354,6 +375,14 @@ public sealed partial class OrderManager : IDisposable } } + // A refused sale is never given up, but it is tried again with growing pauses (1, 2, 4, 8, then 15 minutes): + // a refusal that lasts neither floods the broker nor runs out of order ids. Reconciliation finds the cause. + var refused = orders.AsEnumerable().Reverse().TakeWhile(o => o.Status == OrderStatus.Rejected && o.Reason is not ("okx_51006" or "post_only_would_take")).ToList(); + if (exec.Side == OrderSide.Sell && refused.Count > 0 && _clock.UtcNow - refused[0].CreatedAt < RefusalPause(refused.Count)) + { + return null; + } + var filled = orders.Sum(o => o.Filled); var remaining = exec.Quantity - filled; if (exec.Side == OrderSide.Sell) @@ -380,6 +409,18 @@ public sealed partial class OrderManager : IDisposable return null; } + if (quoteNow is null) + { + if (exec.Side == OrderSide.Buy && exec.Sequence == 0) + { + // No price to buy at: the decision is dropped, not left waiting. + await FinishAsync(exec, ExecutionStates.Canceled, OrderReasons.NoPrice, instrument, cancellationToken).ConfigureAwait(false); + } + + // A sale waits for a price with its stops still on the broker: they are removed only right before it leaves. + return null; + } + if (exec.Side == OrderSide.Sell && exec.Sequence == 0 && !await ClearStopsCoreAsync(exec.InstrumentId, exec.Strategy, instrument, cancellationToken).ConfigureAwait(false)) { // The stops on the quantity to sell must be gone first (SPEC 7.2); they come back if the sale stops. @@ -387,14 +428,9 @@ public sealed partial class OrderManager : IDisposable } if (exec.Side == OrderSide.Buy && exec.Sequence == 0 && _broker.Capabilities.OppositeSideBlocked - && !await ClearStopsCoreAsync(exec.InstrumentId, exec.Strategy, instrument, cancellationToken).ConfigureAwait(false)) - { - // Alpaca refuses a purchase while a sell stop rests on the symbol: cancel, buy, put back. - return null; - } - - if (quoteNow is null) + && !await ClearSymbolStopsCoreAsync(exec.InstrumentId, instrument, cancellationToken).ConfigureAwait(false)) { + // Alpaca refuses a purchase while any sell stop rests on the symbol, of any strategy: cancel, buy, put back. return null; } @@ -752,14 +788,17 @@ public sealed partial class OrderManager : IDisposable private const string OrderSelect = "SELECT id, execution_id, instrument_id, strategy, client_order_id, side, order_type, purpose, quantity, limit_price, stop_price, status, filled_quantity, " + - "average_fill_price, reference_price, step, time_in_force, post_only, created_at FROM orders"; + "average_fill_price, reference_price, step, time_in_force, post_only, created_at, reason FROM orders"; private static OrderRow ReadOrder(DbRow r) => new( r.Id("id"), r.IdOrNull("execution_id"), r.Id("instrument_id"), r.Text("strategy"), r.Text("client_order_id"), Wire.Side(r.Text("side")), Wire.Kind(r.Text("order_type")), r.TextOrNull("purpose") is { } p ? Wire.Purpose(p) : OrderPurpose.Entry, r.Money("quantity"), r.MoneyOrNull("limit_price"), r.MoneyOrNull("stop_price"), Wire.Status(r.Text("status")), r.MoneyOrNull("filled_quantity") ?? 0m, r.MoneyOrNull("average_fill_price"), r.MoneyOrNull("reference_price"), r.IsNull("step") ? null : (int)r.Number("step"), r.TextOrNull("time_in_force") is { } t ? Wire.Tif(t) : TimeInForce.Day, - !r.IsNull("post_only") && r.Bool("post_only"), r.Moment("created_at")); + !r.IsNull("post_only") && r.Bool("post_only"), r.Moment("created_at"), r.TextOrNull("reason")); + + /// The pause before trying a refused sale again: 1, 2, 4, 8, then 15 minutes. + internal static TimeSpan RefusalPause(int refusals) => TimeSpan.FromMinutes(Math.Min(15, 1 << Math.Clamp(refusals - 1, 0, 4))); public void Dispose() => _gate.Dispose(); @@ -768,5 +807,5 @@ public sealed partial class OrderManager : IDisposable private sealed record OrderRow(Guid Id, Guid? ExecutionId, Guid InstrumentId, string Strategy, string ClientOrderId, OrderSide Side, OrderKind Kind, OrderPurpose Purpose, decimal Quantity, decimal? Limit, decimal? Stop, OrderStatus Status, decimal Filled, decimal? AveragePrice, decimal? Reference, int? Step, TimeInForce TimeInForce, - bool PostOnly, DateTimeOffset CreatedAt); + bool PostOnly, DateTimeOffset CreatedAt, string? Reason = null); } diff --git a/src/Anubis/Trading/PaperBroker.cs b/src/Anubis/Trading/PaperBroker.cs index 56e9691..b6230d7 100644 --- a/src/Anubis/Trading/PaperBroker.cs +++ b/src/Anubis/Trading/PaperBroker.cs @@ -223,13 +223,13 @@ public sealed class PaperBroker : IBrokerAccount, IDisposable } /// Credits a cash dividend on the quantity held (SPEC 7.3), from the corporate actions. - public async Task PayDividendAsync(string symbol, decimal perShare, string currency, DateTimeOffset at, CancellationToken cancellationToken) + /// Pays a cash dividend on the shares held before the ex-date, as the broker would. + public async Task PayDividendAsync(string symbol, decimal heldBeforeExDate, decimal perShare, string currency, DateTimeOffset at, CancellationToken cancellationToken) { decimal paid = 0m; await WithBookAsync(book => { - var held = book.Holdings.GetValueOrDefault(symbol)?.Quantity ?? 0m; - paid = DbValues.Money(held * perShare); + paid = DbValues.Money(heldBeforeExDate * perShare); if (paid <= 0m) { return false; @@ -242,18 +242,22 @@ public sealed class PaperBroker : IBrokerAccount, IDisposable return paid; } - /// Applies a split to the simulated holding and to resting orders: quantities times the ratio, prices divided. - public async Task ApplySplitAsync(string symbol, decimal ratio, CancellationToken cancellationToken) + /// + /// Applies a split to the simulated holding and to the orders resting since before the ex-date: the + /// held before the ex-date become ratio times as many, at the same cost. + /// + public async Task ApplySplitAsync(string symbol, decimal ratio, decimal oldShares, DateTimeOffset exDate, CancellationToken cancellationToken) { await WithBookAsync(book => { - if (book.Holdings.TryGetValue(symbol, out var holding)) + if (book.Holdings.TryGetValue(symbol, out var holding) && holding.Quantity > 0m) { - holding.Quantity *= ratio; - holding.AveragePrice = DbValues.Money(holding.AveragePrice / ratio); + var quantity = holding.Quantity + (Math.Min(oldShares, holding.Quantity) * (ratio - 1m)); + holding.AveragePrice = DbValues.Money(holding.Quantity * holding.AveragePrice / quantity); + holding.Quantity = quantity; } - foreach (var order in book.Orders.Where(o => o.Request.Symbol == symbol && IsOpen(o))) + foreach (var order in book.Orders.Where(o => o.Request.Symbol == symbol && IsOpen(o) && o.SubmittedAt < exDate)) { order.Request = order.Request with { diff --git a/src/Anubis/Trading/Reconciler.cs b/src/Anubis/Trading/Reconciler.cs index abf0404..78e8378 100644 --- a/src/Anubis/Trading/Reconciler.cs +++ b/src/Anubis/Trading/Reconciler.cs @@ -151,6 +151,7 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger) } var now = clock.UtcNow; + var previous = await StateAsync(account.AccountId, cancellationToken).ConfigureAwait(false); await db.WriteAsync(async s => { await EnsureStatusAsync(s, account.AccountId, now, cancellationToken).ConfigureAwait(false); @@ -160,9 +161,20 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger) { var difference = new AccountDifference(DifferenceKinds.Key, account.Name, 0m, 1m, $"La chiave del conto {account.Name} permette i prelievi: non la uso per nuovi acquisti."); await s.ExecuteAsync("UPDATE account_status SET mode = @m, mode_since = @now, mode_reason = @r, differences = @d, updated_at = @now WHERE account_id = @a", cancellationToken, - ("m", AccountModes.Prudent), ("now", now), ("r", KeyReason), ("d", Stored([difference], [])), ("a", account.AccountId)).ConfigureAwait(false); - await Diary.NotifyAsync(s, clock, "key_can_withdraw", - $"La chiave del conto {account.Name} permette i prelievi. Non faccio nuovi acquisti su quel conto: sostituiscila con una chiave senza prelievo.", cancellationToken).ConfigureAwait(false); + ("m", AccountModes.Prudent), ("now", now), ("r", KeyReason), ("d", Stored([difference], previous.Excluded)), ("a", account.AccountId)).ConfigureAwait(false); + if (previous.ModeReason != KeyReason) + { + await Diary.NotifyAsync(s, clock, "key_can_withdraw", + $"La chiave del conto {account.Name} permette i prelievi. Non faccio nuovi acquisti su quel conto: sostituiscila con una chiave senza prelievo.", cancellationToken).ConfigureAwait(false); + } + } + else if (previous.ModeReason == KeyReason) + { + // The key was replaced with one that cannot withdraw: the lock it caused ends; the next round compares again. + await s.ExecuteAsync("UPDATE account_status SET mode = @m, mode_since = @now, mode_reason = NULL, differences = @d, updated_at = @now WHERE account_id = @a", cancellationToken, + ("m", AccountModes.Normal), ("now", now), ("d", Stored([], previous.Excluded)), ("a", account.AccountId)).ConfigureAwait(false); + await Diary.WriteAsync(s, clock, DiaryKinds.System, "account_normal", + $"La nuova chiave del conto {account.Name} non permette i prelievi: il conto torna normale.", null, cancellationToken).ConfigureAwait(false); } }, cancellationToken).ConfigureAwait(false); return permissions; @@ -177,12 +189,19 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger) { ArgumentNullException.ThrowIfNull(manager); var account = manager.Account; - var state = await StateAsync(account.AccountId, cancellationToken).ConfigureAwait(false); - if (state.ModeReason == KeyReason) + if ((await StateAsync(account.AccountId, cancellationToken).ConfigureAwait(false)).ModeReason == KeyReason) { throw new InvalidOperationException("A key that may withdraw is replaced, not aligned."); } + // Compared again right now: the choice applies to the differences that exist, never to old ones. + await ReconcileAsync(manager, cancellationToken).ConfigureAwait(false); + var state = await StateAsync(account.AccountId, cancellationToken).ConfigureAwait(false); + if (state.Mode != AccountModes.Prudent || state.ModeReason == KeyReason) + { + return; + } + var excluded = state.Excluded.ToList(); if (choice == ReconcileChoice.Align) { @@ -471,6 +490,11 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger) stored = []; diary = $"La differenza sul conto {account.Name} si è spiegata da sola: il conto torna normale."; } + else if (mode == AccountModes.Prudent && reason != KeyReason) + { + // After the 15 minutes the user still decides, but nothing old is kept to be aligned. + stored = []; + } await db.WriteAsync(async s => { diff --git a/src/Anubis/Trading/TradingService.cs b/src/Anubis/Trading/TradingService.cs index 5b7ed13..db316aa 100644 --- a/src/Anubis/Trading/TradingService.cs +++ b/src/Anubis/Trading/TradingService.cs @@ -238,13 +238,13 @@ public sealed partial class TradingService : IDisposable }, cancellationToken).ConfigureAwait(false); } - await GuardAsync("patrimonio", () => Equity.TakeAsync(cancellationToken), cancellationToken).ConfigureAwait(false); + await GuardAsync("patrimonio", () => PhotographAsync(cancellationToken), cancellationToken).ConfigureAwait(false); } public IReadOnlyList Jobs() => [ new ScheduledJob("riconciliazione", "Riconciliazione dei conti", Schedule.Every(TimeSpan.FromMinutes(5)), ReconcileAllAsync), - new ScheduledJob("patrimonio", "Fotografia del patrimonio", Schedule.Every(TimeSpan.FromMinutes(5)), async ct => await Equity.TakeAsync(ct).ConfigureAwait(false)), + new ScheduledJob("patrimonio", "Fotografia del patrimonio", Schedule.Every(TimeSpan.FromMinutes(5)), PhotographAsync), new ScheduledJob("azioni_societarie", "Dividendi e frazionamenti nei registri", Schedule.Every(TimeSpan.FromHours(1)), CorporateActionsAsync), ]; @@ -273,6 +273,34 @@ public sealed partial class TradingService : IDisposable await Task.WhenAll(streams).ConfigureAwait(false); } + /// The positions valued at the market, then the photograph of the equity (SPEC 7.5). + public async Task PhotographAsync(CancellationToken cancellationToken) + { + await MarkPositionsAsync(cancellationToken).ConfigureAwait(false); + await Equity.TakeAsync(cancellationToken).ConfigureAwait(false); + } + + /// + /// Writes the mid of a recent quote as the last price of every position, so equity, drawdown and the Conti screen + /// follow the market and not only the last fill. Without a quote the last known price stays. + /// + public async Task MarkPositionsAsync(CancellationToken cancellationToken) + { + var held = (await Ledger.PositionsAsync(null, cancellationToken).ConfigureAwait(false)).Select(p => p.InstrumentId).Distinct().ToList(); + foreach (var id in held) + { + if (await _catalog.GetAsync(id, cancellationToken).ConfigureAwait(false) is not { } instrument) + { + continue; + } + + if (await Quotes.GetAsync(instrument.Symbol, cancellationToken).ConfigureAwait(false) is { Bid: > 0m, Ask: > 0m } quote && quote.Ask >= quote.Bid) + { + await Ledger.MarkAsync(id, quote.Mid, cancellationToken).ConfigureAwait(false); + } + } + } + public async Task TickAllAsync(CancellationToken cancellationToken) { foreach (var manager in _managers.Values) @@ -331,11 +359,7 @@ public sealed partial class TradingService : IDisposable var instrument = held[action.Symbol]; if (action.ChangesShares) { - if (manager.Broker is PaperBroker paper && !await AppliedAsync(manager.Account.AccountId, action.SourceKey, cancellationToken).ConfigureAwait(false)) - { - await paper.ApplySplitAsync(action.Symbol, action.Ratio!.Value, cancellationToken).ConfigureAwait(false); - } - + // The ledger and, on the simulator, the broker's book, together and once. await manager.ApplySplitAsync(instrument, action, cancellationToken).ConfigureAwait(false); } else if (action.Kind == CorporateActionKinds.CashDividend && action.CashAmount is > 0m) @@ -356,30 +380,40 @@ public sealed partial class TradingService : IDisposable } var exDate = new DateTimeOffset(action.ExDate.ToDateTime(TimeOnly.MinValue), TimeSpan.Zero); + var currency = action.Currency ?? account.Currency; var strategies = (await Ledger.PositionsAsync(account.AccountId, cancellationToken).ConfigureAwait(false)) .Where(p => p.InstrumentId == instrument.Id).Select(p => p.Strategy).Distinct(StringComparer.Ordinal).ToList(); + var held = new List<(string Strategy, decimal Quantity)>(); foreach (var strategy in strategies) { var quantity = await Ledger.QuantityAtAsync(account.AccountId, instrument.Id, strategy, exDate, cancellationToken).ConfigureAwait(false); - if (quantity <= 0m) + if (quantity > 0m) { - continue; + held.Add((strategy, quantity)); } - - var amount = Math.Round(quantity * action.CashAmount!.Value, 2, MidpointRounding.ToZero); - if (manager.Broker is PaperBroker paper) - { - await paper.PayDividendAsync(action.Symbol, action.CashAmount!.Value, action.Currency ?? account.Currency, exDate, cancellationToken).ConfigureAwait(false); - } - - await Ledger.RecordDividendAsync(account.AccountId, strategy, amount, action.Currency ?? account.Currency, exDate, - $"Dividendo {action.Symbol} {Format.Quantity(action.CashAmount!.Value)} per azione", cancellationToken).ConfigureAwait(false); } - await _db.WriteAsync(s => s.ExecuteAsync("INSERT INTO applied_actions (id, account_id, source_key, created_at) VALUES (@id, @a, @k, @now) ON CONFLICT (account_id, source_key) DO NOTHING", - cancellationToken, ("id", Ids.NewId(_clock)), ("a", account.AccountId), ("k", key), ("now", _clock.UtcNow)), cancellationToken).ConfigureAwait(false); + // Each payment carries its own mark in the same write, so a failure halfway never pays anyone twice. + if (manager.Broker is PaperBroker paper && held.Count > 0 && await MarkAppliedAsync(account.AccountId, key + ":book", cancellationToken).ConfigureAwait(false)) + { + await paper.PayDividendAsync(action.Symbol, held.Sum(h => h.Quantity), action.CashAmount!.Value, currency, exDate, cancellationToken).ConfigureAwait(false); + } + + foreach (var (strategy, quantity) in held) + { + var amount = Math.Round(quantity * action.CashAmount!.Value, 2, MidpointRounding.ToZero); + await Ledger.RecordDividendAsync(account.AccountId, strategy, amount, currency, exDate, + $"Dividendo {action.Symbol} {Format.Quantity(action.CashAmount!.Value)} per azione", cancellationToken, appliedKey: key + ":" + strategy).ConfigureAwait(false); + } + + await MarkAppliedAsync(account.AccountId, key, cancellationToken).ConfigureAwait(false); } + /// Marks an action as applied to an account; false when it already was. + private Task MarkAppliedAsync(Guid accountId, string key, CancellationToken cancellationToken) => + _db.WriteAsync(async s => await s.ExecuteAsync("INSERT INTO applied_actions (id, account_id, source_key, created_at) VALUES (@id, @a, @k, @now) ON CONFLICT (account_id, source_key) DO NOTHING", + cancellationToken, ("id", Ids.NewId(_clock)), ("a", accountId), ("k", key), ("now", _clock.UtcNow)).ConfigureAwait(false) > 0, cancellationToken); + private Task AppliedAsync(Guid accountId, string sourceKey, CancellationToken cancellationToken) => _db.ReadAsync(async s => await s.CountAsync("SELECT COUNT(*) FROM applied_actions WHERE account_id = @a AND source_key = @k", ("a", accountId), ("k", sourceKey)) .ConfigureAwait(false) > 0, cancellationToken); diff --git a/tests/Anubis.Tests/AccountsScreenTests.cs b/tests/Anubis.Tests/AccountsScreenTests.cs index 344b7ec..9c021b8 100644 --- a/tests/Anubis.Tests/AccountsScreenTests.cs +++ b/tests/Anubis.Tests/AccountsScreenTests.cs @@ -101,6 +101,15 @@ public sealed class AccountsScreenTests var settings = await server.TextAsync("/impostazioni"); Assert.IsFalse(settings.Contains("test-alpaca-secret", StringComparison.Ordinal), "A saved key is never shown again."); StringAssert.Contains(settings, "Impostata"); + + // Every form needs its antiforgery token, also the ones that send no field. + foreach (var path in new[] { "/azioni/chiavi/alpaca/togli", "/azioni/conti/" + Guid.NewGuid() + "/allinea" }) + { + using var forged = await server.Http.PostAsync(new Uri(path, UriKind.Relative), new FormUrlEncodedContent([])); + Assert.AreEqual(HttpStatusCode.BadRequest, forged.StatusCode, path); + } + + Assert.IsTrue((await secrets.StatusAsync("ALPACA_API_SECRET_DEMO")).IsSet, "A forged request removes nothing."); // The engine starts in the background after "ready": the paper account appears within moments. var accounts = new List(); for (var i = 0; i < 100 && accounts.Count == 0; i++) diff --git a/tests/Anubis.Tests/LedgerTests.cs b/tests/Anubis.Tests/LedgerTests.cs index 0106cf5..34ed16d 100644 --- a/tests/Anubis.Tests/LedgerTests.cs +++ b/tests/Anubis.Tests/LedgerTests.cs @@ -125,8 +125,11 @@ public sealed class LedgerTests // A 4-for-1 split: four times the shares at a quarter of the price, the value unchanged; applied once. await rig.Ledger.SetStopAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, 180m, _none).ConfigureAwait(false); var before = (await rig.Ledger.EquitiesAsync(_none).ConfigureAwait(false)).Single(e => e.AccountId == alpaca.AccountId); - Assert.IsTrue(await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, _none).ConfigureAwait(false)); - Assert.IsFalse(await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, _none).ConfigureAwait(false), "Never twice."); + // A split whose ex-date came before the purchase changes nothing: those shares were new shares already. + Assert.AreEqual(0m, await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-old", 2m, TradingRig.Start.AddDays(-30), _none).ConfigureAwait(false)); + Assert.AreEqual(10m, (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity); + Assert.AreEqual(10m, await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, rig.Clock.UtcNow, _none).ConfigureAwait(false)); + Assert.IsNull(await rig.Ledger.ApplySplitAsync(alpaca.AccountId, aapl.Id, "split-aapl-1", 4m, rig.Clock.UtcNow, _none).ConfigureAwait(false), "Never twice."); var split = (await rig.Ledger.PositionAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!; Assert.AreEqual((40m, 50m, 45m), (split.Quantity, split.AveragePrice, split.StopPrice)); Assert.AreEqual(40m, (await rig.Ledger.OpenLotsAsync(alpaca.AccountId, aapl.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false)).Sum(l => l.Remaining)); diff --git a/tests/Anubis.Tests/OrderManagerTests.cs b/tests/Anubis.Tests/OrderManagerTests.cs index 5045bc8..4844f2a 100644 --- a/tests/Anubis.Tests/OrderManagerTests.cs +++ b/tests/Anubis.Tests/OrderManagerTests.cs @@ -111,9 +111,16 @@ public sealed class OrderManagerTests await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; + // No price at all: the sale waits, and the stops stay on the broker until it can really leave. + s.Rig.QuotesDown = true; + var (execution, _) = await s.Manager.SubmitAsync(Sell(s, held), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); + Assert.AreEqual("working/", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); + Assert.IsTrue((await OrdersAsync(s.Rig).ConfigureAwait(false)).Any(o => o.Purpose == "stop" && o.Status == "submitted"), "Without a price the stops stay."); + s.Rig.QuotesDown = false; + // Nobody buys near the price: the sale keeps going with a fresh reference and double tolerance. s.Rig.Quote("BTC-EUR", 50000m, 60000m); - var (execution, _) = await s.Manager.SubmitAsync(Sell(s, held), _none).ConfigureAwait(false); await s.Rig.TurnsAsync(s.Manager, 12, _minute).ConfigureAwait(false); Assert.AreEqual("working/", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); var tolerance = (await s.Rig.QueryAsync("SELECT tolerance FROM executions WHERE id = @id", r => r.Money("tolerance"), ("id", execution)).ConfigureAwait(false)).Single(); @@ -239,11 +246,11 @@ public sealed class OrderManagerTests { var first = !applied; applied = true; - return Task.FromResult(first ? new CorporateAction(instrument.Symbol, CorporateActionKinds.Split, new DateOnly(2026, 11, 10), "test", "split-msft") { Ratio = 4m } : null); + return Task.FromResult(first ? new CorporateAction(instrument.Symbol, CorporateActionKinds.Split, new DateOnly(2026, 11, 11), "test", "split-msft") { Ratio = 4m } : null); }).ConfigureAwait(false); await a.Manager.SubmitAsync(Buy(a, 10m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); await a.Rig.TurnsAsync(a.Manager, 2, _minute).ConfigureAwait(false); - await a.Paper.ApplySplitAsync("MSFT", 4m, _none).ConfigureAwait(false); + a.Rig.Clock.Advance(TimeSpan.FromDays(1)); a.Rig.Quote("MSFT", 100m, 100.05m); await a.Rig.Ledger.SetStopAsync(a.Account.AccountId, a.Instrument.Id, Strategies.EquityMomentum, 380m, _none).ConfigureAwait(false); await a.Rig.TurnsAsync(a.Manager, 2, _minute).ConfigureAwait(false); @@ -254,6 +261,7 @@ public sealed class OrderManagerTests Assert.IsFalse((await OrdersAsync(a.Rig).ConfigureAwait(false)).Any(o => o.Purpose == "protective")); var stop = (await OrdersAsync(a.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "stop" && o.Status == "submitted"); Assert.AreEqual((40m, 95m), (stop.Quantity, stop.Stop)); + Assert.AreEqual(40m, (await a.Paper.GetPositionsAsync(_none).ConfigureAwait(false)).Single().Quantity, "The simulator's book follows the same split."); } [TestMethod] diff --git a/tests/Anubis.Tests/ReconcilerTests.cs b/tests/Anubis.Tests/ReconcilerTests.cs index 9ec157b..baeac8b 100644 --- a/tests/Anubis.Tests/ReconcilerTests.cs +++ b/tests/Anubis.Tests/ReconcilerTests.cs @@ -123,6 +123,13 @@ public sealed class ReconcilerTests Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace)); Assert.HasCount(2, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'account_prudent'", r => r.Id("id")).ConfigureAwait(false)); + // If it clears after the 15 minutes the user still decides, but no old difference is kept: "align" could + // otherwise copy numbers that are no longer true. The choice compares again before it acts. + s.Broker.Missing.Clear(); + Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode); + Assert.IsEmpty((await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Differences); + s.Broker.Missing["BTC-EUR"] = 0.004m; + // Align: the ledger takes the account's number with an adjustment, and the account is normal again. await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false); Assert.AreEqual(0.00598m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); @@ -202,7 +209,14 @@ public sealed class ReconcilerTests s.Rig.Clock.Advance(TimeSpan.FromMinutes(5)); Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode); await Assert.ThrowsExactlyAsync(() => s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none)).ConfigureAwait(false); - Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'key_can_withdraw'", r => r.Id("id")).ConfigureAwait(false)); + await s.Reconciler.CheckKeyAsync(s.Manager, _none).ConfigureAwait(false); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'key_can_withdraw'", r => r.Id("id")).ConfigureAwait(false), "Notified once."); + + // Replaced by a key without withdrawals: the lock ends, and purchases are allowed again. + s.Broker.KeyCanWithdraw = false; + await s.Reconciler.CheckKeyAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode); + Assert.IsNull((await s.Manager.SubmitAsync(Entry(s), _none).ConfigureAwait(false)).Refusal); } [TestMethod] diff --git a/tests/Anubis.Tests/TradingRulesTests.cs b/tests/Anubis.Tests/TradingRulesTests.cs index 0b6bab4..1af0f4d 100644 --- a/tests/Anubis.Tests/TradingRulesTests.cs +++ b/tests/Anubis.Tests/TradingRulesTests.cs @@ -79,6 +79,29 @@ public sealed class TradingRulesTests Assert.IsFalse(OrderRules.IsUnsellable(btc, 0.001m, 60000m)); } + [TestMethod] + [TestCategory(Category.Unit)] + public async Task OkxCountsAnOrderAsUnknownOnlyWhenItDoesNotExist() + { + var answer = "{\"code\":\"51603\",\"msg\":\"Order does not exist\",\"data\":[]}"; + var http = new FakeHttp() + .On("/api/v5/public/time", "{\"code\":\"0\",\"msg\":\"\",\"data\":[{\"ts\":\"1597026383085\"}]}") + .On(u => u.AbsolutePath == "/api/v5/trade/order", _ => FakeHttp.Respond(answer)); + using var client = http.Client(); + var okx = new OkxTrading(client, new ManualClock(TradingRig.Start), new OkxCredentials("fake-key", "fake-secret", "fake-pass"), + new OkxTradingOptions { Environment = TradingEnvironments.Demo }, TradingConsent.None); + Assert.IsNull(await okx.GetOrderAsync("ANUX1", "BTC-EUR", CancellationToken.None)); + + // A generic failure is not "unknown": taken as such, a lost order could be sent twice. + answer = "{\"code\":\"1\",\"msg\":\"Operation failed.\",\"data\":[]}"; + await Assert.ThrowsAsync(() => okx.GetOrderAsync("ANUX1", "BTC-EUR", CancellationToken.None)); + } + + [TestMethod] + [TestCategory(Category.Unit)] + public void ARefusedSaleIsTriedAgainWithGrowingPauses() => + CollectionAssert.AreEqual(new[] { 1, 2, 4, 8, 15, 15 }, Enumerable.Range(1, 6).Select(n => (int)OrderManager.RefusalPause(n).TotalMinutes).ToArray()); + [TestMethod] [TestCategory(Category.Unit)] public void OrderStatesOnlyMoveForward() diff --git a/tests/Anubis.Tests/TradingServiceTests.cs b/tests/Anubis.Tests/TradingServiceTests.cs index 00830df..bd943d8 100644 --- a/tests/Anubis.Tests/TradingServiceTests.cs +++ b/tests/Anubis.Tests/TradingServiceTests.cs @@ -92,6 +92,12 @@ public sealed class TradingServiceTests Assert.AreEqual(2200m, await BookAsync(Venues.Alpaca, "USD").ConfigureAwait(false), "Funded once, not again at every call."); var shares = await rig.QueryAsync("SELECT strategy, capital FROM strategy_state ORDER BY strategy", r => (r.Text("strategy"), r.Money("capital"))).ConfigureAwait(false); CollectionAssert.AreEqual(new[] { (Strategies.CryptoTrend, 1000m), (Strategies.EquityMeanrev, 550m), (Strategies.EquityMomentum, 1650m) }, shares.ToArray()); + + // The dollar falls: saving the same euro again changes nothing, and only a new euro amount is converted at today's rate. + await new FxStore(rig.Db, rig.Clock).UpsertAsync([new FxRate("EUR", "USD", new DateOnly(2026, 11, 10), 1.05m, "ecb")], _none).ConfigureAwait(false); + Assert.AreEqual(2200m, (await service.SetCapitalAsync(Venues.Alpaca, 2000m, "test", _none).ConfigureAwait(false)).Amount); + Assert.AreEqual(3250m, (await service.SetCapitalAsync(Venues.Alpaca, 3000m, "test", _none).ConfigureAwait(false)).Amount); + Assert.HasCount(2, await rig.QueryAsync("SELECT id FROM capital_changes WHERE broker = 'alpaca'", r => r.Id("id")).ConfigureAwait(false)); } [TestMethod] @@ -107,10 +113,11 @@ public sealed class TradingServiceTests await service.Ledger.RecordCapitalAsync(account.AccountId, Strategies.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false); await service.Ledger.RecordFillAsync(rig.Fill(account, msft, Strategies.EquityMomentum, OrderSide.Buy, 10m, 400m, feeCurrency: "USD"), _none).ConfigureAwait(false); - // Bought on Tuesday; Wednesday is the ex-date of 0,75 USD a share; Thursday a 2-for-1 split. + // Bought on Tuesday; Wednesday is the ex-date of 0,75 USD a share; Thursday a 2-for-1 split. A split of last + // month is still in the table: it came before the purchase and must change nothing. await rig.Db.WriteAsync(async s => { - foreach (var (kind, day, ratio, cash, key) in new[] { ("cash_dividend", "2026-11-11", (decimal?)null, (decimal?)0.75m, "div-msft"), ("split", "2026-11-12", 2m, null, "split-msft") }) + foreach (var (kind, day, ratio, cash, key) in new[] { ("cash_dividend", "2026-11-11", (decimal?)null, (decimal?)0.75m, "div-msft"), ("split", "2026-11-12", 2m, null, "split-msft"), ("split", "2026-10-01", 3m, null, "split-msft-old") }) { await s.ExecuteAsync("INSERT INTO corporate_actions (id, symbol, kind, ex_date, ratio, cash_amount, currency, source, source_key, created_at) " + "VALUES (@id, 'MSFT', @k, @d, @r, @c, 'USD', 'test', @key, @now)", _none, @@ -122,6 +129,7 @@ public sealed class TradingServiceTests await service.CorporateActionsAsync(_none).ConfigureAwait(false); Assert.IsEmpty(await DividendsAsync().ConfigureAwait(false), "Nothing before the ex-date."); + Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity, "An old split is not applied to new shares."); rig.Clock.Advance(TimeSpan.FromDays(1)); await service.CorporateActionsAsync(_none).ConfigureAwait(false); @@ -137,5 +145,10 @@ public sealed class TradingServiceTests Assert.HasCount(1, await DividendsAsync().ConfigureAwait(false)); Assert.HasCount(1, await rig.QueryAsync("SELECT code FROM journal WHERE code = 'split_applied'", r => r.Text("code")).ConfigureAwait(false)); Assert.IsEmpty(http.Requests); + + // Every photograph values the positions at the market, not at their last fill. + service.Quotes.Put(new Quote("MSFT", 230m, 230.10m, rig.Clock.UtcNow)); + await service.PhotographAsync(_none).ConfigureAwait(false); + Assert.AreEqual(230.05m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.LastPrice); } }