Several improvements and bug fixes for 2025 tax return #25

Merged
natercio merged 10 commits from fix-bugs-for-2025-report into main 2026-05-17 09:47:29 +01:00
Showing only changes of commit d371aca767 - Show all commits

View File

@@ -55,8 +55,8 @@ type ReportWriter interface {
type Selector func(Record) bool type Selector func(Record) bool
// BuildReport reads records from a RecordReader and, if the record passes the Selector, it is // BuildReport reads records from a RecordReader and, if the record passes the Selector, it is
// processed into the report // processed into the ReportWriter.
func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, s Selector) error { func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter, sel Selector) error {
buys := make(map[string]*FillerQueue) buys := make(map[string]*FillerQueue)
var buysCount, sellsCount int64 var buysCount, sellsCount int64
@@ -83,14 +83,6 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
return err return err
} }
if !s(rec) {
slog.Debug("Report: skipping record",
slog.String("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()),
)
continue
}
if rec.Side().IsBuy() { if rec.Side().IsBuy() {
buysCount++ buysCount++
} else { } else {
@@ -105,7 +97,7 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
buys[rec.Symbol()] = buyQueue buys[rec.Symbol()] = buyQueue
} }
err = processRecord(ctx, buyQueue, rec, writer) err = processRecord(ctx, buyQueue, rec, sel, writer)
if err != nil { if err != nil {
return fmt.Errorf("processing record: %w", err) return fmt.Errorf("processing record: %w", err)
} }
@@ -114,7 +106,11 @@ func BuildReport(ctx context.Context, reader RecordReader, writer ReportWriter,
} }
} }
func processRecord(ctx context.Context, q *FillerQueue, rec Record, writer ReportWriter) error { // processRecord either adds buys to the queue or consumes buys from the queue when processing a
// sell record.
// Selectors are only applied for sells for performance reasons. It's much cheaper to just accumulate
// buys and only actually inspect a record once a sell happens
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("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()), slog.String("side", rec.Side().String()),
@@ -125,6 +121,14 @@ func processRecord(ctx context.Context, q *FillerQueue, rec Record, writer Repor
q.Push(NewFiller(rec)) q.Push(NewFiller(rec))
case SideSell: case SideSell:
if !sel(rec) {
slog.Debug("Report: skipping record",
slog.String("symbol", rec.Symbol()),
slog.String("side", rec.Side().String()),
)
return nil
}
unmatchedQty := rec.Quantity() unmatchedQty := rec.Quantity()
for unmatchedQty.IsPositive() { for unmatchedQty.IsPositive() {