refactor: use existing TradeContextEnrichmentWorker for TradeContext and Slippage calculations

This commit is contained in:
Richard
2026-07-05 19:03:04 +02:00
parent 0b799123db
commit b5dfa6bb1b
@@ -93,10 +93,21 @@ public class TradeContextEnrichmentWorker : BackgroundService
var postPoint = orderedHistory var postPoint = orderedHistory
.FirstOrDefault(h => h.Timestamp > tradeTimeUnix); .FirstOrDefault(h => h.Timestamp > tradeTimeUnix);
trade.PreTradePrice1m = prePoint != null ? (decimal)prePoint.Price : null; var prePrice = prePoint != null ? (decimal?)prePoint.Price : null;
trade.PostTradePrice1m = postPoint != null ? (decimal)postPoint.Price : null; trade.PreTradePrice1m = prePrice;
trade.PostTradePrice1m = postPoint != null ? (decimal?)postPoint.Price : null;
trade.IsContextEnriched = true; 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); await tradeRepo.UpdateAsync(trade, stoppingToken);
updatedCount++; updatedCount++;
} }
@@ -121,4 +132,20 @@ public class TradeContextEnrichmentWorker : BackgroundService
_logger.LogInformation("🧠 TradeContextEnrichmentWorker stopped"); _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;
}
} }