Files
trading_bot_v4/app/api/trading/close/route.ts
mindesbunister 056440bf8f feat: add quality score display and timezone fixes
- Add qualityScore to ExecuteTradeResponse interface and response object
- Update analytics page to always show Signal Quality card (N/A if unavailable)
- Fix n8n workflow to pass context metrics and qualityScore to execute endpoint
- Fix timezone in Telegram notifications (Europe/Berlin)
- Fix symbol normalization in /api/trading/close endpoint
- Update Drift ETH-PERP minimum order size (0.002 ETH not 0.01)
- Add transaction confirmation to closePosition() to prevent phantom closes
- Add 30-second grace period for new trades in Position Manager
- Fix execution order: database save before Position Manager.addTrade()
- Update copilot instructions with transaction confirmation pattern
2025-11-01 17:00:37 +01:00

97 lines
2.6 KiB
TypeScript

/**
* Close Position API Endpoint
*
* Closes an existing position (partially or fully)
*/
import { NextRequest, NextResponse } from 'next/server'
import { closePosition } from '@/lib/drift/orders'
import { initializeDriftService } from '@/lib/drift/client'
import { normalizeTradingViewSymbol } from '@/config/trading'
export const dynamic = 'force-dynamic'
export const runtime = 'nodejs'
interface CloseRequest {
symbol: string // e.g., 'SOL-PERP' or 'SOLUSDT'
percentToClose?: number // 0-100, default 100 (close entire position)
}
export async function POST(request: NextRequest) {
try {
// Verify authorization
const authHeader = request.headers.get('authorization')
const expectedAuth = `Bearer ${process.env.API_SECRET_KEY}`
if (!authHeader || authHeader !== expectedAuth) {
return NextResponse.json(
{ success: false, error: 'Unauthorized' },
{ status: 401 }
)
}
const body: CloseRequest = await request.json()
const { symbol, percentToClose = 100 } = body
if (!symbol) {
return NextResponse.json(
{ success: false, error: 'Missing symbol' },
{ status: 400 }
)
}
if (percentToClose < 0 || percentToClose > 100) {
return NextResponse.json(
{ success: false, error: 'percentToClose must be between 0 and 100' },
{ status: 400 }
)
}
// Normalize symbol (SOLUSDT -> SOL-PERP)
const driftSymbol = normalizeTradingViewSymbol(symbol)
console.log(`📊 Closing position: ${driftSymbol} (${percentToClose}%)`)
// Initialize Drift service if not already initialized
await initializeDriftService()
// Close position
const result = await closePosition({
symbol: driftSymbol,
percentToClose,
slippageTolerance: 1.0,
})
if (!result.success) {
return NextResponse.json(
{
success: false,
error: 'Position close failed',
message: result.error,
},
{ status: 500 }
)
}
return NextResponse.json({
success: true,
transactionSignature: result.transactionSignature,
symbol: driftSymbol,
closePrice: result.closePrice,
closedSize: result.closedSize,
realizedPnL: result.realizedPnL,
percentClosed: percentToClose,
})
} catch (error) {
console.error('❌ Close position error:', error)
return NextResponse.json(
{
success: false,
error: 'Internal server error',
message: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}