critical: Fix P&L calculation and TP1 false detection bugs
- Add originalPositionSize tracking to prevent stale size usage - Add price validation to TP1 detection (prevents manual closes misidentified as TP1) - Fix external closure P&L to use originalPositionSize not currentSize - Add handleManualClosure method for proper exit reason detection - Add isPriceAtTarget helper for TP/SL price validation (0.2% tolerance) - Update all ActiveTrade creation points (execute, test, sync-positions, test-db) Bug fixes: - Manual close at 42.34 was detected as TP1 (target 40.71) - FIXED - P&L showed -$1.71 instead of actual -$2.92 - FIXED - Exit reason showed SL instead of manual - FIXED Root cause: Position Manager detected size reduction without validating price was actually at TP1 level. Used stale currentSize for P&L calculation. Files modified: - lib/trading/position-manager.ts (core fixes) - app/api/trading/execute/route.ts - app/api/trading/test/route.ts - app/api/trading/sync-positions/route.ts - app/api/trading/test-db/route.ts
This commit is contained in:
@@ -170,6 +170,9 @@ export async function POST(request: NextRequest): Promise<NextResponse<TestTrade
|
||||
tp2Price,
|
||||
emergencyStopPrice,
|
||||
currentSize: positionSizeUSD,
|
||||
originalPositionSize: positionSizeUSD, // Store original size for P&L
|
||||
takeProfitPrice1: tp1Price,
|
||||
takeProfitPrice2: tp2Price,
|
||||
tp1Hit: false,
|
||||
tp2Hit: false,
|
||||
slMovedToBreakeven: false,
|
||||
|
||||
Reference in New Issue
Block a user