Fix 10 critical bugs in Engine, Retention, Reconciliation and API
This commit is contained in:
@@ -177,6 +177,18 @@ public class TradeRetentionWorker : BackgroundService
|
||||
var totalAmount = list.Sum(t => t.Amount);
|
||||
if (totalSize <= 0) continue;
|
||||
|
||||
if (positions.TryGetValue(outcomeId, out var pos))
|
||||
{
|
||||
// Bug 2: Check for unapplied trades before compacting
|
||||
var hasUnappliedTrades = await db.Trades.AnyAsync(
|
||||
t => t.TraderId == traderId && t.MarketOutcomeId == outcomeId && t.Id > pos.LastAppliedTradeId, ct);
|
||||
|
||||
if (hasUnappliedTrades)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
var weightedAvgPrice = totalAmount / totalSize;
|
||||
|
||||
// Grab a representative trade to copy fields
|
||||
@@ -210,12 +222,12 @@ public class TradeRetentionWorker : BackgroundService
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
// Bump the position checkpoint so it doesn't get double counted
|
||||
if (positions.TryGetValue(outcomeId, out var pos))
|
||||
if (positions.TryGetValue(outcomeId, out var updatePos))
|
||||
{
|
||||
pos.LastAppliedTradeId = Math.Max(pos.LastAppliedTradeId, compactedTrade.Id);
|
||||
updatePos.LastAppliedTradeId = Math.Max(updatePos.LastAppliedTradeId, compactedTrade.Id);
|
||||
// Mark as pruned so we don't accidentally reset and replay (which would lose the exact intraday timestamps)
|
||||
pos.IsHistoryPruned = true;
|
||||
db.TraderPositions.Update(pos);
|
||||
updatePos.IsHistoryPruned = true;
|
||||
db.TraderPositions.Update(updatePos);
|
||||
}
|
||||
|
||||
compactedTradeCount += list.Count - 1;
|
||||
|
||||
Reference in New Issue
Block a user