diff --git a/app/api/trading/position-manager-debug/route.ts b/app/api/trading/position-manager-debug/route.ts new file mode 100644 index 0000000..3087e17 --- /dev/null +++ b/app/api/trading/position-manager-debug/route.ts @@ -0,0 +1,107 @@ +import { NextRequest, NextResponse } from 'next/server' +import { getInitializedPositionManager } from '@/lib/trading/position-manager' +import { getDriftService } from '@/lib/drift/client' +import { getOpenTrades } from '@/lib/database/trades' + +/** + * Position Manager Debug Endpoint + * + * Returns internal runtime state for verification + * + * Created: Dec 13, 2025 + * Purpose: Enable 100% verification of Position Manager protection + */ +export async function GET(request: NextRequest) { + try { + // Get Position Manager singleton + const pm = await getInitializedPositionManager() + const pmState = (pm as any) + + // Extract internal state + const activeTrades = pmState.activeTrades + const isMonitoring = pmState.isMonitoring || false + const priceMonitor = pmState.priceMonitor + + // Convert Map to array for JSON serialization + const tradesList = activeTrades ? Array.from(activeTrades.entries()).map((entry: any) => { + const [id, trade] = entry as [string, any] + return { + id, + symbol: trade.symbol, + direction: trade.direction, + entryPrice: trade.entryPrice, + currentSize: trade.currentSize, + tp1Hit: trade.tp1Hit, + tp2Hit: trade.tp2Hit, + slMovedToBreakeven: trade.slMovedToBreakeven, + trailingStopActive: trade.trailingStopActive, + createdAt: trade.createdAt, + } + }) : [] + + // Get database state + const dbTrades = await getOpenTrades() + + // Get Drift positions + const driftService = getDriftService() + const driftPositions = await driftService.getAllPositions() + const openDriftPositions = driftPositions.filter(p => Math.abs(p.size) > 0) + + // Check price monitor state + const priceMonitorState = priceMonitor ? { + isRunning: typeof priceMonitor.start === 'function', + hasSymbols: priceMonitor.symbols?.length > 0 || false, + symbolsList: priceMonitor.symbols || [], + } : { + isRunning: false, + hasSymbols: false, + symbolsList: [], + } + + return NextResponse.json({ + success: true, + timestamp: new Date().toISOString(), + positionManager: { + isMonitoring, + activeTradesCount: tradesList.length, + activeTrades: tradesList, + }, + priceMonitor: priceMonitorState, + database: { + openTradesCount: dbTrades.length, + openTrades: dbTrades.map(t => ({ + id: t.id, + symbol: t.symbol, + direction: t.direction, + entryPrice: t.entryPrice, + positionSizeUSD: t.positionSizeUSD, + createdAt: t.createdAt, + })), + }, + drift: { + positionsCount: openDriftPositions.length, + positions: openDriftPositions.map(p => ({ + symbol: p.symbol, + size: p.size, + entryPrice: p.entryPrice, + })), + }, + // Status summary + status: { + pmHasTrades: tradesList.length > 0, + pmMonitoringFlag: isMonitoring, + dbHasTrades: dbTrades.length > 0, + driftHasPositions: openDriftPositions.length > 0, + // THE CRITICAL CHECK: Does PM have the trade that's in DB and Drift? + allInSync: tradesList.length === dbTrades.length && tradesList.length === openDriftPositions.length, + } + }) + + } catch (error) { + console.error('❌ Position Manager debug error:', error) + return NextResponse.json({ + success: false, + error: error instanceof Error ? error.message : String(error), + }, { status: 500 }) + } +} diff --git a/lib/trading/position-manager.ts b/lib/trading/position-manager.ts index 72ae1e6..965e409 100644 --- a/lib/trading/position-manager.ts +++ b/lib/trading/position-manager.ts @@ -654,6 +654,11 @@ export class PositionManager { const tradesForSymbol = Array.from(this.activeTrades.values()) .filter(trade => trade.symbol === update.symbol) + // BUG #77 FIX: Log price updates so we can verify monitoring loop is running + if (tradesForSymbol.length > 0) { + console.log(`🔍 Price check: ${update.symbol} @ $${update.price.toFixed(2)} (${tradesForSymbol.length} trades)`) + } + for (const trade of tradesForSymbol) { try { await this.checkTradeConditions(trade, update.price) @@ -686,15 +691,12 @@ export class PositionManager { // CRITICAL: First check if on-chain position still exists // (may have been closed by TP/SL orders without us knowing) try { + // BUG #77 FIX (Dec 13, 2025): Don't skip price checks if Drift not initialized + // This early return was causing monitoring to never run! + // Position Manager price checking loop must run even if external closure detection is temporarily unavailable + // Let the external closure check fail gracefully later if Drift unavailable const driftService = getDriftService() - // Skip position verification if Drift service isn't initialized yet - // (happens briefly after restart while service initializes) - if (!driftService || !(driftService as any).isInitialized) { - // Service still initializing, skip this check cycle - return - } - const marketConfig = getMarketConfig(trade.symbol) const position = await driftService.getPosition(marketConfig.driftMarketIndex)