From e3caa2620747a33db58239681769aa4aceb4891f Mon Sep 17 00:00:00 2001 From: Alberto Balbo Date: Tue, 10 Nov 2026 20:40:21 +0100 Subject: [PATCH] Test the order manager and reconciliation end to end (refs #27 #28 #30) Order manager tests drive the internal simulator turn by turn: the chasing limit with its post-only steps and aggressive cross, a buy given up, a sale never given up with its alert, the pace of six sends a minute, Alpaca's GTC + day stops, the stop-already-passed rule and splits, lost answers, duplicate fills, refusals and residues. Reconciliation tests cover clean, healed, prudent mode and its return, align/exclude, unknown orders, stop coverage, withdraw-capable keys and an unreachable broker. Two defects found by the tests are fixed: stops were not cleared before an execution that starts aggressive (Alpaca refuses a buy with a sell stop open, and a sale must free the quantity), and an order's creation is now recorded as its first event. Co-Authored-By: Claude Opus 5.5 --- src/Anubis/Trading/OrderManager.cs | 28 ++- tests/Anubis.Tests/OrderManagerTests.cs | 314 ++++++++++++++++++++++++ tests/Anubis.Tests/PaperBrokerTests.cs | 158 ++++++++++++ tests/Anubis.Tests/ReconcilerTests.cs | 219 +++++++++++++++++ tests/Anubis.Tests/TradingSupport.cs | 100 ++++++++ 5 files changed, 813 insertions(+), 6 deletions(-) create mode 100644 tests/Anubis.Tests/OrderManagerTests.cs create mode 100644 tests/Anubis.Tests/PaperBrokerTests.cs create mode 100644 tests/Anubis.Tests/ReconcilerTests.cs diff --git a/src/Anubis/Trading/OrderManager.cs b/src/Anubis/Trading/OrderManager.cs index 8a93f2f..c9410c8 100644 --- a/src/Anubis/Trading/OrderManager.cs +++ b/src/Anubis/Trading/OrderManager.cs @@ -339,12 +339,19 @@ public sealed partial class OrderManager : IDisposable return null; } - if (current.Kind != OrderKind.Market && exec.StepStartedAt is { } started && _clock.UtcNow - started >= StepTime) + if (current.Kind == OrderKind.Market || exec.StepStartedAt is not { } started || _clock.UtcNow - started < StepTime) { - await CancelAndReadAsync(current, instrument, "step time over", cancellationToken).ConfigureAwait(false); + return null; } - return null; + // The step's time is over: the next step leaves in this same turn, but only once the broker confirms + // the cancellation, and only for what is left (SPEC 7.2). + await CancelAndReadAsync(current, instrument, "step time over", cancellationToken).ConfigureAwait(false); + orders = await ExecutionOrdersAsync(exec.Id, cancellationToken).ConfigureAwait(false); + if (orders.LastOrDefault() is not { } after || !OrderStates.IsFinal(after.Status)) + { + return null; + } } var filled = orders.Sum(o => o.Filled); @@ -373,13 +380,13 @@ public sealed partial class OrderManager : IDisposable return null; } - if (exec.Side == OrderSide.Sell && exec.Step < 0 && !await ClearStopsCoreAsync(exec.InstrumentId, exec.Strategy, instrument, cancellationToken).ConfigureAwait(false)) + 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. return null; } - if (exec.Side == OrderSide.Buy && exec.Step < 0 && _broker.Capabilities.OppositeSideBlocked + 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. @@ -726,7 +733,16 @@ public sealed partial class OrderManager : IDisposable private Task OrderByClientIdAsync(string clientId, CancellationToken cancellationToken) => _db.ReadAsync(s => s.FirstOrDefaultAsync(OrderSelect + " WHERE client_order_id = @c AND account_id = @a", ReadOrder, ("c", clientId), ("a", _account.AccountId)), cancellationToken); - private Task InsertOrderAsync(DbSession s, OrderRow row, CancellationToken cancellationToken) => s.ExecuteAsync( + /// A new order and its first event: the creation is part of the recorded history (SPEC 7.2). + private async Task InsertOrderAsync(DbSession s, OrderRow row, CancellationToken cancellationToken) + { + var inserted = await InsertOrderRowAsync(s, row, cancellationToken).ConfigureAwait(false); + await s.ExecuteAsync("INSERT INTO order_events (id, order_id, to_status, filled_quantity, note, occurred_at, created_at) VALUES (@id, @o, 'created', 0, 'created', @now, @now)", + cancellationToken, ("id", Ids.NewId(_clock)), ("o", row.Id), ("now", row.CreatedAt)).ConfigureAwait(false); + return inserted; + } + + private Task InsertOrderRowAsync(DbSession s, OrderRow row, CancellationToken cancellationToken) => s.ExecuteAsync( "INSERT INTO orders (id, account_id, instrument_id, strategy, client_order_id, side, order_type, quantity, limit_price, stop_price, status, created_at, updated_at, " + "purpose, time_in_force, post_only, reference_price, filled_quantity, step, execution_id) " + "VALUES (@id, @a, @i, @s, @c, @side, @k, @q, @l, @st, 'created', @now, @now, @p, @tif, @po, @r, 0, @step, @e)", cancellationToken, diff --git a/tests/Anubis.Tests/OrderManagerTests.cs b/tests/Anubis.Tests/OrderManagerTests.cs new file mode 100644 index 0000000..5045bc8 --- /dev/null +++ b/tests/Anubis.Tests/OrderManagerTests.cs @@ -0,0 +1,314 @@ +using Anubis.Core; +using Anubis.Data; +using Anubis.Trading; + +namespace Anubis.Tests; + +/// The order manager (issues #27, #28, #30, SPEC 7.2): chasing limit, pace, sales, server stops, faults. +[TestClass] +public sealed class OrderManagerTests +{ + private static readonly CancellationToken _none = CancellationToken.None; + private static readonly TimeSpan _minute = TimeSpan.FromSeconds(60); + + private sealed record Setup(TradingRig Rig, AccountContext Account, Instrument Instrument, PaperBroker Paper, OrderManager Manager) : IAsyncDisposable + { + public ValueTask DisposeAsync() => Rig.DisposeAsync(); + } + + private static async Task OkxAsync(InstrumentSpec? spec = null, decimal bid = 60000m, decimal ask = 60010m) + { + var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); + var instrument = await rig.InstrumentAsync(spec ?? TradingRig.Btc).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Okx(), account.AccountId); + await paper.DepositAsync(100_000m, "EUR", _none).ConfigureAwait(false); + rig.Quote(instrument.Symbol, bid, ask); + return new Setup(rig, account, instrument, paper, rig.Manager(paper, account)); + } + + private static async Task AlpacaAsync(Func>? pendingSplit = null) + { + var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Simulation).ConfigureAwait(false); + var instrument = await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Alpaca(), account.AccountId); + await paper.DepositAsync(50_000m, "USD", _none).ConfigureAwait(false); + rig.Quote(instrument.Symbol, 400m, 400.10m); + return new Setup(rig, account, instrument, paper, rig.Manager(paper, account, pendingSplit: pendingSplit)); + } + + private static TradeIntent Buy(Setup s, decimal quantity, decimal? stop = null, bool aggressive = false, string strategy = Strategies.CryptoTrend) => + new(s.Instrument.Id, strategy, OrderSide.Buy, quantity, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)) { StopLevel = stop, StartAggressive = aggressive }; + + private static TradeIntent Sell(Setup s, decimal quantity, string strategy = Strategies.CryptoTrend) => + new(s.Instrument.Id, strategy, OrderSide.Sell, quantity, OrderPurpose.Exit, Ids.NewId(s.Rig.Clock)); + + private static Task> OrdersAsync(TradingRig rig) => + rig.QueryAsync("SELECT purpose, order_type, status, quantity, limit_price, stop_price, post_only, time_in_force, client_order_id FROM orders ORDER BY created_at, id", + r => (r.Text("purpose"), r.Text("order_type"), r.Text("status"), r.Money("quantity"), r.MoneyOrNull("limit_price"), r.MoneyOrNull("stop_price"), r.Bool("post_only"), + r.Text("time_in_force"), r.Text("client_order_id"))); + + private static async Task ExecutionStatusAsync(TradingRig rig, Guid id) => + (await rig.QueryAsync("SELECT status, reason FROM executions WHERE id = @id", r => r.Text("status") + "/" + r.TextOrNull("reason"), ("id", id)).ConfigureAwait(false)).Single(); + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task ABuyChasesThePriceThenCrossesWithinToleranceAndGetsItsServerStop() + { + await using var s = await OkxAsync().ConfigureAwait(false); + var (execution, refusal) = await s.Manager.SubmitAsync(Buy(s, 0.01m, stop: 57000m), _none).ConfigureAwait(false); + Assert.IsNull(refusal); + await s.Rig.TurnsAsync(s.Manager, 6, _minute).ConfigureAwait(false); + + var orders = await OrdersAsync(s.Rig).ConfigureAwait(false); + var entries = orders.Where(o => o.Purpose == "entry").ToList(); + Assert.HasCount(4, entries); + + // Three post-only steps at the best bid, each canceled after a minute, then an aggressive limit at mid + 0.5%. + Assert.IsTrue(entries.Take(3).All(o => o.Kind == "limit" && o.PostOnly && o.Limit == 60000m && o.Status == "canceled")); + Assert.AreEqual(("limit", false, OrderRules.RoundDown(60005m * 1.005m, 0.1m), "filled"), (entries[3].Kind, entries[3].PostOnly, entries[3].Limit, entries[3].Status)); + Assert.AreEqual(4, entries.Select(o => o.ClientId).Distinct().Count(), "Every step is a new id."); + Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); + var events = await s.Rig.QueryAsync("SELECT from_status, to_status FROM order_events e JOIN orders o ON o.id = e.order_id WHERE o.client_order_id = @c ORDER BY e.occurred_at, e.id", + r => (r.TextOrNull("from_status") ?? "") + ">" + r.Text("to_status"), ("c", entries[0].ClientId)).ConfigureAwait(false); + CollectionAssert.AreEqual(new[] { ">created", "created>submitted", "submitted>canceled" }, events.ToArray(), "Every transition is recorded."); + + // Held: 0.01 less the taker fee kept in coin. The slippage against the mid of the first order is measured. + var position = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!; + Assert.AreEqual((0.00998m, 57000m), (position.Quantity, position.StopPrice)); + var slippage = (await s.Rig.QueryAsync("SELECT slippage_bp FROM fills", r => r.Money("slippage_bp")).ConfigureAwait(false)).Single(); + Assert.IsTrue(slippage is > 0m and < 10m, slippage.ToString(System.Globalization.CultureInfo.InvariantCulture)); + + // One trigger order on OKX for the whole quantity, valid until canceled (SPEC 7.2). + var stop = orders.Single(o => o.Purpose == "stop"); + Assert.AreEqual(("stop", "submitted", 0.00998m, 57000m, "gtc"), (stop.Kind, stop.Status, stop.Quantity, stop.Stop, stop.Tif)); + Assert.AreEqual(OrderPurpose.Stop, ClientOrderIds.Parse(stop.ClientId)!.Purpose); + Assert.AreEqual(stop.ClientId, (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single().ClientOrderId); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task ABuyIsGivenUpWhenEvenTheAggressiveLimitDoesNotFill() + { + await using var s = await OkxAsync(bid: 60000m, ask: 61000m).ConfigureAwait(false); + var (execution, _) = await s.Manager.SubmitAsync(Buy(s, 0.01m, stop: 57000m), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 7, _minute).ConfigureAwait(false); + Assert.AreEqual("canceled/not_filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); + var orders = await OrdersAsync(s.Rig).ConfigureAwait(false); + Assert.HasCount(4, orders); + Assert.IsTrue(orders.All(o => o.Status == "canceled")); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.IsEmpty(await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false), "Nothing left working."); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task ASaleIsNeverGivenUpAndAlertsAfterFiveMinutes() + { + await using var s = await OkxAsync().ConfigureAwait(false); + await s.Manager.SubmitAsync(Buy(s, 0.01m, stop: 40000m, aggressive: true), _none).ConfigureAwait(false); + 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; + + // 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(); + Assert.AreEqual(0.01m, tolerance); + var alerts = await s.Rig.QueryAsync("SELECT kind FROM notifications WHERE kind = 'sell_not_filled'", r => r.Text("kind")).ConfigureAwait(false); + Assert.HasCount(1, alerts, "One alert, then it carries on."); + Assert.IsFalse((await OrdersAsync(s.Rig).ConfigureAwait(false)).Any(o => o.Purpose == "exit" && o.Kind == "market"), "Never a market order for a strategy's exit."); + Assert.IsFalse((await OrdersAsync(s.Rig).ConfigureAwait(false)).Any(o => o.Purpose == "stop" && o.Status == "submitted"), "The stops leave before the sale."); + + // Buyers come back: sold, the trade closes. + s.Rig.Quote("BTC-EUR", 59990m, 60000m); + await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); + Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution.Value).ConfigureAwait(false)); + Assert.AreEqual("closed", (await s.Rig.QueryAsync("SELECT status FROM trades", r => r.Text("status")).ConfigureAwait(false)).Single()); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AtMostSixSendsAMinuteWithSalesAndStepsFirst() + { + await using var s = await OkxAsync().ConfigureAwait(false); + var coins = new List(); + for (var i = 1; i <= 9; i++) + { + var coin = await s.Rig.InstrumentAsync(MarketDataStoreTests.Okx($"C{i}-EUR") with { TickSize = 0.01m, LotSize = 0.0001m, MinSize = 0.001m }).ConfigureAwait(false); + s.Rig.Quote(coin.Symbol, 100m, 100.10m); + coins.Add(coin); + } + + TradeIntent BuyOf(Instrument coin, bool aggressive = false) => + new(coin.Id, Strategies.CryptoTrend, OrderSide.Buy, 1m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)) { StartAggressive = aggressive, StopLevel = 90m }; + + // A position to sell later. + await s.Manager.SubmitAsync(BuyOf(coins[8], aggressive: true), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 2, TimeSpan.FromMinutes(2)).ConfigureAwait(false); + var held = (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, coins[8].Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity; + + var buys = new List(); + foreach (var coin in coins.Take(8)) + { + buys.Add((await s.Manager.SubmitAsync(BuyOf(coin), _none).ConfigureAwait(false)).Execution!.Value); + } + + var sale = (await s.Manager.SubmitAsync(new TradeIntent(coins[8].Id, Strategies.CryptoTrend, OrderSide.Sell, held, OrderPurpose.Exit, Ids.NewId(s.Rig.Clock)), _none) + .ConfigureAwait(false)).Execution!.Value; + await s.Rig.TurnsAsync(s.Manager, 1, TimeSpan.FromSeconds(30)).ConfigureAwait(false); + + // Six sends: the sale first, then five new buys; three buys wait in line. Stops do not count. + var sent = await s.Rig.QueryAsync("SELECT execution_id FROM orders WHERE purpose IN ('entry', 'exit') AND created_at > @t", r => r.Id("execution_id"), + ("t", TradingRig.Start.AddMinutes(3))).ConfigureAwait(false); + Assert.HasCount(6, sent); + Assert.Contains(sale, sent); + Assert.AreEqual(5, sent.Count(buys.Contains)); + + // Within the same minute nothing else goes. + await s.Rig.TurnsAsync(s.Manager, 1, TimeSpan.FromSeconds(31)).ConfigureAwait(false); + Assert.HasCount(6, await s.Rig.QueryAsync("SELECT id FROM orders WHERE purpose IN ('entry', 'exit') AND created_at > @t", r => r.Id("id"), ("t", TradingRig.Start.AddMinutes(3))).ConfigureAwait(false)); + + // The next minute: the sale and the five buys already working take their next step; the three new buys still wait. + await s.Rig.TurnsAsync(s.Manager, 1, TimeSpan.FromSeconds(30)).ConfigureAwait(false); + var started = await s.Rig.QueryAsync("SELECT DISTINCT execution_id FROM orders WHERE purpose = 'entry' AND created_at > @t", r => r.Id("execution_id"), + ("t", TradingRig.Start.AddMinutes(3))).ConfigureAwait(false); + Assert.HasCount(5, started.Where(buys.Contains).ToList()); + Assert.HasCount(12, await s.Rig.QueryAsync("SELECT id FROM orders WHERE purpose IN ('entry', 'exit') AND created_at > @t", r => r.Id("id"), ("t", TradingRig.Start.AddMinutes(3))).ConfigureAwait(false)); + Assert.AreEqual(0, s.Manager.Pacer.Available); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AlpacaStopsAreGtcForWholeSharesAndDayForTheFractionAndFollowEveryChange() + { + await using var s = await AlpacaAsync().ConfigureAwait(false); + await s.Manager.SubmitAsync(Buy(s, 10.5m, stop: 380m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); + var stops = (await OrdersAsync(s.Rig).ConfigureAwait(false)).Where(o => o.Purpose == "stop" && o.Status == "submitted").OrderBy(o => o.Quantity).ToList(); + Assert.AreEqual(2, stops.Count); + Assert.AreEqual((0.5m, "day"), (stops[0].Quantity, stops[0].Tif)); + Assert.AreEqual((10m, "gtc"), (stops[1].Quantity, stops[1].Tif)); + Assert.IsTrue(stops.All(o => o.Stop == 380m && o.Kind == "stop")); + + // Adding to the position: Alpaca refuses a buy with a sell stop open, so the stops leave, the buy goes, they come back. + await s.Manager.SubmitAsync(Buy(s, 5m, aggressive: true, strategy: Strategies.EquityMomentum), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); + var all = await OrdersAsync(s.Rig).ConfigureAwait(false); + Assert.IsTrue(stops.All(old => all.Single(o => o.ClientId == old.ClientId).Status == "canceled")); + var now = all.Where(o => o.Purpose == "stop" && o.Status == "submitted").OrderBy(o => o.Quantity).ToList(); + Assert.AreEqual((0.5m, "day", 15m, "gtc"), (now[0].Quantity, now[0].Tif, now[1].Quantity, now[1].Tif)); + Assert.IsGreaterThan(ClientOrderIds.Parse(stops[1].ClientId)!.Sequence, ClientOrderIds.Parse(now[0].ClientId)!.Sequence, "A new send, a new progressive."); + + // At the close the day stop expires; the GTC one fires overnight: the two count as one, the rest is sold at once. + await s.Paper.ExpireDayOrdersAsync(_none).ConfigureAwait(false); + s.Rig.Quote("MSFT", 379m, 379.20m); + await s.Paper.PumpAsync(_none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false)); + var protective = (await OrdersAsync(s.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "protective"); + Assert.AreEqual(("market", 0.5m, "filled"), (protective.Kind, protective.Quantity, protective.Status)); + Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AStopAlreadyPassedSellsAtMarketUnlessASplitExplainsIt() + { + await using (var s = await OkxAsync().ConfigureAwait(false)) + { + await s.Manager.SubmitAsync(Buy(s, 0.01m, aggressive: true), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 2, _minute).ConfigureAwait(false); + + // The level to place is already above the price: the stop counts as fired (SPEC 7.2). + await s.Rig.Ledger.SetStopAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, 61000m, _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.AreEqual("market", (await OrdersAsync(s.Rig).ConfigureAwait(false)).Single(o => o.Purpose == "protective").Kind); + Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'stop_already_passed'", r => r.Id("id")).ConfigureAwait(false)); + } + + // The morning of a 4-for-1 split the broker already shows a quarter of the price: a stop at the old level is + // not placed and nothing is sold; the split is applied and the stop follows it. + var applied = false; + await using var a = await AlpacaAsync((instrument, _) => + { + 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); + }).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.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); + + Assert.IsTrue(applied); + var position = (await a.Rig.Ledger.PositionAsync(a.Account.AccountId, a.Instrument.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!; + Assert.AreEqual((40m, 95m), (position.Quantity, position.StopPrice)); + 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)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task ALostAnswerIsNeverSentTwiceDuplicateFillsCountOnceAndRefusalsEndTheBuy() + { + await using var s = await OkxAsync().ConfigureAwait(false); + var faulty = new FaultyBroker(s.Paper) { LoseNextAnswer = true, DuplicateFills = true }; + var manager = s.Rig.Manager(faulty, s.Account); + var (execution, _) = await manager.SubmitAsync(Buy(s, 0.01m, stop: 57000m, aggressive: true), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(manager, 1, TimeSpan.FromSeconds(5)).ConfigureAwait(false); + var lost = (await OrdersAsync(s.Rig).ConfigureAwait(false)).Single(); + Assert.AreEqual("created", lost.Status, "The answer was lost: the bot does not know yet."); + + // Restart in the middle of the order: a new manager asks the broker before resending and finds it. + var restarted = s.Rig.Manager(faulty, s.Account); + await s.Rig.TurnsAsync(restarted, 3, TimeSpan.FromSeconds(5)).ConfigureAwait(false); + Assert.HasCount(1, faulty.Sent.Where(r => r.ClientOrderId == lost.ClientId).ToList(), "Sent once."); + Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM fills", r => r.Id("id")).ConfigureAwait(false), "Every fill was reported twice, recorded once."); + Assert.AreEqual(0.00998m, (await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity); + + // A refused buy ends, with its reason in the diary. + faulty.RejectNext = "insufficient"; + var (refused, _) = await restarted.SubmitAsync(Buy(s, 0.01m, aggressive: true), _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(restarted, 2, TimeSpan.FromSeconds(5)).ConfigureAwait(false); + Assert.AreEqual("canceled/rejected_insufficient", await ExecutionStatusAsync(s.Rig, refused!.Value).ConfigureAwait(false)); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM journal WHERE code = 'order_rejected'", r => r.Id("id")).ConfigureAwait(false)); + + // A prudent account takes no new entries; exits still go (SPEC 7.4). + await s.Rig.Db.WriteAsync(x => x.ExecuteAsync("INSERT INTO account_status (id, account_id, mode, created_at, updated_at) VALUES (@id, @a, 'prudent', @now, @now)", _none, + ("id", Ids.NewId(s.Rig.Clock)), ("a", s.Account.AccountId), ("now", s.Rig.Clock.UtcNow)), _none).ConfigureAwait(false); + Assert.AreEqual(OrderReasons.PrudentMode, (await restarted.SubmitAsync(Buy(s, 0.01m), _none).ConfigureAwait(false)).Refusal); + Assert.IsNull((await restarted.SubmitAsync(Sell(s, 0.005m), _none).ConfigureAwait(false)).Refusal); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task WhatCannotBeSoldAfterAnExitIsWrittenOffAndListedApart() + { + await using var s = await OkxAsync(TradingRig.Pepe, bid: 0.00001m, ask: 0.00001001m).ConfigureAwait(false); + await s.Manager.SubmitAsync(Buy(s, 10_000_000m, aggressive: true), _none).ConfigureAwait(false); + 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; + Assert.AreEqual(9_980_000m, held); + + s.Rig.Quote("PEPE-EUR", 0.0000099m, 0.00000991m); + var (execution, _) = await s.Manager.SubmitAsync(Sell(s, 9_900_000m) with { StartAggressive = true }, _none).ConfigureAwait(false); + await s.Rig.TurnsAsync(s.Manager, 3, _minute).ConfigureAwait(false); + Assert.AreEqual("done/filled", await ExecutionStatusAsync(s.Rig, execution!.Value).ConfigureAwait(false)); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + var residue = (await s.Rig.QueryAsync("SELECT quantity, reason FROM residues", r => (Q: r.Money("quantity"), R: r.Text("reason"))).ConfigureAwait(false)).Single(); + Assert.AreEqual((80_000m, "below_minimum"), (residue.Q, residue.R)); + Assert.AreEqual(("closed", "residue"), (await s.Rig.QueryAsync("SELECT status, exit_reason FROM trades", r => (r.Text("status"), r.Text("exit_reason"))).ConfigureAwait(false)).Single()); + Assert.IsEmpty(await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false), "No stop for a residue the bot no longer owns."); + Assert.AreEqual(80_000m, (await s.Paper.GetPositionsAsync(_none).ConfigureAwait(false)).Single().Quantity, "The residue stays on the account."); + } +} diff --git a/tests/Anubis.Tests/PaperBrokerTests.cs b/tests/Anubis.Tests/PaperBrokerTests.cs new file mode 100644 index 0000000..31fbeb1 --- /dev/null +++ b/tests/Anubis.Tests/PaperBrokerTests.cs @@ -0,0 +1,158 @@ +using Anubis.Data; +using Anubis.Trading; + +namespace Anubis.Tests; + +/// The internal simulator (issue #26, SPEC 7.3): realistic and prudent fills. +[TestClass] +public sealed class PaperBrokerTests +{ + private static readonly CancellationToken _none = CancellationToken.None; + + private static OrderRequest Limit(string id, string symbol, OrderSide side, decimal quantity, decimal price, bool postOnly = false, TimeInForce tif = TimeInForce.Gtc) => + new(id, symbol, side, OrderKind.Limit, quantity, tif) { LimitPrice = price, PostOnly = postOnly }; + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task LimitsFillOnlyWhenCrossedAndTakersPaySlippageAndTheListFee() + { + await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); + await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Okx(), account.AccountId); + await paper.DepositAsync(10000m, "EUR", _none).ConfigureAwait(false); + rig.Quote("BTC-EUR", 60000m, 60010m); + + // A buy below the market rests; touching the limit is not enough, the market must go through it. + await paper.SubmitOrderAsync(Limit("B1", "BTC-EUR", OrderSide.Buy, 0.01m, 59990m), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + rig.Quote("BTC-EUR", 59980m, 59990m); + Assert.AreEqual(OrderStatus.Submitted, (await paper.GetOrderAsync("B1", "BTC-EUR", _none).ConfigureAwait(false))!.Status, "Touched, not crossed."); + rig.Quote("BTC-EUR", 59970m, 59985m); + var filled = (await paper.GetOrderAsync("B1", "BTC-EUR", _none).ConfigureAwait(false))!; + Assert.AreEqual(OrderStatus.Filled, filled.Status); + + // Maker on OKX: 0.10% kept in the coin (SPEC 4.2), filled at the order's own price. + var fill = (await paper.GetFillsAsync(TradingRig.Start, _none).ConfigureAwait(false)).Single(); + Assert.AreEqual((59990m, 0.00001m, "BTC"), (fill.Price, fill.Fee, fill.FeeCurrency)); + Assert.AreEqual(0.00999m, (await paper.GetPositionsAsync(_none).ConfigureAwait(false)).Single().Quantity); + + // A market sale takes the bid less 5 basis points and pays the taker fee, 0.20%, in euro. + await paper.SubmitOrderAsync(new OrderRequest("S1", "BTC-EUR", OrderSide.Sell, OrderKind.Market, 0.00999m, TimeInForce.Ioc), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + var sale = (await paper.GetFillsAsync(TradingRig.Start, _none).ConfigureAwait(false)).Single(f => f.ClientOrderId == "S1"); + Assert.AreEqual(OrderRules.RoundDown(59970m * 0.9995m, 0.1m), sale.Price); + Assert.AreEqual(Math.Round(0.00999m * sale.Price * 0.002m, 18), sale.Fee); + Assert.AreEqual("EUR", sale.FeeCurrency); + + // A post-only order that would take liquidity is canceled, as on OKX. + await paper.SubmitOrderAsync(Limit("B2", "BTC-EUR", OrderSide.Buy, 0.01m, 60000m, postOnly: true), _none).ConfigureAwait(false); + var postOnly = (await paper.GetOrderAsync("B2", "BTC-EUR", _none).ConfigureAwait(false))!; + Assert.AreEqual((OrderStatus.Canceled, "post_only_would_take"), (postOnly.Status, postOnly.Reason)); + + // Partial fills follow the size on offer. + rig.Quote("BTC-EUR", 59970m, 59985m, size: 0.004m); + await paper.SubmitOrderAsync(Limit("B3", "BTC-EUR", OrderSide.Buy, 0.01m, 60000m), _none).ConfigureAwait(false); + var partial = (await paper.GetOrderAsync("B3", "BTC-EUR", _none).ConfigureAwait(false))!; + Assert.AreEqual((OrderStatus.PartiallyFilled, 0.004m), (partial.Status, partial.FilledQuantity)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task OrdersWaitForTheSimulatedLatencyAndTheBookSurvivesARestart() + { + await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); + await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); + using var paper = new PaperBroker(PaperOptions.Okx(), rig.Clock, rig.QuoteAsync, rig.Spec, new PaperBookStore(rig.Db, rig.Clock, account.AccountId)); + await paper.DepositAsync(10000m, "EUR", _none).ConfigureAwait(false); + rig.Quote("BTC-EUR", 60000m, 60010m); + await paper.SubmitOrderAsync(new OrderRequest("M1", "BTC-EUR", OrderSide.Buy, OrderKind.Market, 0.01m, TimeInForce.Ioc), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + Assert.AreEqual(OrderStatus.Submitted, (await paper.GetOrderAsync("M1", "BTC-EUR", _none).ConfigureAwait(false))!.Status, "Not before 300 ms."); + rig.Clock.Advance(TimeSpan.FromSeconds(1)); + rig.Quote("BTC-EUR", 60000m, 60010m); + Assert.AreEqual(OrderStatus.Filled, (await paper.GetOrderAsync("M1", "BTC-EUR", _none).ConfigureAwait(false))!.Status, "Within 1,000 ms."); + + // Another instance on the same account reads the same book. + using var again = new PaperBroker(PaperOptions.Okx(), rig.Clock, rig.QuoteAsync, rig.Spec, new PaperBookStore(rig.Db, rig.Clock, account.AccountId)); + Assert.AreEqual(OrderStatus.Filled, (await again.GetOrderAsync("M1", "BTC-EUR", _none).ConfigureAwait(false))!.Status); + Assert.HasCount(1, await again.GetPositionsAsync(_none).ConfigureAwait(false)); + + // Sending the same id twice gives back the same order (idempotency). + var first = await again.SubmitOrderAsync(Limit("L1", "BTC-EUR", OrderSide.Buy, 0.001m, 50000m), _none).ConfigureAwait(false); + var second = await again.SubmitOrderAsync(Limit("L1", "BTC-EUR", OrderSide.Buy, 0.001m, 50000m), _none).ConfigureAwait(false); + Assert.AreEqual(first.BrokerOrderId, second.BrokerOrderId); + Assert.HasCount(1, await again.GetOpenOrdersAsync(_none).ConfigureAwait(false)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AlpacaRulesAndFeesAreTheBrokersOwn() + { + await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Simulation).ConfigureAwait(false); + await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Alpaca(), account.AccountId); + await paper.DepositAsync(10000m, "USD", _none).ConfigureAwait(false); + rig.Quote("MSFT", 400m, 400.10m); + + // Fractions only as day orders; an opposite order on the same symbol is refused (wash trade). + var gtc = await Assert.ThrowsExactlyAsync(() => + paper.SubmitOrderAsync(Limit("F1", "MSFT", OrderSide.Buy, 1.5m, 401m), _none)).ConfigureAwait(false); + Assert.AreEqual("fractional_orders_must_be_day", gtc.Code); + await paper.SubmitOrderAsync(Limit("B1", "MSFT", OrderSide.Buy, 10.5m, 401m, tif: TimeInForce.Day), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + Assert.AreEqual(10.5m, (await paper.GetPositionsAsync(_none).ConfigureAwait(false)).Single().Quantity, "No fee in kind on stocks."); + await paper.SubmitOrderAsync(new OrderRequest("S1", "MSFT", OrderSide.Sell, OrderKind.Stop, 10m, TimeInForce.Gtc) { StopPrice = 380m }, _none).ConfigureAwait(false); + var wash = await Assert.ThrowsExactlyAsync(() => + paper.SubmitOrderAsync(Limit("B2", "MSFT", OrderSide.Buy, 1m, 401m, tif: TimeInForce.Day), _none)).ConfigureAwait(false); + Assert.AreEqual("opposite_side_order_open", wash.Code); + + // The stop reserves its shares: only the fraction can still be sold. + var reserved = await Assert.ThrowsExactlyAsync(() => + paper.SubmitOrderAsync(Limit("X1", "MSFT", OrderSide.Sell, 1m, 399m, tif: TimeInForce.Day), _none)).ConfigureAwait(false); + Assert.AreEqual("insufficient_quantity", reserved.Code); + + // A sale pays the SEC fee on its value and the CAT fee per share, each rounded up to the cent. + await paper.CancelOrderAsync("S1", "MSFT", _none).ConfigureAwait(false); + await paper.SubmitOrderAsync(Limit("X2", "MSFT", OrderSide.Sell, 10m, 399m, tif: TimeInForce.Day), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + var sale = (await paper.GetFillsAsync(TradingRig.Start, _none).ConfigureAwait(false)).Single(f => f.ClientOrderId == "X2"); + var expected = OrderRules.RoundUp(10m * sale.Price * 0.0000206m, 0.01m) + OrderRules.RoundUp(10m * 0.000003m, 0.01m); + Assert.AreEqual(expected, sale.Fee); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AfterAnAbsenceStopsFireOnTheMinuteBarsAndBuysAreCanceled() + { + await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Okx, TradingEnvironments.Simulation).ConfigureAwait(false); + await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Okx(), account.AccountId); + await paper.DepositAsync(10000m, "EUR", _none).ConfigureAwait(false); + rig.Quote("BTC-EUR", 60000m, 60010m); + await paper.SubmitOrderAsync(new OrderRequest("M1", "BTC-EUR", OrderSide.Buy, OrderKind.Market, 0.01m, TimeInForce.Ioc), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + await paper.SubmitOrderAsync(new OrderRequest("S1", "BTC-EUR", OrderSide.Sell, OrderKind.Stop, 0.00998m, TimeInForce.Gtc) { StopPrice = 57000m }, _none).ConfigureAwait(false); + await paper.SubmitOrderAsync(Limit("B1", "BTC-EUR", OrderSide.Buy, 0.01m, 55000m), _none).ConfigureAwait(false); + await paper.PumpAsync(_none).ConfigureAwait(false); + + // The bot is off for two hours; meanwhile a bar opens at 56,500, below the stop: the stop fills at the open. + var off = rig.Clock.UtcNow; + rig.Clock.Advance(TimeSpan.FromHours(2)); + IReadOnlyList bars = + [ + new(off.AddMinutes(10), 58000, 58100, 57500, 57600, 1), + new(off.AddMinutes(11), 56500, 56800, 56000, 56200, 1), + ]; + var handled = await paper.CatchUpAsync((_, _, _, _) => Task.FromResult(bars), _none).ConfigureAwait(false); + Assert.AreEqual(2, handled); + var stop = (await paper.GetFillsAsync(TradingRig.Start, _none).ConfigureAwait(false)).Single(f => f.ClientOrderId == "S1"); + Assert.AreEqual(OrderRules.RoundDown(56500m * 0.9995m, 0.1m), stop.Price); + Assert.AreEqual(off.AddMinutes(11).AddSeconds(30), stop.ExecutedAt); + var buy = (await paper.GetOrderAsync("B1", "BTC-EUR", _none).ConfigureAwait(false))!; + Assert.AreEqual((OrderStatus.Canceled, "canceled_after_absence"), (buy.Status, buy.Reason)); + } +} diff --git a/tests/Anubis.Tests/ReconcilerTests.cs b/tests/Anubis.Tests/ReconcilerTests.cs new file mode 100644 index 0000000..9ec157b --- /dev/null +++ b/tests/Anubis.Tests/ReconcilerTests.cs @@ -0,0 +1,219 @@ +using Anubis.Core; +using Anubis.Data; +using Anubis.Trading; + +namespace Anubis.Tests; + +/// Reconciliation (issue #30, SPEC 7.4): differences that explain themselves, prudent mode, the user's choice. +[TestClass] +public sealed class ReconcilerTests +{ + private static readonly CancellationToken _none = CancellationToken.None; + private static readonly TimeSpan _minute = TimeSpan.FromSeconds(60); + + private sealed record Setup(TradingRig Rig, AccountContext Account, Instrument Instrument, PaperBroker Paper, FaultyBroker Broker, OrderManager Manager, Reconciler Reconciler) + : IAsyncDisposable + { + public ValueTask DisposeAsync() => Rig.DisposeAsync(); + } + + /// An OKX account holding 0.01 BTC with its stop at 57000; "demo" compares positions and orders with the account. + private static async Task HoldingAsync(string environment = TradingEnvironments.Demo) + { + var rig = await TradingRig.CreateAsync().ConfigureAwait(false); + var account = await rig.AccountAsync(Venues.Okx, environment).ConfigureAwait(false); + var instrument = await rig.InstrumentAsync(TradingRig.Btc).ConfigureAwait(false); + var paper = rig.Paper(PaperOptions.Okx(), account.AccountId); + await paper.DepositAsync(100_000m, "EUR", _none).ConfigureAwait(false); + await rig.Ledger.RecordCapitalAsync(account.AccountId, Strategies.CryptoTrend, 10_000m, "EUR", "test", _none).ConfigureAwait(false); + rig.Quote(instrument.Symbol, 60000m, 60010m); + var broker = new FaultyBroker(paper, environment); + var manager = rig.Manager(broker, account); + await manager.SubmitAsync(new TradeIntent(instrument.Id, Strategies.CryptoTrend, OrderSide.Buy, 0.01m, OrderPurpose.Entry, Ids.NewId(rig.Clock)) + { + StopLevel = 57000m, + StartAggressive = true, + }, _none).ConfigureAwait(false); + await TurnsAsync(rig, paper, manager, 2).ConfigureAwait(false); + return new Setup(rig, account, instrument, paper, broker, manager, new Reconciler(rig.Db, rig.Clock, rig.Ledger)); + } + + private static async Task TurnsAsync(TradingRig rig, PaperBroker paper, OrderManager manager, int turns) + { + for (var i = 0; i < turns; i++) + { + rig.Refresh(); + await paper.PumpAsync(_none).ConfigureAwait(false); + await manager.TickAsync(_none).ConfigureAwait(false); + rig.Clock.Advance(_minute); + } + } + + private static Task> CodesAsync(TradingRig rig, string code) => + rig.QueryAsync("SELECT code FROM journal WHERE code = @c", r => r.Text("code"), ("c", code)); + + private static TradeIntent Entry(Setup s) => new(s.Instrument.Id, Strategies.CryptoTrend, OrderSide.Buy, 0.001m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock)); + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AnAccountThatAgreesIsCleanAndLeavesNoRow() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Clean, AccountModes.Normal), (report.Outcome, report.Mode), string.Join("; ", report.Differences.Select(d => d.Message))); + Assert.IsEmpty(report.Differences); + Assert.IsEmpty(await s.Rig.QueryAsync("SELECT id FROM reconciliations", r => r.Id("id")).ConfigureAwait(false)); + var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Clean, s.Rig.Clock.UtcNow), (state.LastOutcome, state.LastReconciledAt)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AStopThatFiredUnseenExplainsItselfWithTheLateFill() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + + // The stop fires on the account while the bot looks elsewhere: the account holds nothing, the ledger 0.00998. + s.Rig.Quote("BTC-EUR", 56000m, 56010m); + await s.Paper.PumpAsync(_none).ConfigureAwait(false); + Assert.IsEmpty(await s.Paper.GetPositionsAsync(_none).ConfigureAwait(false)); + Assert.IsNotNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + + var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Healed, AccountModes.Normal), (report.Outcome, report.Mode), string.Join("; ", report.Differences.Select(d => d.Message))); + Assert.IsNull(await s.Rig.Ledger.PositionAsync(s.Account.AccountId, s.Instrument.Id, Strategies.CryptoTrend, _none).ConfigureAwait(false)); + Assert.AreEqual("stop", (await s.Rig.QueryAsync("SELECT exit_reason FROM trades", r => r.Text("exit_reason")).ConfigureAwait(false)).Single()); + Assert.HasCount(1, await CodesAsync(s.Rig, "reconcile_healed").ConfigureAwait(false)); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM reconciliations WHERE outcome = 'healed'", r => r.Id("id")).ConfigureAwait(false)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AnUnexplainedDifferenceMakesTheAccountPrudentUntilItClearsOrTheUserChooses() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + + // Part of the coin is gone from the account and nothing explains it. + s.Broker.Missing["BTC-EUR"] = 0.004m; + var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Different, AccountModes.Prudent), (report.Outcome, report.Mode)); + Assert.HasCount(1, report.Differences, string.Join("; ", report.Differences.Select(d => d.Message))); + Assert.AreEqual((DifferenceKinds.Position, 0.00998m, 0.00598m), (report.Differences.Single().Kind, report.Differences.Single().Ledger, report.Differences.Single().Broker)); + Assert.HasCount(1, await s.Rig.QueryAsync("SELECT id FROM notifications WHERE kind = 'account_prudent'", r => r.Id("id")).ConfigureAwait(false)); + + // Prudent: no new purchases, sales still go. + Assert.AreEqual(OrderReasons.PrudentMode, (await s.Manager.SubmitAsync(Entry(s), _none).ConfigureAwait(false)).Refusal); + Assert.IsNull((await s.Manager.SubmitAsync(Entry(s) with { Side = OrderSide.Sell, Purpose = OrderPurpose.Exit }, _none).ConfigureAwait(false)).Refusal); + await s.Rig.Db.WriteAsync(x => x.ExecuteAsync("UPDATE executions SET status = 'canceled'", _none), _none).ConfigureAwait(false); + + // It clears within 15 minutes: back to normal by itself, notified once. + s.Rig.Clock.Advance(TimeSpan.FromMinutes(5)); + s.Broker.Missing.Clear(); + report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Clean, AccountModes.Normal), (report.Outcome, report.Mode)); + Assert.HasCount(1, await CodesAsync(s.Rig, "account_normal").ConfigureAwait(false)); + + // Again, and this time it stays: after 15 minutes only the user can close it, and it is not reported twice. + s.Broker.Missing["BTC-EUR"] = 0.004m; + await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + s.Rig.Clock.Advance(TimeSpan.FromMinutes(16)); + report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual(AccountModes.Prudent, report.Mode); + var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); + 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)); + + // 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); + state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); + Assert.AreEqual((AccountModes.Normal, ReconcileOutcomes.UserAligned), (state.Mode, state.LastOutcome)); + Assert.HasCount(1, await CodesAsync(s.Rig, "account_resolved").ConfigureAwait(false)); + + // The stop follows the new quantity and the next round is clean. + await s.Manager.AlignStopsAsync(_none).ConfigureAwait(false); + report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual(ReconcileOutcomes.Clean, report.Outcome, string.Join("; ", report.Differences.Select(d => d.Message))); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AnUnknownOrderOfThisInstallationIsReportedAndCanBeLeftOut() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + var stray = ClientOrderIds.Build(TradingRig.Installation, TradingEnvironments.Demo, Strategies.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(Guid.NewGuid()), 1); + await s.Paper.SubmitOrderAsync(new OrderRequest(stray, "BTC-EUR", OrderSide.Buy, OrderKind.Limit, 0.001m, TimeInForce.Gtc) { LimitPrice = 50000m }, _none).ConfigureAwait(false); + + // Someone else's order on the same account is none of the bot's business. + await s.Paper.SubmitOrderAsync(new OrderRequest("manual-1", "BTC-EUR", OrderSide.Buy, OrderKind.Limit, 0.001m, TimeInForce.Gtc) { LimitPrice = 50000m }, _none) + .ConfigureAwait(false); + + var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + var difference = report.Differences.Single(); + Assert.AreEqual((DifferenceKinds.Order, stray), (difference.Kind, difference.Subject)); + Assert.AreEqual(AccountModes.Prudent, report.Mode); + + await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Exclude, "test", _none).ConfigureAwait(false); + report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual((ReconcileOutcomes.Clean, AccountModes.Normal), (report.Outcome, report.Mode)); + var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); + CollectionAssert.AreEqual(new[] { difference.Fingerprint }, state.Excluded.ToArray()); + Assert.AreEqual(ReconcileOutcomes.UserExcluded, + (await s.Rig.QueryAsync("SELECT outcome FROM reconciliations ORDER BY occurred_at DESC, id DESC", r => r.Text("outcome")).ConfigureAwait(false))[0]); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AMissingStopIsPutBackAndInTheSimulatorOnlyTheLedgerIsCompared() + { + await using var s = await HoldingAsync(TradingEnvironments.Simulation).ConfigureAwait(false); + var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single(); + await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false); + + // Positions the simulator does not show are not compared there: only its own book exists. + s.Broker.Missing["BTC-EUR"] = 0.004m; + var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual(ReconcileOutcomes.Healed, report.Outcome, string.Join("; ", report.Differences.Select(d => d.Message))); + var now = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single(); + Assert.AreEqual((0.00998m, 57000m), (now.Quantity, now.StopPrice)); + Assert.AreNotEqual(stop.ClientOrderId, now.ClientOrderId); + + // A ledger that does not agree with itself is a difference everywhere. + await s.Rig.Db.WriteAsync(x => x.ExecuteAsync("UPDATE positions SET quantity = 0.5", _none), _none).ConfigureAwait(false); + report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false); + Assert.AreEqual(DifferenceKinds.Ledger, report.Differences.Single(d => d.Kind == DifferenceKinds.Ledger).Kind); + Assert.AreEqual(AccountModes.Prudent, report.Mode); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task AKeyThatMayWithdrawStopsPurchasesUntilItIsReplaced() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + s.Broker.KeyCanWithdraw = true; + var permissions = await s.Reconciler.CheckKeyAsync(s.Manager, _none).ConfigureAwait(false); + Assert.IsTrue(permissions!.CanWithdraw); + var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false); + Assert.AreEqual((AccountModes.Prudent, Reconciler.KeyReason, true), (state.Mode, state.ModeReason, state.KeyCanWithdraw)); + Assert.IsFalse(state.WaitsForUser(s.Rig.Clock.UtcNow.AddHours(1), s.Reconciler.Grace), "Not a choice to make: the key has to change."); + Assert.AreEqual(OrderReasons.PrudentMode, (await s.Manager.SubmitAsync(Entry(s), _none).ConfigureAwait(false)).Refusal); + + // A clean round does not bring it back, nor does the user's choice. + 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)); + } + + [TestMethod] + [TestCategory(Category.Simulation)] + public async Task ABrokerThatDoesNotAnswerIsWrittenDownOnce() + { + await using var s = await HoldingAsync().ConfigureAwait(false); + s.Broker.Down = true; + Assert.AreEqual(ReconcileOutcomes.Unreachable, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome); + Assert.AreEqual(ReconcileOutcomes.Unreachable, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome); + Assert.HasCount(1, await CodesAsync(s.Rig, "reconcile_unreachable").ConfigureAwait(false)); + Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Mode, "Silence is not a difference."); + } +} diff --git a/tests/Anubis.Tests/TradingSupport.cs b/tests/Anubis.Tests/TradingSupport.cs index 255e124..f214f53 100644 --- a/tests/Anubis.Tests/TradingSupport.cs +++ b/tests/Anubis.Tests/TradingSupport.cs @@ -69,6 +69,15 @@ internal sealed class TradingRig : IAsyncDisposable public void Quote(string symbol, decimal bid, decimal ask, decimal? size = null) => _quotes[symbol] = new Quote(symbol, bid, ask, Clock.UtcNow) { BidSize = size, AskSize = size }; + /// The same prices, read again now: the feed is alive and the market has not moved. + public void Refresh() + { + foreach (var (symbol, quote) in _quotes) + { + _quotes[symbol] = quote with { At = Clock.UtcNow }; + } + } + public Task QuoteAsync(string symbol, CancellationToken cancellationToken) => Task.FromResult(QuotesDown ? null : _quotes.TryGetValue(symbol, out var quote) ? quote : null); @@ -89,6 +98,7 @@ internal sealed class TradingRig : IAsyncDisposable { var manager = new OrderManager(Db, Clock, Ledger, broker, account, Installation, FindAsync, QuoteAsync) { + StepTime = TimeSpan.FromSeconds(60), ConfirmPrice = QuoteAsync, MarketOpen = marketOpen ?? ((_, _) => Task.FromResult(true)), PendingSplit = pendingSplit, @@ -102,6 +112,7 @@ internal sealed class TradingRig : IAsyncDisposable { for (var i = 0; i < turns; i++) { + Refresh(); if (manager.Broker is PaperBroker paper) { await paper.PumpAsync(CancellationToken.None).ConfigureAwait(false); @@ -128,3 +139,92 @@ internal sealed class TradingRig : IAsyncDisposable await Test.DisposeAsync().ConfigureAwait(false); } } + +/// A broker in front of another one that can lose answers, refuse orders, repeat fills or go silent (fault tests, SPEC 7.4). +internal sealed class FaultyBroker(IBrokerAccount inner, string? environment = null) : IBrokerAccount +{ + /// Quantities the account no longer shows, by symbol: a difference the ledger cannot explain. + public Dictionary Missing { get; } = new(StringComparer.Ordinal); + + /// The key reports that it may withdraw. + public bool KeyCanWithdraw { get; set; } + + /// The next order reaches the broker but its answer is lost. + public bool LoseNextAnswer { get; set; } + + /// The next order is refused with this code. + public string? RejectNext { get; set; } + + /// Every fill is reported twice. + public bool DuplicateFills { get; set; } + + /// Every call fails as if the network were down. + public bool Down { get; set; } + + public List Sent { get; } = []; + + public string Venue => inner.Venue; + + public string Environment => environment ?? inner.Environment; + + public BrokerCapabilities Capabilities => inner.Capabilities; + + public Task ServerTimeAsync(CancellationToken cancellationToken) => Guard(() => inner.ServerTimeAsync(cancellationToken)); + + public Task GetAccountAsync(CancellationToken cancellationToken) => Guard(() => inner.GetAccountAsync(cancellationToken)); + + public async Task> GetPositionsAsync(CancellationToken cancellationToken) + { + var positions = await Guard(() => inner.GetPositionsAsync(cancellationToken)).ConfigureAwait(false); + return [.. positions.Select(p => p with { Quantity = p.Quantity - Missing.GetValueOrDefault(p.Symbol) })]; + } + + public Task> GetOpenOrdersAsync(CancellationToken cancellationToken) => Guard(() => inner.GetOpenOrdersAsync(cancellationToken)); + + public async Task SubmitOrderAsync(OrderRequest request, CancellationToken cancellationToken) + { + if (Down) + { + throw new BrokerException("rete giù", retryable: true); + } + + if (RejectNext is { } code) + { + RejectNext = null; + throw new OrderRejectedException("rifiutato", code); + } + + Sent.Add(request); + var answer = await inner.SubmitOrderAsync(request, cancellationToken).ConfigureAwait(false); + if (LoseNextAnswer) + { + LoseNextAnswer = false; + throw new BrokerException("risposta persa", retryable: true); + } + + return answer; + } + + public Task CancelOrderAsync(string clientOrderId, string symbol, CancellationToken cancellationToken) => Guard(async () => + { + await inner.CancelOrderAsync(clientOrderId, symbol, cancellationToken).ConfigureAwait(false); + return true; + }); + + public Task GetOrderAsync(string clientOrderId, string symbol, CancellationToken cancellationToken) => Guard(() => inner.GetOrderAsync(clientOrderId, symbol, cancellationToken)); + + public async Task> GetFillsAsync(DateTimeOffset since, CancellationToken cancellationToken) + { + var fills = await Guard(() => inner.GetFillsAsync(since, cancellationToken)).ConfigureAwait(false); + return DuplicateFills ? [.. fills, .. fills] : fills; + } + + public Task> GetCashActivitiesAsync(DateTimeOffset since, CancellationToken cancellationToken) => Guard(() => inner.GetCashActivitiesAsync(since, cancellationToken)); + + public Task GetKeyPermissionsAsync(CancellationToken cancellationToken) => + KeyCanWithdraw ? Task.FromResult(new KeyPermissions(true, true, true, "lettura, ordini, prelievi")) : Guard(() => inner.GetKeyPermissionsAsync(cancellationToken)); + + public IAsyncEnumerable StreamOrderUpdatesAsync(CancellationToken cancellationToken) => inner.StreamOrderUpdatesAsync(cancellationToken); + + private Task Guard(Func> call) => Down ? throw new BrokerException("rete giù", retryable: true) : call(); +}