Fix selector behaviour (#26)
Badges / coveralls (push) Successful in 1m4s

Co-authored-by: Natercio Moniz <[email protected]>
Co-committed-by: Natercio Moniz <[email protected]>
This commit was merged in pull request #26.
This commit is contained in:
2026-07-11 15:24:54 +01:00
committed by natercio
parent 8b2c4aa6fe
commit 1ce8561782
6 changed files with 66 additions and 65 deletions
+15 -5
View File
@@ -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 {
+3
View File
@@ -16,12 +16,15 @@ func And(a, b Selector) Selector {
}
}
// OnlyNature will only select records with the given Nature n (G01, G20, etc...).
func OnlyNature(n Nature) Selector {
return func(r Record) bool {
return r.Nature() == n
}
}
// OnlyAssetCountry will only select records with the given ISO code c (620 for Portugal, 196 for
// Cyprus, etc...).
func OnlyAssetCountry(c int64) Selector {
return func(r Record) bool {
return r.AssetCountry() == c