ci / test (postgres) (push) Successful in 5m56s
ci / test (sqlite) (push) Successful in 5m5s
ci / test (postgres) (pull_request) Successful in 5m36s
ci / test (sqlite) (pull_request) Successful in 4m52s
ci / image (push) Successful in 2m6s
ci / image (pull_request) Successful in 52s
News, the economic calendar and earnings dates no longer change any decision by default (ADR 0023). A new switch, decisions.external_rules, chooses between "misurate" (default) and "attive" (the Phase 7 behaviour); ADR 0027 records every choice. - Engine: with "misurate" nothing is held, postponed or kept, no diary or note is written and equity_meanrev gets no earnings dates. The gate is still asked, per rule and with all rows on, to write one shadow case per rule, strategy, instrument and day. The switch is read once per decision; the one-step check and the release of a planned order read it as it was when the decision ran. - Shadow: five rules measured with the existing improvement machinery, 99% interval, "too early" under six months, the ADR 0023 criterion; the earnings exit is measured by relabelling with the report date. Shown in the Laboratorio and in the quarterly review beside the historical restriction balance. - Model: the seven outside clues stay out of training; a champion that uses one is set aside at once, without an alarm, and Sunday trains one without them. - Settings: the switch changes only from the settings form, with the protections of the five rows; the API refuses it; each change writes one diary line. The first start of this version records "attive" until then on an installation that had already decided. - Screens, demo and manual follow "misurate". Two independent review rounds; their confirmed findings are fixed and tested. SQLite, PostgreSQL and the image with the guard drill are green. refs #116, refs #117 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
259 lines
13 KiB
C#
259 lines
13 KiB
C#
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>The engine again, with the learning beside it.</summary>
|
||
public void UseLearning(Learning.LearningDesk learning)
|
||
{
|
||
Engine.Dispose();
|
||
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar) { Learning = learning };
|
||
}
|
||
|
||
/// <summary>
|
||
/// The engine again with news, calendar and earnings only measured (ADR 0023): the earnings calendar, the learning and the
|
||
/// gate it asks for the shadow.
|
||
/// </summary>
|
||
/// <paramref name="measuredAt"/> says whether they were measured at an instant (the change log); without it, always.
|
||
public void UseMeasured(Func<DateOnly, CancellationToken, Task<EarningsView?>> earnings, Learning.LearningDesk learning, News.IEntryGate? gate = null,
|
||
Func<DateTimeOffset, bool>? measuredAt = null)
|
||
{
|
||
Engine.Dispose();
|
||
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar)
|
||
{
|
||
Earnings = earnings,
|
||
Learning = learning,
|
||
Gate = gate,
|
||
RulesMeasured = (at, _) => Task.FromResult(measuredAt?.Invoke(at) ?? true),
|
||
};
|
||
}
|
||
|
||
/// <summary>The engine again, with the rules of SPEC 10.5 on single purchases.</summary>
|
||
public void UseGate(News.IEntryGate gate)
|
||
{
|
||
Engine.Dispose();
|
||
Engine = new LiveEngine(Rig.Db, Rig.Clock, Rig.Ledger, _stages, id => id == Alpaca.AccountId ? Manager : null, Rig.QuoteAsync, _calendar) { Gate = gate };
|
||
}
|
||
|
||
/// <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(Alpaca.AccountId, _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();
|
||
}
|
||
}
|