Judge the variant in use and count the shadow cases for what they are

The quarterly review judged the best variant of the grid instead of the one
that trades: the plan now fixes the variant in use and the protocol measures
that one, while the tries are still those of the grid. A failure while the
history is prepared is a reason in the report and no longer stops the review,
and a strategy whose protocol does not exist says so when its data are there.

The volatility improvement compared books of different days and stages: the
book keeps its stage and its previous day, and a case without a book valid on
its day is left out. Fewer than six months give no interval, the others use
the clustered error with the Student t.

The restriction balance counts a case in the period in which its outcome
became known, so no case is lost or counted twice between two reviews.

The contact given to the SEC travels only on the client that never follows a
redirect, so it cannot reach another site.

refs #75

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Alby96andClaude Opus 5.5 committed 2026-12-01 22:26:06 +01:00
1 parent 24e20584d4
commit 3543f29aba
23 files changed
+577 -126

No files matched your search

+103 -10
View File
@@ -33,10 +33,11 @@ public sealed class ShadowTests
private static Intent Entry(string symbol, double strength = 0, decimal weight = 0.1m) => new(symbol, weight, "entry") { StopOffset = 1m, Strength = strength };
private static ShadowTurn Turn(string strategy, StrategyContext context, IReadOnlyList<Intent> intents, ShadowInput? input = null, Func<StrategyContext, Intent, double?>? probability = null)
private static ShadowTurn Turn(string strategy, StrategyContext context, IReadOnlyList<Intent> intents, ShadowInput? input = null, Func<StrategyContext, Intent, double?>? probability = null,
string stage = StrategyStages.Observation)
{
var collector = new CaseCollector(strategy, (_, _) => true, null, context.Day);
var turn = new ShadowTurn(strategy, context.Day, collector, input ?? ShadowInput.Nothing, probability ?? ((_, _) => null), () => new EquityMeanrev());
var turn = new ShadowTurn(strategy, stage, context.Day, collector, input ?? ShadowInput.Nothing, probability ?? ((_, _) => null), () => new EquityMeanrev());
Assert.AreSame(intents, collector.Apply(context, intents));
Assert.AreSame(intents, turn.OnProposals.Apply(context, intents), "the very same list: nothing changes");
Assert.AreSame(intents, turn.OnFinal.Apply(context, intents));
@@ -76,7 +77,7 @@ public sealed class ShadowTests
// Another day of the run says nothing.
var other = Context([Stock("AAA", i => 50), Stock(StrategyNames.MarketSymbol, i => 400 - i)], day: _days[^2]);
var collector = new CaseCollector(StrategyNames.EquityMeanrev, (_, _) => true, null, Today);
var turn = new ShadowTurn(StrategyNames.EquityMeanrev, Today, collector, ShadowInput.Nothing, (_, _) => null, () => new EquityMeanrev());
var turn = new ShadowTurn(StrategyNames.EquityMeanrev, StrategyStages.Observation, Today, collector, ShadowInput.Nothing, (_, _) => null, () => new EquityMeanrev());
turn.OnProposals.Apply(other, collector.Apply(other, [Entry("AAA")]));
Assert.IsEmpty(turn.Cases);
}
@@ -106,6 +107,65 @@ public sealed class ShadowTests
Assert.AreEqual(Improvements.EntryTime, candidate.Cases.Single().Improvement);
}
private static readonly IReadOnlyList<BacktestInstrument> _moving = [Stock("AAA", i => i % 2 == 0 ? 100 : 102)];
private static Task DecideAsync(TradingRig rig, string strategy, string stage, DateOnly day) => rig.Db.WriteAsync(s =>
ShadowDesk.WriteAsync(s, rig.Clock, Guid.NewGuid(), Turn(strategy, Context(_moving, day: day), [Entry("AAA", weight: 1m)], stage: stage), strategy, _none), _none);
[TestMethod]
[TestCategory(Category.Integration)]
public async Task AStrategyThatTradesCountsOnlyWhatTheOthersReallyHold()
{
await using var rig = await TradingRig.CreateAsync();
var desk = new LearningDesk(rig.Db, rig.Clock).Shadow;
async Task<string> BooksAsync(string stage) => string.Join(",", (await desk.InputAsync(StrategyNames.CryptoTrend, stage, Today, _none)).OtherBooks.Select(b => b.Stage).Order(StringComparer.Ordinal));
await DecideAsync(rig, StrategyNames.EquityMomentum, StrategyStages.Observation, Today);
await DecideAsync(rig, StrategyNames.EquityMeanrev, StrategyStages.Simulation, Today);
Assert.AreEqual("simulation", await BooksAsync(StrategyStages.Simulation));
Assert.AreEqual("simulation", await BooksAsync(StrategyStages.Real));
Assert.AreEqual("observation,simulation", await BooksAsync(StrategyStages.Observation), "one that only observes is measured on the three");
// The entry of one that trades is not shrunk by what another only looks at.
async Task<double> FactorAsync(string stage) => Turn(StrategyNames.EquityMeanrev, Context(_moving), [Entry("AAA", weight: 0.2m)],
await desk.InputAsync(StrategyNames.EquityMeanrev, stage, Today, _none), stage: stage).Cases.Single(c => c.Improvement == Improvements.VolatilityCap).Factor;
Assert.AreEqual(1.0, await FactorAsync(StrategyStages.Simulation));
Assert.IsLessThan(1.0, await FactorAsync(StrategyStages.Observation));
// From the first decision it makes after its promotion, it counts.
await DecideAsync(rig, StrategyNames.EquityMomentum, StrategyStages.Simulation, Today);
Assert.AreEqual("simulation,simulation", await BooksAsync(StrategyStages.Reduced));
}
[TestMethod]
[TestCategory(Category.Integration)]
public async Task ADecisionRecoveredLateNeverSeesABookOfALaterDay()
{
await using var rig = await TradingRig.CreateAsync();
var desk = new LearningDesk(rig.Db, rig.Clock).Shadow;
Task DecideOnAsync(DateOnly day) => DecideAsync(rig, StrategyNames.EquityMomentum, StrategyStages.Observation, day);
async Task<string> SeenAsync(DateOnly day) => await desk.InputAsync(StrategyNames.EquityMeanrev, StrategyStages.Observation, day, _none) is var i && i.Whole
? string.Join(",", i.OtherBooks.Select(b => b.Day)) : "unknown";
Assert.AreEqual("", await SeenAsync(Today), "no decision yet, nothing held");
await DecideOnAsync(_days[^4]);
await DecideOnAsync(Today);
Assert.AreEqual(BarStore.DayText(Today), await SeenAsync(Today));
Assert.AreEqual(BarStore.DayText(_days[^4]), await SeenAsync(_days[^2]), "the one in force that day");
Assert.AreEqual("unknown", await SeenAsync(_days[^5]));
Assert.AreEqual("", await SeenAsync(Today.AddDays(ShadowBook.MaxAgeDays + 1)), "too old to say what it holds");
// The same day again keeps the one before; a late decision of the same strategy never takes the place of a newer one.
await DecideOnAsync(Today);
Assert.AreEqual(BarStore.DayText(_days[^4]), await SeenAsync(_days[^2]));
await DecideOnAsync(_days[^3]);
Assert.AreEqual(BarStore.DayText(Today), await SeenAsync(Today));
Assert.AreEqual(BarStore.DayText(_days[^3]), await SeenAsync(_days[^2]));
// Without the whole portfolio there is no case, but the book is there for the others.
var turn = Turn(StrategyNames.EquityMomentum, Context(_moving), [Entry("AAA", weight: 1m)], ShadowInput.Nothing with { Whole = false });
Assert.IsNotNull(turn.Book);
Assert.IsFalse(turn.Cases.Any(c => c.Improvement == Improvements.VolatilityCap));
}
[TestMethod]
[TestCategory(Category.Unit)]
public void TheModelWouldChooseOtherCandidates()
@@ -138,18 +198,51 @@ public sealed class ShadowTests
Assert.IsNull(ShadowDesk.Summary(null, []).Mean);
Assert.IsNull(ShadowDesk.Summary(null, [(Monday, 0.01)]).Interval);
// Ten equal signals in each of two months: the spread between months is what counts.
var many = ShadowDesk.Summary(null, [.. Enumerable.Range(0, 20).Select(i => (Monday.AddDays(i < 10 ? 0 : 40), i < 10 ? 0.02 : -0.01))]);
// Ten equal signals in each of six months: the spread between months is what counts, not the sixty signals.
ImprovementLine Months(int months) => ShadowDesk.Summary(null, [.. Enumerable.Range(0, months * 10).Select(i => (Monday.AddMonths(i / 10), (i / 10) % 2 == 0 ? 0.02 : -0.01))]);
var many = Months(6);
Assert.AreEqual((60, 6), (many.Cases, many.Months));
Assert.AreEqual(0.005, many.Mean!.Value, 1e-9);
Assert.IsGreaterThan(0.01, many.Error!.Value);
var view = new ImprovementView(Improvements.All[0], 30, [new ImprovementLine(null, 25, 0.004, 0.001)], "", false, null);
Assert.AreEqual(Math.Sqrt(6 * 0.15 * 0.15 * 6 / 5) / 60, many.Error!.Value, 1e-9);
Assert.IsGreaterThan(3 * 0.015 / Math.Sqrt(59), many.Error.Value, "three times the error of independent signals");
Assert.AreEqual(0.005 + (2.5706 * many.Error.Value), many.Interval!.Value.High, 1e-5, "five degrees of freedom, not the normal");
Assert.IsNull(many.Missing);
var view = new ImprovementView(Improvements.All[0], 30, [new ImprovementLine(null, 25, 12, 0.004, 0.001)], "", false, null);
Assert.AreEqual("un indizio discreto", view.Trust);
Assert.AreEqual("troppo pochi per fidarsi", (view with { Lines = [new ImprovementLine(null, 19, 0, 0.1)] }).Trust);
Assert.AreEqual("conclusione solida", (view with { Lines = [new ImprovementLine(null, 100, 0, 0.1)] }).Trust);
Assert.AreEqual("troppo pochi per fidarsi", (view with { Lines = [new ImprovementLine(null, 19, 12, 0, 0.1)] }).Trust);
Assert.AreEqual("conclusione solida", (view with { Lines = [new ImprovementLine(null, 100, 12, 0, 0.1)] }).Trust);
var said = ShadowDesk.Discoveries([view]).Single();
StringAssert.EndsWith(said.Title, "avrebbe aiutato");
StringAssert.Contains(said.Detail, "+0,40%");
Assert.IsEmpty(ShadowDesk.Discoveries([view with { Lines = [new ImprovementLine(null, 9, 0.1, 0.01)] }]));
Assert.IsEmpty(ShadowDesk.Discoveries([view with { Lines = [new ImprovementLine(null, 9, 12, 0.1, 0.01)] }]));
// With fewer months there is a mean and nothing else, however many the signals.
foreach (var months in new[] { 1, 2, 5 })
{
var few = Months(months);
Assert.AreEqual(months * 10, few.Cases);
Assert.IsNotNull(few.Mean);
Assert.IsNull(few.Error);
Assert.IsNull(few.Interval);
Assert.AreEqual($"troppo pochi mesi per un intervallo: {months}, ne servono 6", few.Missing);
Assert.IsEmpty(ShadowDesk.Discoveries([view with { Lines = [few] }]));
Assert.AreEqual("troppo pochi per fidarsi", (view with { Lines = [few with { Cases = 500 }] }).Trust);
}
Assert.IsNull(ShadowDesk.Summary(null, []).Missing);
}
[TestMethod]
[TestCategory(Category.Unit)]
public void TheQuantilesOfStudentMatchTheTables()
{
foreach (var (freedom, expected) in new[] { (5, 2.5706), (6, 2.4469), (7, 2.3646), (10, 2.2281), (11, 2.2010), (23, 2.0687), (30, 2.0423), (60, 2.0003), (120, 1.9799) })
{
Assert.AreEqual(expected, StudentT.Quantile(0.975, freedom), 0.0005);
Assert.AreEqual(-expected, StudentT.Quantile(0.025, freedom), 0.0005);
}
Assert.AreEqual(Normal.Quantile(0.975), StudentT.Quantile(0.975, 1_000_000), 1e-5);
}
/// <summary>The same decisions with and without the shadow, also when it cannot be written; then its measure.</summary>