From b5dfa6bb1bc0cc80bd32618c7075dfadc9cea254 Mon Sep 17 00:00:00 2001 From: Richard Date: Sun, 5 Jul 2026 19:03:04 +0200 Subject: [PATCH] refactor: use existing TradeContextEnrichmentWorker for TradeContext and Slippage calculations --- .../Services/TradeContextEnrichmentWorker.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Predictalytics.Worker/Services/TradeContextEnrichmentWorker.cs b/src/Predictalytics.Worker/Services/TradeContextEnrichmentWorker.cs index 437181a..6a11a5f 100644 --- a/src/Predictalytics.Worker/Services/TradeContextEnrichmentWorker.cs +++ b/src/Predictalytics.Worker/Services/TradeContextEnrichmentWorker.cs @@ -93,10 +93,21 @@ public class TradeContextEnrichmentWorker : BackgroundService var postPoint = orderedHistory .FirstOrDefault(h => h.Timestamp > tradeTimeUnix); - trade.PreTradePrice1m = prePoint != null ? (decimal)prePoint.Price : null; - trade.PostTradePrice1m = postPoint != null ? (decimal)postPoint.Price : null; + var prePrice = prePoint != null ? (decimal?)prePoint.Price : null; + trade.PreTradePrice1m = prePrice; + trade.PostTradePrice1m = postPoint != null ? (decimal?)postPoint.Price : null; trade.IsContextEnriched = true; + // Populate new high-res TradeContext + trade.Context = new TradeContext + { + TradeId = trade.Id, + PriceBefore1m = prePrice, + PriceAfter1m = trade.PostTradePrice1m, + EstimatedSlippage = prePrice.HasValue ? Math.Abs(trade.Price - prePrice.Value) : null, + EstimatedOrderType = DetermineOrderType(trade, prePrice) + }; + await tradeRepo.UpdateAsync(trade, stoppingToken); updatedCount++; } @@ -121,4 +132,20 @@ public class TradeContextEnrichmentWorker : BackgroundService _logger.LogInformation("🧠 TradeContextEnrichmentWorker stopped"); } + + private static Predictalytics.Domain.Enums.OrderType DetermineOrderType(Trade trade, decimal? priceBefore) + { + if (priceBefore == null) return Predictalytics.Domain.Enums.OrderType.Unknown; + + if (trade.Side == Predictalytics.Domain.Enums.TradeSide.Buy) + { + return trade.Price <= priceBefore.Value ? Predictalytics.Domain.Enums.OrderType.Maker : Predictalytics.Domain.Enums.OrderType.Taker; + } + else if (trade.Side == Predictalytics.Domain.Enums.TradeSide.Sell) + { + return trade.Price >= priceBefore.Value ? Predictalytics.Domain.Enums.OrderType.Maker : Predictalytics.Domain.Enums.OrderType.Taker; + } + + return Predictalytics.Domain.Enums.OrderType.Unknown; + } }