The strategy contract of SPEC 5.3 (IStrategy, StrategyContext, Intent) is shared by backtest and live: the context exposes bars only up to the decision, adjusted and anchored to that day, and reading past it throws. The engine runs each session as ordered events (corporate actions, open, session, close), decides on the close and executes at the next open with the simulator's price lists, slippage tiers, tick and lot rounding, minimums and no leverage. Corrective and risk hooks are wired in and pass everything through until Phase 5. One decision cycle can restart from a recorded book (SPEC 12.3). A scripted toy strategy reproduces a result computed by hand to the cent on OKX and Alpaca costs. The Phase 3 class Trading.Strategies becomes StrategyNames so the Anubis.Strategies namespace can exist. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
234 lines
15 KiB
C#
234 lines
15 KiB
C#
using Anubis.Core;
|
|
using Anubis.Data;
|
|
using Anubis.Trading;
|
|
|
|
namespace Anubis.Tests;
|
|
|
|
/// <summary>Reconciliation (issue #30, SPEC 7.4): differences that explain themselves, prudent mode, the user's choice.</summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>An OKX account holding 0.01 BTC with its stop at 57000; "demo" compares positions and orders with the account.</summary>
|
|
private static async Task<Setup> 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, StrategyNames.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, StrategyNames.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<IReadOnlyList<string>> 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, StrategyNames.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, StrategyNames.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, StrategyNames.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));
|
|
|
|
// 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, StrategyNames.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, StrategyNames.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<InvalidOperationException>(() => s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none)).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]
|
|
[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.");
|
|
}
|
|
}
|