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
@@ -25,11 +25,11 @@ public sealed class ReconcilerTests
|
||||
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, Strategies.CryptoTrend, 10_000m, "EUR", "test", _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, Strategies.CryptoTrend, OrderSide.Buy, 0.01m, OrderPurpose.Entry, Ids.NewId(rig.Clock))
|
||||
await manager.SubmitAsync(new TradeIntent(instrument.Id, StrategyNames.CryptoTrend, OrderSide.Buy, 0.01m, OrderPurpose.Entry, Ids.NewId(rig.Clock))
|
||||
{
|
||||
StopLevel = 57000m,
|
||||
StartAggressive = true,
|
||||
@@ -52,7 +52,7 @@ public sealed class ReconcilerTests
|
||||
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, Strategies.CryptoTrend, OrderSide.Buy, 0.001m, OrderPurpose.Entry, Ids.NewId(s.Rig.Clock));
|
||||
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)]
|
||||
@@ -77,11 +77,11 @@ public sealed class ReconcilerTests
|
||||
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, Strategies.CryptoTrend, _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, Strategies.CryptoTrend, _none).ConfigureAwait(false));
|
||||
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));
|
||||
@@ -132,7 +132,7 @@ public sealed class ReconcilerTests
|
||||
|
||||
// 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, Strategies.CryptoTrend, _none).ConfigureAwait(false))!.Quantity);
|
||||
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));
|
||||
@@ -148,7 +148,7 @@ public sealed class ReconcilerTests
|
||||
public async Task AnUnknownOrderOfThisInstallationIsReportedAndCanBeLeftOut()
|
||||
{
|
||||
await using var s = await HoldingAsync().ConfigureAwait(false);
|
||||
var stray = ClientOrderIds.Build(TradingRig.Installation, TradingEnvironments.Demo, Strategies.CryptoTrend, OrderPurpose.Entry, ClientOrderIds.Key(Guid.NewGuid()), 1);
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user