refactor: use existing TradeContextEnrichmentWorker for TradeContext and Slippage calculations
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user