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>
286 lines
14 KiB
C#
286 lines
14 KiB
C#
using Anubis.Backtest;
|
||
using Anubis.Data;
|
||
using Anubis.Strategies;
|
||
using Anubis.Trading;
|
||
|
||
namespace Anubis.Tests;
|
||
|
||
/// <summary>A strategy that says what it is told on given days: its result can be computed by hand.</summary>
|
||
internal sealed class ScriptedStrategy(IReadOnlyDictionary<DateOnly, IReadOnlyList<Intent>> script) : IStrategy
|
||
{
|
||
public string Id => "scripted";
|
||
|
||
public string Version => "1";
|
||
|
||
public IReadOnlyList<DecisionTime> Schedule() => [DecisionTime.AfterClose];
|
||
|
||
public IReadOnlyList<string> Universe(UniverseContext context) => context.Available;
|
||
|
||
public IReadOnlyList<Intent> Decide(StrategyContext context) => script.GetValueOrDefault(context.Day) ?? [];
|
||
}
|
||
|
||
internal static class BacktestData
|
||
{
|
||
public static readonly DateOnly D1 = new(2026, 1, 5);
|
||
|
||
public static InstrumentSpec Btc() => new(Venues.Okx, "BTC-EUR", "Bitcoin", AssetClasses.Crypto, "EUR")
|
||
{
|
||
BaseCurrency = "BTC",
|
||
TickSize = 0.1m,
|
||
LotSize = 0.00000001m,
|
||
MinSize = 0.00001m,
|
||
Status = "live",
|
||
};
|
||
|
||
public static InstrumentSpec Stock(string symbol = "AAPL") => new(Venues.Alpaca, symbol, symbol, AssetClasses.Equity, "USD")
|
||
{
|
||
Fractionable = true,
|
||
Status = "active",
|
||
Exchange = "NASDAQ",
|
||
};
|
||
|
||
/// <summary>Five sessions (open, high, low, close) on consecutive days from <see cref="D1"/>.</summary>
|
||
public static IReadOnlyList<DailyBar> FiveDays() =>
|
||
[
|
||
new(D1, 100, 101, 99, 100, 1000),
|
||
new(D1.AddDays(1), 100, 102, 99, 101, 1000),
|
||
new(D1.AddDays(2), 102, 104, 101, 103, 1000),
|
||
new(D1.AddDays(3), 103, 106, 102, 105, 1000),
|
||
new(D1.AddDays(4), 106, 107, 104, 106, 1000),
|
||
];
|
||
|
||
public static BacktestDataset One(InstrumentSpec spec, IReadOnlyList<DailyBar> bars, IReadOnlyList<CorporateAction>? actions = null) =>
|
||
new([new BacktestInstrument(spec, bars, actions)], spec.QuoteCurrency);
|
||
|
||
/// <summary>A random walk with a fixed seed: the same series at every run.</summary>
|
||
public static IReadOnlyList<DailyBar> Walk(int days, ulong seed, double start = 100, double drift = 0.0003, double volatility = 0.02, DateOnly? from = null)
|
||
{
|
||
var gen = new Gen((int)(seed % int.MaxValue));
|
||
var bars = new List<DailyBar>(days);
|
||
var close = start;
|
||
var day = from ?? new DateOnly(2018, 1, 1);
|
||
for (var i = 0; i < days; i++)
|
||
{
|
||
var open = close;
|
||
var change = drift + (volatility * gen.Normal());
|
||
close = Math.Round(open * Math.Exp(change), 2);
|
||
var high = Math.Round(Math.Max(open, close) * (1 + (0.005 * gen.Double())), 2);
|
||
var low = Math.Round(Math.Min(open, close) * (1 - (0.005 * gen.Double())), 2);
|
||
bars.Add(new DailyBar(day.AddDays(i), open, high, low, close, 1000));
|
||
}
|
||
|
||
return bars;
|
||
}
|
||
}
|
||
|
||
/// <summary>The event engine, its costs and its records (issue #34, SPEC 12.1).</summary>
|
||
[TestClass]
|
||
public sealed class BacktestTests
|
||
{
|
||
private static readonly DateOnly D1 = BacktestData.D1;
|
||
|
||
private static ScriptedStrategy Script(params (DateOnly Day, Intent[] Intents)[] steps) =>
|
||
new(steps.ToDictionary(s => s.Day, s => (IReadOnlyList<Intent>)s.Intents));
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void AToyStrategyGivesExactlyTheResultComputedByHand()
|
||
{
|
||
// Half the capital in bitcoin at the first close, everything out at the third. By hand, with OKX's
|
||
// price list (0,20% taker kept in the coin bought, 5 bp of slippage, 0,1 € tick):
|
||
// buy at the open of day 2: 100 × 1,0005 = 100,05 → tick up → 100,1; 50 BTC = 5.005,00 €; fee 0,1 BTC
|
||
// received 49,9 BTC; cash 10.000 − 5.005 = 4.995,00 €
|
||
// sell at the open of day 4: 103 × 0,9995 = 102,9485 → tick down → 102,9; 49,9 × 102,9 = 5.134,71 €;
|
||
// fee 0,2% = 10,26942 €; cash 4.995 + 5.124,44058 = 10.119,44058 €
|
||
var strategy = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")]), (D1.AddDays(2), [new Intent("BTC-EUR", 0m, "exit")]));
|
||
var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestOptions { InitialCapital = 10_000m });
|
||
|
||
Assert.AreEqual(10_119.44058m, run.FinalEquity);
|
||
Assert.AreEqual(119.44058m, run.NetResult);
|
||
Assert.HasCount(2, run.Fills);
|
||
Assert.AreEqual(new BacktestFill(D1.AddDays(1), "BTC-EUR", OrderSide.Buy, 50m, 100.1m, 5_005m, 10.01m, "enter"), run.Fills[0]);
|
||
Assert.AreEqual(new BacktestFill(D1.AddDays(3), "BTC-EUR", OrderSide.Sell, 49.9m, 102.9m, 5_134.71m, 10.26942m, "exit"), run.Fills[1]);
|
||
Assert.AreEqual(20.27942m, run.Fees);
|
||
|
||
var trade = run.Trades.Single();
|
||
Assert.AreEqual(new BacktestTrade("BTC-EUR", D1.AddDays(1), D1.AddDays(3), 5_005m, 119.44058m, 20.27942m), trade);
|
||
|
||
// Valued at every close: cash plus 49,9 BTC at 101 and 103 while held.
|
||
CollectionAssert.AreEqual(
|
||
new[] { 10_000m, 10_034.9m, 10_134.7m, 10_119.44058m, 10_119.44058m },
|
||
run.Equity.Select(e => e.Equity).ToArray());
|
||
Assert.IsEmpty(run.Notes);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheSameToyOnAStockPaysTheRegulatoryFeesRoundedUp()
|
||
{
|
||
// Alpaca: no commission, CAT 0,000003 $ a share on both sides and SEC 0,0000206 on sales, each rounded
|
||
// up to the cent; price step 0,01 $ above 1 $. By hand:
|
||
// buy 50 at 100 × 1,0005 = 100,05: 5.002,50 $ + CAT 0,01 → cash 4.997,49 $
|
||
// sell 50 at 103 × 0,9995 = 102,9485 → 102,94: 5.147,00 $ − SEC 0,11 − CAT 0,01 → cash 10.144,37 $
|
||
var strategy = Script((D1, [new Intent("AAPL", 0.5m, "enter")]), (D1.AddDays(2), [new Intent("AAPL", 0m, "exit")]));
|
||
var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Stock(), BacktestData.FiveDays()));
|
||
|
||
Assert.AreEqual(10_144.37m, run.FinalEquity);
|
||
Assert.AreEqual(0.13m, run.Fees);
|
||
Assert.AreEqual(new BacktestTrade("AAPL", D1.AddDays(1), D1.AddDays(3), 5_002.5m, 144.37m, 0.13m), run.Trades.Single());
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void StopsFillAtTheirLevelOrAtTheOpenWhenThePriceGapsBelow()
|
||
{
|
||
// Touched during the session: the low of day 2 (99) reaches the stop at 99,5, sold at 99,5 less slippage.
|
||
var touched = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter") { Stop = 99.5m }]));
|
||
var run = BacktestEngine.Run(touched, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()));
|
||
var sale = run.Fills.Single(f => f.Side == OrderSide.Sell);
|
||
Assert.AreEqual((D1.AddDays(1), 99.4m, BacktestReasons.Stop), (sale.Day, sale.Price, sale.Reason));
|
||
Assert.AreEqual(4_995m + 4_960.06m - 9.92012m, run.FinalEquity);
|
||
|
||
// Opened below: the stop raised to 102,5 at the close of day 2 meets an open of 102 and sells there (trap 24).
|
||
var gapped = Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")]), (D1.AddDays(1), [new Intent("BTC-EUR", null, "raise_stop") { Stop = 102.5m }]));
|
||
run = BacktestEngine.Run(gapped, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()));
|
||
sale = run.Fills.Single(f => f.Side == OrderSide.Sell);
|
||
Assert.AreEqual((D1.AddDays(2), 101.9m, BacktestReasons.StopGap), (sale.Day, sale.Price, sale.Reason));
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void ASplitChangesSharesAndStopsButNotTheValueAndSignalsSeeOneSeries()
|
||
{
|
||
// A 2-for-1 split with ex-date on day 4: real prices halve, the adjusted series does not jump.
|
||
var bars = BacktestData.FiveDays().Select((b, i) => i >= 3 ? b with { Open = b.Open / 2, High = b.High / 2, Low = b.Low / 2, Close = b.Close / 2 } : b).ToList();
|
||
var split = new CorporateAction("AAPL", CorporateActionKinds.Split, D1.AddDays(3), "test", "s1") { Ratio = 2m };
|
||
var seen = new List<double>();
|
||
var strategy = new Recording(seen, Script((D1, [new Intent("AAPL", 0.5m, "enter") { Stop = 90m }])));
|
||
var run = BacktestEngine.Run(strategy, BacktestData.One(BacktestData.Stock(), bars, [split]));
|
||
|
||
// 50 shares became 100, the stop 90 became 45: nothing was sold and the value went on with the price.
|
||
Assert.HasCount(1, run.Fills);
|
||
Assert.AreEqual(4_997.49m + (100m * 53m), run.FinalEquity);
|
||
|
||
// What the strategy saw on the last day: one continuous series in today's prices, no halving.
|
||
CollectionAssert.AreEqual(new[] { 50d, 50.5, 51.5, 52.5, 53 }, seen.ToArray());
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void PurchasesNeverBorrowAndOrdersBelowTheMinimumAreNotSent()
|
||
{
|
||
// Two instruments at weight 0,8 each: the second purchase is cut to the cash left, never past it.
|
||
var data = new BacktestDataset(
|
||
[new BacktestInstrument(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestInstrument(BacktestData.Btc() with { Symbol = "ETH-EUR", BaseCurrency = "ETH" }, BacktestData.FiveDays())],
|
||
"EUR");
|
||
var run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.8m, "a"), new Intent("ETH-EUR", 0.8m, "b")])), data);
|
||
Assert.IsTrue(run.Equity.All(e => e.Cash >= 0m), "Cash never goes below zero.");
|
||
Assert.IsTrue(run.Notes.Any(n => n.Symbol == "ETH-EUR" && n.Reason == BacktestReasons.NoCash));
|
||
|
||
// A purchase worth less than the minimum size is not sent and the note says why.
|
||
run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.00000001m, "tiny")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()));
|
||
Assert.IsEmpty(run.Fills);
|
||
Assert.AreEqual(OrderReasons.BelowMinimumSize, run.Notes.Single().Reason);
|
||
|
||
// Short selling is refused (SPEC 9.1).
|
||
run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", -0.5m, "short")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()));
|
||
Assert.IsEmpty(run.Fills);
|
||
Assert.AreEqual(BacktestReasons.NoShortSelling, run.Notes.Single().Reason);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void DoubleCostsCostMoreAndTheSameInputsGiveTheSameRun()
|
||
{
|
||
var data = BacktestData.One(BacktestData.Btc(), BacktestData.Walk(400, 7));
|
||
var strategy = new MovingAverageCross(10, 40);
|
||
var once = BacktestEngine.Run(strategy, data);
|
||
var again = BacktestEngine.Run(new MovingAverageCross(10, 40), data);
|
||
CollectionAssert.AreEqual(once.Equity.ToArray(), again.Equity.ToArray());
|
||
CollectionAssert.AreEqual(once.Fills.ToArray(), again.Fills.ToArray());
|
||
Assert.IsGreaterThan(4, once.Trades.Count);
|
||
|
||
var doubled = BacktestEngine.Run(strategy, data, new BacktestOptions { Costs = new CostModel(2m) });
|
||
Assert.IsGreaterThan(once.Fees, doubled.Fees);
|
||
Assert.IsLessThan(once.FinalEquity, doubled.FinalEquity);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void TheCorrectivesAndRiskHooksSeeEveryIntentAndCanChangeIt()
|
||
{
|
||
var seen = new List<string>();
|
||
var block = new Hook("risk", (ctx, intents) =>
|
||
{
|
||
seen.AddRange(intents.Select(i => i.Symbol));
|
||
return [];
|
||
});
|
||
var run = BacktestEngine.Run(Script((D1, [new Intent("BTC-EUR", 0.5m, "enter")])), BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), new BacktestOptions { Risk = block });
|
||
CollectionAssert.AreEqual(new[] { "BTC-EUR" }, seen);
|
||
Assert.IsEmpty(run.Fills);
|
||
Assert.HasCount(1, run.Decisions[0].Proposed);
|
||
Assert.IsEmpty(run.Decisions[0].Final);
|
||
}
|
||
|
||
[TestMethod]
|
||
[TestCategory(Category.Unit)]
|
||
public void OneCycleRestartsFromARecordedBook()
|
||
{
|
||
// The live book holds 49,9 BTC since day 2: the strategy decides on day 3 as if the backtest had run till then.
|
||
var book = new PortfolioState(4_995m, new Dictionary<string, Holding> { ["BTC-EUR"] = new(49.9m, 100.3m, null, D1.AddDays(1)) });
|
||
var exitWhenHeld = new ExitIfHeld();
|
||
var record = BacktestEngine.DecideOnce(exitWhenHeld, BacktestData.One(BacktestData.Btc(), BacktestData.FiveDays()), book, D1.AddDays(2));
|
||
Assert.AreEqual(D1.AddDays(2), record.Day);
|
||
Assert.AreEqual(new Intent("BTC-EUR", 0m, "held"), record.Proposed.Single());
|
||
Assert.AreEqual(4_995m + (49.9m * 103m), exitWhenHeld.Equity, "The book is valued at that day's close.");
|
||
}
|
||
|
||
private sealed class Recording(List<double> seen, IStrategy inner) : IStrategy
|
||
{
|
||
public string Id => inner.Id;
|
||
|
||
public string Version => inner.Version;
|
||
|
||
public IReadOnlyList<DecisionTime> Schedule() => inner.Schedule();
|
||
|
||
public IReadOnlyList<string> Universe(UniverseContext context) => inner.Universe(context);
|
||
|
||
public IReadOnlyList<Intent> Decide(StrategyContext context)
|
||
{
|
||
var bars = context.Bars("AAPL");
|
||
if (bars.Count == 5)
|
||
{
|
||
seen.AddRange(bars.Closes(5));
|
||
}
|
||
|
||
return inner.Decide(context);
|
||
}
|
||
}
|
||
|
||
private sealed class Hook(string name, Func<StrategyContext, IReadOnlyList<Intent>, IReadOnlyList<Intent>> apply) : IIntentStage
|
||
{
|
||
public string Name => name;
|
||
|
||
public IReadOnlyList<Intent> Apply(StrategyContext context, IReadOnlyList<Intent> intents) => apply(context, intents);
|
||
}
|
||
|
||
private sealed class ExitIfHeld : IStrategy
|
||
{
|
||
public decimal Equity { get; private set; }
|
||
|
||
public string Id => "exit_if_held";
|
||
|
||
public string Version => "1";
|
||
|
||
public IReadOnlyList<DecisionTime> Schedule() => [DecisionTime.AfterClose];
|
||
|
||
public IReadOnlyList<string> Universe(UniverseContext context) => context.Available;
|
||
|
||
public IReadOnlyList<Intent> Decide(StrategyContext context)
|
||
{
|
||
Equity = context.Portfolio.Equity;
|
||
return [.. context.Universe.Where(context.Portfolio.Holds).Select(s => new Intent(s, 0m, "held"))];
|
||
}
|
||
}
|
||
}
|