From 52949200dac870e8155923221b68ff08fce6213e Mon Sep 17 00:00:00 2001 From: Natercio Moniz Date: Fri, 10 Jul 2026 17:16:55 +0100 Subject: [PATCH] skip buy records not covered by selector --- internal/report.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/report.go b/internal/report.go index fb72ea1..b8e5a59 100644 --- a/internal/report.go +++ b/internal/report.go @@ -68,7 +68,8 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, case <-ctx.Done(): return ctx.Err() case <-progTicker.C: - slog.InfoContext(ctx, "Progress update", + slog.InfoContext( + ctx, "Progress update", slog.Int64("total_records", buysCount+sellsCount), slog.Int64("sell_records", sellsCount), slog.Int64("buy_records", buysCount), @@ -108,10 +109,13 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, // processRecord either adds buys to the queue or consumes buys from the queue when processing a // sell record. -// Selectors are only applied on sells for performance reasons. It's much cheaper to just accumulate -// buys and only actually inspect a record once a sell happens due to potential network requests to +// +// NOTE: Selectors are only applied when processing sell records for performance reasons. It's much +// cheaper to just accumulate buys and only actually inspect any records once a sell happens. This +// avoids potential network requests to for every single record. func processRecord(ctx context.Context, q *FillerQueue, rec Record, sel Selector, writer ReportWriter) error { - slog.Debug("Report: processing record", + slog.Debug( + "Report: processing record", slog.String("symbol", rec.Symbol()), slog.String("side", rec.Kind().String()), ) @@ -122,7 +126,8 @@ func processRecord(ctx context.Context, q *FillerQueue, rec Record, sel Selector case KindSell: if !sel(rec) { - slog.Debug("Report: skipping record", + slog.Debug( + "Report: skipping record", slog.String("symbol", rec.Symbol()), slog.String("side", rec.Kind().String()), ) @@ -137,6 +142,11 @@ func processRecord(ctx context.Context, q *FillerQueue, rec Record, sel Selector return ErrInsufficientBoughtVolume } + // Since we don't apply selectors while processing buys we need to apply them here. + if !sel(buy) { + continue + } + matchedQty, filled := buy.Fill(unmatchedQty) if filled {