Settle what the checks of the review fixes found
A difference that comes and goes no longer makes the account ask about a stop the user has already chosen on. The balance of the restrictions adds each period to what was published, so a change of capital does not rewrite the past. An image is published only under a tag that exists, and a file that is not part of it no longer marks it as unclean. refs #70 #74 #75 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d26d945818
commit
8d7efe425d
8 files changed
+199
-23
No files matched your search
@@ -275,14 +275,15 @@ public sealed partial class QuarterlyReview(Database db, IClock clock, Laborator
|
||||
var live = (await learning.Store.AllAsync(cancellationToken).ConfigureAwait(false)).Where(e => e.Source == CaseSources.Live).ToList();
|
||||
var examples = await ExamplesAsync(through, crypto, all, cancellationToken).ConfigureAwait(false);
|
||||
var stockBars = await HasStockBarsAsync(cancellationToken).ConfigureAwait(false);
|
||||
var restrictions = await RestrictionBalance.LoadAsync(db, clock, from, now, cancellationToken).ConfigureAwait(false);
|
||||
var report = new ReviewReport
|
||||
{
|
||||
Missing = missing,
|
||||
RatesBefore = before,
|
||||
RatesAfter = examples is null ? null : LearningDesk.Merge(before, Trainer.SharesOf(examples)),
|
||||
RestrictionsFrom = Zones.DayIn(from > start ? from : start, Zones.Rome),
|
||||
Restrictions = await RestrictionBalance.LoadAsync(db, clock, from, now, cancellationToken).ConfigureAwait(false),
|
||||
RestrictionsEver = await RestrictionBalance.LoadAsync(db, clock, DateTimeOffset.UnixEpoch, now, cancellationToken).ConfigureAwait(false),
|
||||
Restrictions = restrictions,
|
||||
RestrictionsEver = RestrictionBalance.Add(previous?.Report?.RestrictionsEver, restrictions),
|
||||
};
|
||||
var lines = new List<ReviewLine>();
|
||||
foreach (var stage in all)
|
||||
|
||||
@@ -85,20 +85,19 @@ public static class RestrictionBalance
|
||||
|
||||
foreach (var (example, blocks) in cases.Values)
|
||||
{
|
||||
var code = Codes.First(c => blocks.Any(b => b.Code == c));
|
||||
var line = lines[code];
|
||||
// A later block, or a purchase, can still change it: it is settled with the outcome, and what comes after no
|
||||
// longer counts.
|
||||
var first = blocks[0].At;
|
||||
var last = example.Label?.ClosedDay is { } closed ? new DateTimeOffset(closed.AddDays(1).ToDateTime(TimeOnly.MinValue), TimeSpan.Zero) : DateTimeOffset.MaxValue;
|
||||
DateTimeOffset? settled = example.Label is null ? null : example.LabelledAt > first ? example.LabelledAt : last > first ? last : first;
|
||||
var counted = blocks.Where(b => settled is null || b.At <= settled).ToList();
|
||||
var code = Codes.First(c => counted.Any(b => b.Code == c));
|
||||
var line = lines[code];
|
||||
|
||||
// In observation nothing is ever bought: an entry only put off would have been made at its end. A later block of
|
||||
// the same example can still stop it, so it is settled with the outcome.
|
||||
var observed = example.Stage == StrategyStages.Observation;
|
||||
var delayed = observed
|
||||
? blocks.All(b => b.Postponed)
|
||||
: trades.Any(t => t.Strategy == example.Strategy && t.Symbol == example.Symbol && t.OpenedAt >= first && t.OpenedAt < last);
|
||||
DateTimeOffset? settled = delayed && !observed
|
||||
? trades.Where(t => t.Strategy == example.Strategy && t.Symbol == example.Symbol && t.OpenedAt >= first).Min(t => t.OpenedAt)
|
||||
: example.Label is null ? null : example.LabelledAt > first ? example.LabelledAt : last > first ? last : first;
|
||||
// In observation nothing is ever bought: an entry only put off would have been made at its end.
|
||||
var delayed = example.Stage == StrategyStages.Observation
|
||||
? counted.All(b => b.Postponed)
|
||||
: trades.Any(t => t.Strategy == example.Strategy && t.Symbol == example.Symbol && t.OpenedAt >= first && t.OpenedAt < last && (settled is null || t.OpenedAt <= settled));
|
||||
if (settled is not { } known || known > until)
|
||||
{
|
||||
lines[code] = line with { Open = line.Open + 1 };
|
||||
@@ -125,6 +124,23 @@ public static class RestrictionBalance
|
||||
return new RestrictionReport([.. Codes.Select(c => lines[c]).Where(l => l.Stopped + l.Open + l.NeverStarted + l.Delayed + l.Increases > 0)]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The whole up to now: what the last review published plus the period after it, with the cases open now. The past is
|
||||
/// not counted again, so it stays at the capital and the prices it was published with.
|
||||
/// </summary>
|
||||
public static RestrictionReport Add(RestrictionReport? before, RestrictionReport period)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(period);
|
||||
var lines = Codes.Select(code =>
|
||||
{
|
||||
var then = before?.Lines.FirstOrDefault(l => l.Code == code) ?? new RestrictionLine(code, 0, 0m, 0m, 0, 0, 0, 0);
|
||||
var now = period.Lines.FirstOrDefault(l => l.Code == code) ?? new RestrictionLine(code, 0, 0m, 0m, 0, 0, 0, 0);
|
||||
return new RestrictionLine(code, then.Stopped + now.Stopped, then.Avoided + now.Avoided, then.GivenUp + now.GivenUp, now.Open,
|
||||
then.NeverStarted + now.NeverStarted, then.Delayed + now.Delayed, then.Increases + now.Increases);
|
||||
});
|
||||
return new RestrictionReport([.. lines.Where(l => l.Stopped + l.Open + l.NeverStarted + l.Delayed + l.Increases > 0)]);
|
||||
}
|
||||
|
||||
/// <summary>The balance of the entries blocked whose outcome became known in a period.</summary>
|
||||
public static async Task<RestrictionReport> LoadAsync(Database db, IClock clock, DateTimeOffset from, DateTimeOffset until, CancellationToken cancellationToken)
|
||||
{
|
||||
|
||||
@@ -126,6 +126,9 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger)
|
||||
/// <summary>The user has chosen and only a stop is missing: nothing is left to ask, the account returns to normal when it is back.</summary>
|
||||
public const string StopReason = "waiting_for_stop";
|
||||
|
||||
// A difference the user has not chosen about came while the stop was awaited: its kind follows.
|
||||
private const string StopAndOther = StopReason + "+";
|
||||
|
||||
/// <summary>How long a difference may take to explain itself before the user is asked.</summary>
|
||||
public TimeSpan Grace { get; init; } = TimeSpan.FromMinutes(15);
|
||||
|
||||
@@ -599,7 +602,13 @@ public sealed class Reconciler(Database db, IClock clock, Ledger ledger)
|
||||
{
|
||||
// Something the user has not chosen about: it has its own 15 minutes to explain itself (SPEC 7.4).
|
||||
since = now;
|
||||
reason = other.Kind;
|
||||
reason = StopAndOther + other.Kind;
|
||||
}
|
||||
else if (reason is not null && reason.StartsWith(StopAndOther, StringComparison.Ordinal) && since is { } came && now - came < Grace && differences.All(d => d.Kind == DifferenceKinds.Stop))
|
||||
{
|
||||
// It explained itself in time: only the stop is missing again, and nothing is left to ask.
|
||||
since = now;
|
||||
reason = StopReason;
|
||||
}
|
||||
}
|
||||
else if (mode == AccountModes.Prudent && (reason == StopReason || (reason != KeyReason && since is { } start && now - start < Grace)))
|
||||
|
||||
Reference in new issue
Block a user