Files
trading_bot_v4/app/api/analytics/positions/route.ts
mindesbunister 8e5c592cac Fix database persistence and add analytics
- Fixed Prisma client not being available in Docker container
- Added isTestTrade flag to exclude test trades from analytics
- Created analytics views for net positions (matches Drift UI netting)
- Added API endpoints: /api/analytics/positions and /api/analytics/stats
- Added test trade endpoint: /api/trading/test-db
- Updated Dockerfile to properly copy Prisma client from builder stage
- Database now successfully stores all trades with full details
- Supports position netting calculations to match Drift perpetuals behavior
2025-10-27 09:35:01 +01:00

41 lines
1.2 KiB
TypeScript

/**
* Position Analytics API
*
* Shows net positions (what Drift displays) vs individual trades (what DB stores)
* GET /api/analytics/positions
*/
import { NextResponse } from 'next/server'
import { getPositionSummary, getNetPositions, getTradesWithNetContext } from '@/lib/database/views'
export async function GET() {
try {
const [summary, netPositions, tradesWithContext] = await Promise.all([
getPositionSummary(),
getNetPositions(),
getTradesWithNetContext(),
])
return NextResponse.json({
success: true,
summary,
netPositions,
individualTrades: tradesWithContext,
explanation: {
drift: 'Drift UI shows NET positions - opposite directions in same market cancel out',
database: 'Database stores individual trades to track entry/exit of each position',
example: 'If you have 0.3 SOL LONG and 0.5 SOL SHORT, Drift shows 0.2 SOL SHORT (net)',
},
})
} catch (error) {
console.error('❌ Error getting position analytics:', error)
return NextResponse.json(
{
success: false,
error: error instanceof Error ? error.message : 'Unknown error',
},
{ status: 500 }
)
}
}