Finish an interrupted rebuild and replay the past without acting on it
The independent review found that a rebuild cut halfway was never finished (stops canceled or lowered), that old stop executions replayed from the broker queued a real sale, that a pending "close all" survived the restore, and that reconciliation and corporate actions touched accounts not rebuilt yet. The rebuild now keeps its starting point until it ends, protects the bot before the first row, meets the splits at their date and leaves the ledger alone until it is done. Dividends count the shares of after a split. A failed restore of a missing SQLite file leaves no empty database, and a failed migration can be tried again from the choice page. refs #69 #75 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
24e20584d4
commit
5ef54b6f2a
17 files changed
+613
-80
No files matched your search
@@ -72,6 +72,33 @@ public sealed class TradingServiceTests
|
||||
}, (await StrategiesAsync(rig).ConfigureAwait(false)).ToArray(), "A strategy with a history stays where it is.");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task AnAccountOfTheBrokerWaitsForItsRebuildBeforeAnythingTouchesIt()
|
||||
{
|
||||
await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false);
|
||||
var http = new FakeHttp().On(_ => true, _ => FakeHttp.Respond("{}", System.Net.HttpStatusCode.BadRequest));
|
||||
using var service = Service(rig, http, new Dictionary<string, string> { ["ALPACA_API_KEY_DEMO"] = "test-key", ["ALPACA_API_SECRET_DEMO"] = "test-secret" });
|
||||
await service.StartAsync(_none).ConfigureAwait(false);
|
||||
var paper = service.Managers.Single(m => m.Account.Environment == TradingEnvironments.Demo);
|
||||
Assert.IsTrue(paper.AwaitingRebuild, "The broker answers nothing: the rebuild is still to be done.");
|
||||
Assert.IsTrue(service.Managers.Where(m => m != paper).All(m => !m.AwaitingRebuild));
|
||||
CollectionAssert.Contains(service.AccountsToRebuild.ToList(), paper.Account.Name);
|
||||
|
||||
// A split and a dividend in the table, and the five minutes of the reconciliation: nothing reaches the account.
|
||||
var msft = await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false);
|
||||
await service.Ledger.RecordCapitalAsync(paper.Account.AccountId, StrategyNames.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false);
|
||||
await service.Ledger.RecordFillAsync(rig.Fill(paper.Account, msft, StrategyNames.EquityMomentum, OrderSide.Buy, 10m, 400m, feeCurrency: "USD"), _none).ConfigureAwait(false);
|
||||
await rig.Db.WriteAsync(s => s.ExecuteAsync("INSERT INTO corporate_actions (id, symbol, kind, ex_date, ratio, source, source_key, created_at) VALUES (@id, 'MSFT', 'split', '2026-11-11', @r, 'test', 'k', @now)",
|
||||
_none, ("id", Ids.NewId(rig.Clock)), ("r", 2m), ("now", rig.Clock.UtcNow)), _none).ConfigureAwait(false);
|
||||
rig.Clock.Advance(TimeSpan.FromDays(2));
|
||||
var before = http.Requests.Count;
|
||||
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
|
||||
await service.ReconcileAllAsync(_none).ConfigureAwait(false);
|
||||
Assert.AreEqual(10m, (await service.Ledger.PositionAsync(paper.Account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.Quantity);
|
||||
Assert.AreEqual(before, http.Requests.Count, "The reconciliation does not ask anything either.");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task TheSimulatorHoldsTheMoneyItsLedgersGive()
|
||||
@@ -121,7 +148,8 @@ public sealed class TradingServiceTests
|
||||
// month is still in the table: it came before the purchase and must change nothing.
|
||||
await rig.Db.WriteAsync(async s =>
|
||||
{
|
||||
foreach (var (kind, day, ratio, cash, key) in new[] { ("cash_dividend", "2026-11-11", (decimal?)null, (decimal?)0.75m, "div-msft"), ("split", "2026-11-12", 2m, null, "split-msft"), ("split", "2026-10-01", 3m, null, "split-msft-old") })
|
||||
foreach (var (kind, day, ratio, cash, key) in new[] { ("cash_dividend", "2026-11-11", (decimal?)null, (decimal?)0.75m, "div-msft"), ("split", "2026-11-12", 2m, null, "split-msft"), ("split", "2026-10-01", 3m, null, "split-msft-old"),
|
||||
("cash_dividend", "2026-11-14", null, 0.50m, "div-msft-2") })
|
||||
{
|
||||
await s.ExecuteAsync("INSERT INTO corporate_actions (id, symbol, kind, ex_date, ratio, cash_amount, currency, source, source_key, created_at) " +
|
||||
"VALUES (@id, 'MSFT', @k, @d, @r, @c, 'USD', 'test', @key, @now)", _none,
|
||||
@@ -150,6 +178,12 @@ public sealed class TradingServiceTests
|
||||
Assert.HasCount(1, await rig.QueryAsync("SELECT code FROM journal WHERE code = 'split_applied'", r => r.Text("code")).ConfigureAwait(false));
|
||||
Assert.IsEmpty(http.Requests);
|
||||
|
||||
// A dividend after the split pays the new shares, sold since or not.
|
||||
rig.Clock.Advance(TimeSpan.FromDays(3));
|
||||
await service.Ledger.RecordFillAsync(rig.Fill(account, msft, StrategyNames.EquityMomentum, OrderSide.Sell, 4m, 200m, feeCurrency: "USD"), _none).ConfigureAwait(false);
|
||||
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
|
||||
CollectionAssert.AreEquivalent(new[] { 7.5m, 10m }, (await DividendsAsync().ConfigureAwait(false)).ToArray(), "20 new shares at 0,50.");
|
||||
|
||||
// Every photograph values the positions at the market, not at their last fill.
|
||||
service.Quotes.Put(new Quote("MSFT", 230m, 230.10m, rig.Clock.UtcNow));
|
||||
await service.PhotographAsync(_none).ConfigureAwait(false);
|
||||
|
||||
Reference in new issue
Block a user