Fix P&L calculation and signal flip detection

- Fix external closure P&L using tp1Hit flag instead of currentSize
- Add direction change detection to prevent false TP1 on signal flips
- Signal flips now recorded with accurate P&L as 'manual' exits
- Add retry logic with exponential backoff for Solana RPC rate limits
- Create /api/trading/cancel-orders endpoint for manual cleanup
- Improves data integrity for win/loss statistics
This commit is contained in:
mindesbunister
2025-11-09 17:59:50 +01:00
parent 4d533ccb53
commit 22195ed34c
15 changed files with 2166 additions and 17 deletions

View File

@@ -0,0 +1,51 @@
import { NextRequest, NextResponse } from 'next/server'
import { cancelAllOrders } from '@/lib/drift/orders'
import { initializeDriftService } from '@/lib/drift/client'
/**
* Cancel all orders for a symbol
* POST /api/trading/cancel-orders
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { symbol } = body
if (!symbol) {
return NextResponse.json(
{ success: false, error: 'Symbol required' },
{ status: 400 }
)
}
console.log(`🗑️ Manual order cancellation requested for ${symbol}`)
// Initialize Drift service
await initializeDriftService()
// Cancel all orders
const result = await cancelAllOrders(symbol)
if (result.success) {
return NextResponse.json({
success: true,
message: `Cancelled ${result.cancelledCount || 0} orders for ${symbol}`,
cancelledCount: result.cancelledCount,
})
} else {
return NextResponse.json(
{ success: false, error: result.error },
{ status: 500 }
)
}
} catch (error) {
console.error('❌ Error cancelling orders:', error)
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Failed to cancel orders',
},
{ status: 500 }
)
}
}