Files
Anubis/tests/Anubis.Tests/LiveStockSupport.cs
T
Alby96andClaude Opus 5.5 b087dd215b
ci / test (postgres) (push) Successful in 8m20s
ci / test (sqlite) (push) Successful in 4m25s
ci / test (postgres) (pull_request) Successful in 8m13s
ci / test (sqlite) (pull_request) Successful in 4m38s
ci / image (push) Successful in 2m5s
ci / image (pull_request) Successful in 2m6s
Apply the SPEC 10.5 news and calendar rules to new entries (refs #59)
Confirmed serious events hold new purchases of an instrument for 72 hours
and keep its stop where it is; confirmed negative news holds them for 24
hours; an unconfirmed rumour only gets a diary note and a check two hours
later. Coming earnings keep mean reversion out and macro figures hold
entries around their release, now in the live engine too. The strictest
row wins, every row can be switched off in the settings, and windows count
from the fact, so after an absence only effects still in force apply.

A held purchase moves to the end of its restriction while the order is
still valid and keeps its first due time, so the price drift check of
SPEC 5.6 still compares with the decision. Each restriction is written to
the diary once, with its motive. The per-instrument state keeps the tone
over one and three days, the story count against its norm and the active
restriction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 17:39:26 +01:00

227 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Anubis.Backtest;
using Anubis.Core;
using Anubis.Data;
using Anubis.Db;
using Anubis.Trading;
namespace Anubis.Tests;
/// <summary>Every weekday is a session, 9:30–16:00 in New York (13:30–20:00 UTC in September and October).</summary>
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<DateOnly> 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;
}
}
}
}
/// <summary>
/// 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.
/// </summary>
internal sealed class StockRig : IAsyncDisposable
{
public static readonly string[] Stocks = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF"];
/// <summary>Dollars for a euro: the capital is entrusted in euro (SPEC 7.1).</summary>
public const decimal Rate = 1.10m;
private readonly Dictionary<string, Instrument> _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<string, List<DailyBar>> Bars { get; } = new(StringComparer.Ordinal);
public static async Task<StockRig> 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<UniverseMember>();
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);
_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);
}
private StageBook _stages = null!;
/// <summary>The engine again, reading the earnings calendar (SPEC 10.5, fourth row).</summary>
public void UseEarnings(Func<DateOnly, CancellationToken, Task<EarningsView?>> earnings)
{
Engine.Dispose();
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar) { Earnings = earnings };
}
/// <summary>
/// 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.
/// </summary>
public static List<DailyBar> Path(IReadOnlyList<DateOnly> sessions, double start, int phase, uint seed, bool dips = true)
{
var bars = new List<DailyBar>(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<DailyBar, double> 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);
}
}
/// <summary>
/// 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.
/// </summary>
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);
}
/// <summary>The live fills by New York day, symbol and side.</summary>
public async Task<IReadOnlyList<(DateOnly Day, string Symbol, OrderSide Side, decimal Quantity)>> 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();
}
}