A difference that comes and goes no longer makes the account ask about a stop the user has already chosen on. The balance of the restrictions adds each period to what was published, so a change of capital does not rewrite the past. An image is published only under a tag that exists, and a file that is not part of it no longer marks it as unclean. refs #70 #74 #75 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
440 lines
29 KiB
C#
440 lines
29 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);
|
|
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.IsEmpty(state.Differences);
|
|
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "Also with nothing left to align: otherwise it would stay prudent forever.");
|
|
Assert.IsFalse(state.HasChoices);
|
|
s.Broker.Missing["BTC-EUR"] = 0.004m;
|
|
|
|
// With the broker silent nothing is compared, so the choice changes nothing.
|
|
s.Broker.Down = true;
|
|
Assert.AreEqual(ReconcileResolution.NotCompared, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
|
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).Mode);
|
|
s.Broker.Down = 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, 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 AMissingStopIsNeverLeftOutByTheUsersChoice()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
|
|
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
|
|
s.Broker.RefuseStops = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
Assert.IsTrue(report.Differences.Any(d => d.Kind == DifferenceKinds.Stop), string.Join("; ", report.Differences.Select(d => d.Message)));
|
|
foreach (var choice in new[] { ReconcileChoice.Align, ReconcileChoice.Exclude })
|
|
{
|
|
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, choice, "test", _none).ConfigureAwait(false));
|
|
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.AreEqual((AccountModes.Prudent, Reconciler.StopReason), (state.Mode, state.ModeReason));
|
|
Assert.IsFalse(state.Excluded.Any(e => e.StartsWith(DifferenceKinds.Stop, StringComparison.Ordinal)), string.Join("; ", state.Excluded));
|
|
}
|
|
|
|
// The stop is back within the new 15 minutes: normal by itself.
|
|
s.Broker.RefuseStops = false;
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
|
|
report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
Assert.AreEqual(AccountModes.Normal, report.Mode, string.Join("; ", report.Differences.Select(d => d.Message)));
|
|
}
|
|
|
|
/// <summary>Coin gone from the account and no stop left: prudent, and past the 15 minutes.</summary>
|
|
private static async Task GoneWithItsStopAsync(Setup s, decimal missing)
|
|
{
|
|
s.Broker.Missing["BTC-EUR"] = missing;
|
|
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
|
|
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
|
|
s.Broker.RefuseStops = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
var report = await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
CollectionAssert.AreEquivalent(new[] { DifferenceKinds.Position, DifferenceKinds.Stop }, report.Differences.Select(d => d.Kind).ToArray());
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
[DataRow(ReconcileChoice.Align)]
|
|
[DataRow(ReconcileChoice.Exclude)]
|
|
public async Task APositionThatLeftWithItsStopDoesNotKeepTheAccountPrudentAfterTheChoice(ReconcileChoice choice)
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
await GoneWithItsStopAsync(s, 0.00998m).ConfigureAwait(false);
|
|
|
|
// No stop can cover what is not there: whatever the user chooses, none is waited for.
|
|
Assert.AreEqual(ReconcileResolution.Normal, await s.Reconciler.ResolveAsync(s.Manager, choice, "test", _none).ConfigureAwait(false));
|
|
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.AreEqual(AccountModes.Normal, state.Mode);
|
|
Assert.IsEmpty(state.Differences);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
|
|
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)));
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
public async Task AStopMissingOnWhatIsLeftStaysAndAlignTakesUpTheExcludedPosition()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
await GoneWithItsStopAsync(s, 0.004m).ConfigureAwait(false);
|
|
|
|
// 0.00598 are still on the account without their stop: that is not left out, and only for that quantity.
|
|
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Exclude, "test", _none).ConfigureAwait(false));
|
|
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.AreEqual((DifferenceKinds.Stop, 0.00598m, 0m), (state.Differences.Single().Kind, state.Differences.Single().Ledger, state.Differences.Single().Broker));
|
|
|
|
// The stop asked for what the ledger says never goes through: the way out is to take the account's number.
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace));
|
|
Assert.IsTrue(state.HasChoices, "The position left out can still be aligned.");
|
|
Assert.AreEqual(ReconcileResolution.StopsMissing, 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.IsEmpty(state.Excluded);
|
|
Assert.IsFalse(state.HasChoices);
|
|
|
|
s.Broker.RefuseStops = false;
|
|
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
|
Assert.AreEqual(0.00598m, (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single().Quantity);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
public async Task AfterTheChoiceTheAccountReturnsToNormalWheneverTheStopIsBack()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
|
|
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
|
|
s.Broker.RefuseStops = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
|
|
|
// Much later than 15 minutes: the user has already chosen, nothing is left to ask.
|
|
s.Rig.Clock.Advance(TimeSpan.FromHours(3));
|
|
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
|
s.Broker.RefuseStops = false;
|
|
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
|
Assert.HasCount(1, await CodesAsync(s.Rig, "account_normal").ConfigureAwait(false));
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
public async Task ANewDifferenceWhileAStopIsAwaitedHasItsOwnFifteenMinutes()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
|
|
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
|
|
s.Broker.RefuseStops = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false);
|
|
|
|
// Coin leaves the account while the stop is awaited: past its 15 minutes that one waits for the user (SPEC 7.4).
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(30));
|
|
s.Broker.Missing["BTC-EUR"] = 0.004m;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
var state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.IsFalse(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "It may still explain itself.");
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
s.Broker.Missing.Clear();
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
state = await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false);
|
|
Assert.AreEqual(DifferenceKinds.Stop, state.Differences.Single().Kind);
|
|
Assert.IsTrue(state.WaitsForUser(s.Rig.Clock.UtcNow, s.Reconciler.Grace), "Gone too late.");
|
|
Assert.AreNotEqual(Reconciler.StopReason, state.ModeReason);
|
|
s.Broker.RefuseStops = false;
|
|
Assert.AreEqual(AccountModes.Prudent, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Mode);
|
|
Assert.AreEqual(ReconcileResolution.Normal, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
public async Task ADifferenceThatComesAndGoesDoesNotMakeTheStopAwaitedAskAgain()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
var stop = (await s.Paper.GetOpenOrdersAsync(_none).ConfigureAwait(false)).Single();
|
|
await s.Paper.CancelOrderAsync(stop.ClientOrderId, stop.Symbol, _none).ConfigureAwait(false);
|
|
s.Broker.RefuseStops = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(16));
|
|
Assert.AreEqual(ReconcileResolution.StopsMissing, await s.Reconciler.ResolveAsync(s.Manager, ReconcileChoice.Align, "test", _none).ConfigureAwait(false));
|
|
|
|
// Coin leaves the account and is back 5 minutes later, the stop still refused: the user has nothing to choose.
|
|
s.Broker.Missing["BTC-EUR"] = 0.004m;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(5));
|
|
s.Broker.Missing.Clear();
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
Assert.AreEqual(Reconciler.StopReason, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).ModeReason);
|
|
s.Rig.Clock.Advance(TimeSpan.FromMinutes(20));
|
|
s.Broker.RefuseStops = false;
|
|
Assert.AreEqual(AccountModes.Normal, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).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.");
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Simulation)]
|
|
public async Task AKeyTheBrokerNoLongerAcceptsIsSaidToTheUserOnce()
|
|
{
|
|
await using var s = await HoldingAsync().ConfigureAwait(false);
|
|
s.Broker.Down = true;
|
|
await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false);
|
|
s.Broker.KeyRefused = true;
|
|
|
|
// At the start the key is read first, and the account may never get to its reconciliation.
|
|
Assert.IsNull(await s.Reconciler.CheckKeyAsync(s.Manager, _none).ConfigureAwait(false));
|
|
Assert.AreEqual(ReconcileOutcomes.KeyRefused, (await s.Reconciler.StateAsync(s.Account.AccountId, _none).ConfigureAwait(false)).LastOutcome);
|
|
Assert.AreEqual(ReconcileOutcomes.KeyRefused, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome);
|
|
Assert.HasCount(1, await CodesAsync(s.Rig, "key_refused").ConfigureAwait(false));
|
|
var sent = await s.Rig.Db.ReadAsync(r => r.CountAsync("SELECT COUNT(*) FROM notifications WHERE message LIKE @b", ("b", "%non accetta più la chiave%")), _none).ConfigureAwait(false);
|
|
Assert.AreEqual(1, sent, "It reaches the phone too.");
|
|
|
|
s.Broker.Down = false;
|
|
Assert.AreEqual(ReconcileOutcomes.Clean, (await s.Reconciler.ReconcileAsync(s.Manager, _none).ConfigureAwait(false)).Outcome);
|
|
}
|
|
|
|
[TestMethod]
|
|
[TestCategory(Category.Unit)]
|
|
[DataRow(401, """{"code":"50111","msg":"Invalid OK-ACCESS-KEY","data":[]}""", true)]
|
|
[DataRow(401, """{"code":"50110","msg":"Your IP is not included in your API key's IP whitelist.","data":[]}""", true)]
|
|
[DataRow(401, """{"code":"50102","msg":"Timestamp request expired","data":[]}""", false)]
|
|
[DataRow(403, """{"message":"forbidden."}""", true)]
|
|
[DataRow(404, """{"message":"position does not exist"}""", false)]
|
|
public void ARefusedKeyIsToldFromOtherAnswers(int status, string body, bool refused)
|
|
{
|
|
using var client = new HttpClient();
|
|
var http = new TradingHttp(client, new SystemClock(), "broker", (_, _) => true);
|
|
Assert.AreEqual(refused, http.Failure(new Uri("https://broker.example/v2/positions"), new HttpAnswer((System.Net.HttpStatusCode)status, body)).KeyRefused);
|
|
}
|
|
}
|