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 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 20:40:21 +01:00
1 parent 1419457fe4
commit e3caa26207
5 files changed
+813 -6

No files matched your search

+22 -6
View File
@@ -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<OrderRow?> 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<int> InsertOrderAsync(DbSession s, OrderRow row, CancellationToken cancellationToken) => s.ExecuteAsync(
/// <summary>A new order and its first event: the creation is part of the recorded history (SPEC 7.2).</summary>
private async Task<int> 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<int> 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,