Add the event-driven backtest engine and the strategy interface (refs #34)
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>
This commit is contained in:
1 parent
6d3ef8e7b5
commit
d97304d7b9
19 files changed
+1592
-78
No files matched your search
@@ -36,9 +36,9 @@ public sealed class TradingServiceTests
|
||||
// Every strategy starts in observation, crypto on OKX and stocks on Alpaca.
|
||||
CollectionAssert.AreEqual(new[]
|
||||
{
|
||||
(Strategies.CryptoTrend, "OKX simulatore", Strategies.Observation),
|
||||
(Strategies.EquityMeanrev, "Alpaca simulatore", Strategies.Observation),
|
||||
(Strategies.EquityMomentum, "Alpaca simulatore", Strategies.Observation),
|
||||
(StrategyNames.CryptoTrend, "OKX simulatore", StrategyNames.Observation),
|
||||
(StrategyNames.EquityMeanrev, "Alpaca simulatore", StrategyNames.Observation),
|
||||
(StrategyNames.EquityMomentum, "Alpaca simulatore", StrategyNames.Observation),
|
||||
}, (await StrategiesAsync(rig).ConfigureAwait(false)).ToArray());
|
||||
|
||||
// A second start finds what is there.
|
||||
@@ -55,16 +55,16 @@ public sealed class TradingServiceTests
|
||||
await using var rig = await TradingRig.CreateAsync().ConfigureAwait(false);
|
||||
var accounts = await AccountSetup.EnsureAsync(rig.Db, rig.Clock, alpacaDemo: false, okxDemo: false, _none).ConfigureAwait(false);
|
||||
var simulator = accounts.Single(a => a.Broker == Venues.Alpaca);
|
||||
await rig.Ledger.RecordCapitalAsync(simulator.AccountId, Strategies.EquityMeanrev, 500m, "USD", "test", _none).ConfigureAwait(false);
|
||||
await rig.Ledger.RecordCapitalAsync(simulator.AccountId, StrategyNames.EquityMeanrev, 500m, "USD", "test", _none).ConfigureAwait(false);
|
||||
|
||||
accounts = await AccountSetup.EnsureAsync(rig.Db, rig.Clock, alpacaDemo: true, okxDemo: false, _none).ConfigureAwait(false);
|
||||
Assert.HasCount(3, accounts);
|
||||
Assert.IsFalse(accounts.Any(a => a.Environment == TradingEnvironments.Live), "Never a real account in this phase (SPEC 2.2).");
|
||||
CollectionAssert.AreEqual(new[]
|
||||
{
|
||||
(Strategies.CryptoTrend, "OKX simulatore", Strategies.Observation),
|
||||
(Strategies.EquityMeanrev, "Alpaca simulatore", Strategies.Observation),
|
||||
(Strategies.EquityMomentum, "Alpaca paper", Strategies.Observation),
|
||||
(StrategyNames.CryptoTrend, "OKX simulatore", StrategyNames.Observation),
|
||||
(StrategyNames.EquityMeanrev, "Alpaca simulatore", StrategyNames.Observation),
|
||||
(StrategyNames.EquityMomentum, "Alpaca paper", StrategyNames.Observation),
|
||||
}, (await StrategiesAsync(rig).ConfigureAwait(false)).ToArray(), "A strategy with a history stays where it is.");
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public sealed class TradingServiceTests
|
||||
Assert.AreEqual(1000m, await BookAsync(Venues.Okx, "EUR").ConfigureAwait(false));
|
||||
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());
|
||||
CollectionAssert.AreEqual(new[] { (StrategyNames.CryptoTrend, 1000m), (StrategyNames.EquityMeanrev, 550m), (StrategyNames.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);
|
||||
@@ -110,8 +110,8 @@ public sealed class TradingServiceTests
|
||||
var msft = await rig.InstrumentAsync(TradingRig.Stock("MSFT")).ConfigureAwait(false);
|
||||
await service.StartAsync(_none).ConfigureAwait(false);
|
||||
var account = service.Managers.Single(m => m.Account.Broker == Venues.Alpaca).Account;
|
||||
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);
|
||||
await service.Ledger.RecordCapitalAsync(account.AccountId, StrategyNames.EquityMomentum, 5000m, "USD", "test", _none).ConfigureAwait(false);
|
||||
await service.Ledger.RecordFillAsync(rig.Fill(account, msft, StrategyNames.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. A split of last
|
||||
// month is still in the table: it came before the purchase and must change nothing.
|
||||
@@ -129,18 +129,18 @@ 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.");
|
||||
Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.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);
|
||||
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
|
||||
CollectionAssert.AreEqual(new[] { 7.5m }, (await DividendsAsync().ConfigureAwait(false)).ToArray());
|
||||
Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!.Quantity);
|
||||
Assert.AreEqual(10m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.Quantity);
|
||||
|
||||
rig.Clock.Advance(TimeSpan.FromDays(1));
|
||||
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
|
||||
await service.CorporateActionsAsync(_none).ConfigureAwait(false);
|
||||
var position = (await service.Ledger.PositionAsync(account.AccountId, msft.Id, Strategies.EquityMomentum, _none).ConfigureAwait(false))!;
|
||||
var position = (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!;
|
||||
Assert.AreEqual((20m, 200m), (position.Quantity, position.AveragePrice));
|
||||
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));
|
||||
@@ -149,6 +149,6 @@ public sealed class TradingServiceTests
|
||||
// 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);
|
||||
Assert.AreEqual(230.05m, (await service.Ledger.PositionAsync(account.AccountId, msft.Id, StrategyNames.EquityMomentum, _none).ConfigureAwait(false))!.LastPrice);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user