Fix the defects found by the Phase 3 review (refs #33 #26 #27 #28 #29 #30 #31 #32)
ci / test (postgres) (push) Successful in 3m9s
ci / test (sqlite) (push) Successful in 2m28s
ci / test (postgres) (pull_request) Successful in 4m1s
ci / test (sqlite) (pull_request) Failing after 2m31s
ci / image (pull_request) Skipped
ci / image (push) Successful in 1m34s

- Splits multiply only the shares held before the ex-date. Old splits in
  the table no longer inflate positions bought later; the simulator's book
  follows the same split once, together with the ledger.
- Dividends pay the simulator once on the ex-date holdings, and each
  payment is marked in the same write, so a failure never pays twice.
- Positions are marked to market before every equity photograph; until
  now equity, drawdown and the Conti screen stayed at the last fill.
- A sale with no price keeps its broker stops until it can really leave;
  a purchase with no price is dropped.
- A refused sale is retried with pauses of 1, 2, 4, 8 and 15 minutes
  instead of every tick, and one failing execution no longer stops the
  stop alignment of the whole account.
- On Alpaca a purchase clears the sell stops of every strategy on the
  symbol and holds them until it ends.
- OKX order lookups treat only 51603 as unknown; any other failure is an
  error, so a lost order is never sent twice.
- The reconcile choice and key removal forms now require the antiforgery
  token; the choice re-runs the comparison and is only offered after the
  15 minutes; stale differences are dropped.
- A replaced key that cannot withdraw releases the prudent mode.
- Re-entering the same euro capital for Alpaca changes nothing; a new
  amount converts only the euro difference at today's rate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-11-10 21:47:36 +01:00
1 parent 60d5ebbc05
commit 1a0912e436
17 files changed
+358 -89

No files matched your search

+15 -2
View File
@@ -92,6 +92,12 @@ public sealed class TradingServiceTests
Assert.AreEqual(2200m, await BookAsync(Venues.Alpaca, "USD").ConfigureAwait(false), "Funded once, not again at every call.");
var shares = await rig.QueryAsync("SELECT strategy, capital FROM strategy_state ORDER BY strategy", r => (r.Text("strategy"), r.Money("capital"))).ConfigureAwait(false);
CollectionAssert.AreEqual(new[] { (Strategies.CryptoTrend, 1000m), (Strategies.EquityMeanrev, 550m), (Strategies.EquityMomentum, 1650m) }, shares.ToArray());
// The dollar falls: saving the same euro again changes nothing, and only a new euro amount is converted at today's rate.
await new FxStore(rig.Db, rig.Clock).UpsertAsync([new FxRate("EUR", "USD", new DateOnly(2026, 11, 10), 1.05m, "ecb")], _none).ConfigureAwait(false);
Assert.AreEqual(2200m, (await service.SetCapitalAsync(Venues.Alpaca, 2000m, "test", _none).ConfigureAwait(false)).Amount);
Assert.AreEqual(3250m, (await service.SetCapitalAsync(Venues.Alpaca, 3000m, "test", _none).ConfigureAwait(false)).Amount);
Assert.HasCount(2, await rig.QueryAsync("SELECT id FROM capital_changes WHERE broker = 'alpaca'", r => r.Id("id")).ConfigureAwait(false));
}
[TestMethod]
@@ -107,10 +113,11 @@ public sealed class TradingServiceTests
await service.Ledger.RecordCapitalAsync(account.AccountId, Strategies.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false);
await service.Ledger.RecordFillAsync(rig.Fill(account, msft, Strategies.EquityMomentum, OrderSide.Buy, 10m, 400m, feeCurrency: "USD"), _none).ConfigureAwait(false);
// Bought on Tuesday; Wednesday is the ex-date of 0,75 USD a share; Thursday a 2-for-1 split.
// Bought on Tuesday; Wednesday is the ex-date of 0,75 USD a share; Thursday a 2-for-1 split. A split of last
// 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") })
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") })
{
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,
@@ -122,6 +129,7 @@ public sealed class TradingServiceTests
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
Assert.IsEmpty(await DividendsAsync().ConfigureAwait(false), "Nothing before the ex-date.");
Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity, "An old split is not applied to new shares.");
rig.Clock.Advance(TimeSpan.FromDays(1));
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
@@ -137,5 +145,10 @@ public sealed class TradingServiceTests
Assert.HasCount(1, await DividendsAsync().ConfigureAwait(false));
Assert.HasCount(1, await rig.QueryAsync("SELECT code FROM journal WHERE code = 'split_applied'", r => r.Text("code")).ConfigureAwait(false));
Assert.IsEmpty(http.Requests);
// 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);
Assert.AreEqual(230.05m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.LastPrice);
}
}