Problem: When TP1 order fills on-chain and runner closes quickly, Position Manager detects entire position gone but doesn't know TP1 filled. Result: Marks trade as 'SL' instead of 'TP1', closes 100% instead of partial. Root cause: Position Manager monitoring loop only knows about trade state flags (tp1Hit), not actual Drift order fill history. When both TP1 and runner close before next monitoring cycle, tp1Hit=false but position gone. Fix: Use profit percentage to infer exit reason instead of trade flags - Profit >1.2%: TP2 range - Profit 0.3-1.2%: TP1 range - Profit <0.3%: SL/breakeven range Always calculate P&L on full originalPositionSize for external closures. Exit reason logic determines what actually triggered based on P&L amount. Example from Nov 19 08:40 CET trade: - Entry $140.17, Exit $140.85 = 0.48% profit - Old: Marked as 'SL' (tp1Hit=false, didn't know TP1 filled) - New: Will mark as 'TP1' (profit in TP1 range) Lines changed: lib/trading/position-manager.ts:760-835
64 KiB
64 KiB