using Anubis.Backtest; using Anubis.Core; using Anubis.Data; using Anubis.Db; using Anubis.Trading; namespace Anubis.Tests; /// Every weekday is a session, 9:30–16:00 in New York (13:30–20:00 UTC in September and October). internal sealed class WeekdayCalendar : IMarketCalendar { public MarketSession? SessionOn(DateOnly day) => day.DayOfWeek is DayOfWeek.Saturday or DayOfWeek.Sunday ? null : new MarketSession(day, new DateTimeOffset(day.ToDateTime(new TimeOnly(13, 30)), TimeSpan.Zero), new DateTimeOffset(day.ToDateTime(new TimeOnly(20, 0)), TimeSpan.Zero)); public static IEnumerable Sessions(DateOnly from, DateOnly to) { for (var day = from; day <= to; day = day.AddDays(1)) { if (day.DayOfWeek is not (DayOfWeek.Saturday or DayOfWeek.Sunday)) { yield return day; } } } } /// /// The stock side of the live engine on the internal Alpaca simulator (issue #47, SPEC 5.5): signals after the close, /// the orders at the next open, the exits of equity_meanrev five minutes before the close, with synthetic stocks that /// dip for three sessions and recover. /// internal sealed class StockRig : IAsyncDisposable { public static readonly string[] Stocks = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"]; /// Dollars for a euro: the capital is entrusted in euro (SPEC 7.1). public const decimal Rate = 1.10m; private readonly Dictionary _instruments = new(StringComparer.Ordinal); private readonly WeekdayCalendar _calendar = new(); private StockRig(TradingRig rig) => Rig = rig; public TradingRig Rig { get; } public AccountContext Alpaca { get; private set; } = null!; public PaperBroker Paper { get; private set; } = null!; public OrderManager Manager { get; private set; } = null!; public LiveEngine Engine { get; private set; } = null!; public EquityBook Equity { get; private set; } = null!; public Dictionary> Bars { get; } = new(StringComparer.Ordinal); public static async Task CreateAsync(DateOnly liveFrom, DateOnly last, string strategy = StrategyNames.EquityMeanrev, decimal capital = 10_000m) { var rig = new StockRig(await TradingRig.CreateAsync().ConfigureAwait(false)); await rig.SetUpAsync(liveFrom, last, strategy, capital).ConfigureAwait(false); return rig; } private async Task SetUpAsync(DateOnly liveFrom, DateOnly last, string strategy, decimal capital) { var none = CancellationToken.None; Rig.Clock.Set(new DateTimeOffset(liveFrom.AddDays(-1).ToDateTime(new TimeOnly(12, 0)), TimeSpan.Zero)); Alpaca = await Rig.AccountAsync(Venues.Alpaca, TradingEnvironments.Simulation).ConfigureAwait(false); var sessions = WeekdayCalendar.Sessions(last.AddDays(-700), last).ToList(); var store = new BarStore(Rig.Db, Rig.Clock); var members = new List(); for (var i = 0; i < Stocks.Length; i++) { var symbol = Stocks[i]; _instruments[symbol] = await Rig.InstrumentAsync(TradingRig.Stock(symbol)).ConfigureAwait(false); Bars[symbol] = Path(sessions, 50 + (i * 20), phase: i * 3, seed: (uint)(i + 11)); members.Add(new UniverseMember(_instruments[symbol].Id, symbol, i + 1, 1e9)); } _instruments[StrategyNames.MarketSymbol] = await Rig.InstrumentAsync(TradingRig.Stock(StrategyNames.MarketSymbol) with { IsFund = true }).ConfigureAwait(false); Bars[StrategyNames.MarketSymbol] = Path(sessions, 400, phase: 5, seed: 3, dips: false); foreach (var (symbol, bars) in Bars) { await store.UpsertDailyAsync(_instruments[symbol].Id, bars.Where(b => b.Day < liveFrom), "test", none).ConfigureAwait(false); } await new UniverseStore(Rig.Db, Rig.Clock).SaveAsync(Universes.Equity, liveFrom.AddDays(-30), members, none).ConfigureAwait(false); await Rig.Db.WriteAsync(s => s.ExecuteAsync( "INSERT INTO strategy_state (id, strategy, account_id, stage, capital, state, created_at, updated_at, share, stage_since) VALUES (@id, @s, @a, @st, 0, @j, @now, @now, 1, @now)", none, ("id", Ids.NewId(Rig.Clock)), ("s", strategy), ("a", Alpaca.AccountId), ("st", StrategyStages.Simulation), ("j", new DbJson("{}")), ("now", Rig.Clock.UtcNow)), none) .ConfigureAwait(false); var fx = new FxStore(Rig.Db, Rig.Clock); await fx.UpsertAsync(WeekdayCalendar.Sessions(liveFrom.AddDays(-10), last).Select(d => new FxRate("EUR", "USD", d, Rate, "test")), none).ConfigureAwait(false); await new Capital(Rig.Db, Rig.Clock, Rig.Ledger, fx).SetAsync(Venues.Alpaca, capital, "test", none).ConfigureAwait(false); Paper = Rig.Paper(PaperOptions.Alpaca(), Alpaca.AccountId); await Paper.DepositAsync(capital * Rate, "USD", none).ConfigureAwait(false); Manager = Rig.Manager(Paper, Alpaca); var stages = new StageBook(Rig.Db, Rig.Clock, TradingConsent.None, (_, _, _) => Task.FromResult(false)); Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar); Equity = new EquityBook(Rig.Db, Rig.Clock, Rig.Ledger, fx); } /// /// A slow rise with noise and, every 17 sessions, three sessions down of 2.5% each: RSI(2) falls under 10 and the /// close under its 5-session average, so equity_meanrev buys and sells a few sessions later. /// public static List Path(IReadOnlyList sessions, double start, int phase, uint seed, bool dips = true) { var bars = new List(sessions.Count); var price = start; var state = (seed * 2654435761u) + 1u; double Next() { state = (state * 1664525u) + 1013904223u; return (state >> 8) / (double)(1 << 24); } for (var i = 0; i < sessions.Count; i++) { var cycle = (i + phase) % 17; var change = dips && cycle >= 14 ? -0.025 : (dips ? 0.008 : 0.003) + ((Next() - 0.5) * 0.012); var open = Math.Round(price * (1 + ((Next() - 0.5) * 0.004)), 2); var close = Math.Round(open * (1 + change), 2); var low = Math.Round(Math.Min(open, close) * (1 - (Next() * 0.006)), 2); var high = Math.Round(Math.Max(open, close) * (1 + (Next() * 0.006)), 2); bars.Add(new DailyBar(sessions[i], open, high, low, close, 5_000_000)); price = close; } return bars; } private DailyBar? Bar(string symbol, DateOnly day) => Bars[symbol].SingleOrDefault(b => b.Day == day); public void Quotes(DateOnly day, Func pick) { foreach (var symbol in Bars.Keys) { if (Bar(symbol, day) is { } bar) { var price = (decimal)pick(bar); Rig.Quote(symbol, price - 0.01m, price + 0.01m, 1000m); } } } public async Task MinutesAsync(int count) { for (var i = 0; i < count; i++) { await Engine.PulseAsync(CancellationToken.None).ConfigureAwait(false); await Rig.TurnsAsync(Manager, 1, TimeSpan.FromMinutes(1)).ConfigureAwait(false); } } /// /// One session: the orders decided last night go at 9:35 with the open as price; at midday the quotes touch the /// low (stops); at 15:55 the price is the close and the meanrev exits leave; at 16:16 the bar of the day arrives and /// at 16:20 the signals are computed; the day orders expire and the equity is photographed. /// public async Task SessionAsync(DateOnly day) { if (_calendar.SessionOn(day) is not { } session) { return; } var none = CancellationToken.None; Rig.Clock.Set(session.OpenUtc.AddMinutes(5)); Quotes(day, b => b.Open); await MinutesAsync(15).ConfigureAwait(false); Rig.Clock.Set(session.OpenUtc.AddHours(3)); Quotes(day, b => b.Low); await MinutesAsync(3).ConfigureAwait(false); Rig.Clock.Set(session.CloseUtc.AddMinutes(-5)); Quotes(day, b => b.Close); await MinutesAsync(4).ConfigureAwait(false); Rig.Clock.Set(session.CloseUtc.AddMinutes(1)); await Paper.ExpireDayOrdersAsync(none).ConfigureAwait(false); Rig.Clock.Set(session.CloseUtc.AddMinutes(16)); await Task.WhenAll(Bars.Select(b => new BarStore(Rig.Db, Rig.Clock).UpsertDailyAsync(_instruments[b.Key].Id, b.Value.Where(x => x.Day == day), "test", none))) .ConfigureAwait(false); Rig.Clock.Set(session.CloseUtc.AddMinutes(20)); await MinutesAsync(3).ConfigureAwait(false); foreach (var symbol in Stocks) { await Rig.Ledger.MarkAsync(_instruments[symbol].Id, (decimal)Bar(symbol, day)!.Close, none).ConfigureAwait(false); } await Equity.TakeAsync(none).ConfigureAwait(false); } public BacktestRun Backtest(string strategy, DateOnly from, DateOnly to, decimal capital = 10_000m * Rate) { var instruments = Bars.Select(b => new BacktestInstrument(_instruments[b.Key].Spec, b.Value.Where(x => x.Day <= to && x.Day >= to.AddDays(-700)).ToList())); var calendar = Bars[Stocks[0]].Select(b => b.Day).Where(d => d <= to.AddDays(14)); var data = new BacktestDataset(instruments, "USD", calendar, [StrategyNames.MarketSymbol]); var options = StrategyPipeline.For(strategy, data, new BacktestOptions { InitialCapital = capital, From = from, To = to }); return BacktestEngine.Run(LiveEngine.CreateStrategy(strategy, null), data, options); } /// The live fills by New York day, symbol and side. public async Task> LiveFillsAsync() { var rows = await Rig.QueryAsync("SELECT f.executed_at, i.symbol, f.side, f.quantity FROM fills f JOIN instruments i ON i.id = f.instrument_id ORDER BY f.executed_at", r => (At: r.Moment("executed_at"), Symbol: r.Text("symbol"), Side: Wire.Side(r.Text("side")), Quantity: r.Money("quantity"))).ConfigureAwait(false); return [.. rows.GroupBy(r => (DateOnly.FromDateTime(r.At.UtcDateTime), r.Symbol, r.Side)).Select(g => (g.Key.Item1, g.Key.Symbol, g.Key.Side, g.Sum(x => x.Quantity)))]; } public ValueTask DisposeAsync() { Engine.Dispose(); return Rig.DisposeAsync(); } }