fix: correct PnL math and add health probe
This commit is contained in:
16
app/api/health/route.ts
Normal file
16
app/api/health/route.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
/**
|
||||
* Health check endpoint for Docker HEALTHCHECK
|
||||
* Returns 200 OK if the server is running
|
||||
*/
|
||||
export async function GET() {
|
||||
return NextResponse.json(
|
||||
{
|
||||
status: 'healthy',
|
||||
timestamp: new Date().toISOString(),
|
||||
uptime: process.uptime(),
|
||||
},
|
||||
{ status: 200 }
|
||||
)
|
||||
}
|
||||
@@ -240,9 +240,10 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
|
||||
// Save the closure to database
|
||||
try {
|
||||
const holdTimeSeconds = Math.floor((Date.now() - oppositePosition.entryTime) / 1000)
|
||||
const profitPercent = ((closeResult.closePrice! - oppositePosition.entryPrice) / oppositePosition.entryPrice) * 100
|
||||
const accountPnL = profitPercent * oppositePosition.leverage * (oppositePosition.direction === 'long' ? 1 : -1)
|
||||
const realizedPnL = (oppositePosition.currentSize * accountPnL) / 100
|
||||
const priceProfitPercent = oppositePosition.direction === 'long'
|
||||
? ((closeResult.closePrice! - oppositePosition.entryPrice) / oppositePosition.entryPrice) * 100
|
||||
: ((oppositePosition.entryPrice - closeResult.closePrice!) / oppositePosition.entryPrice) * 100
|
||||
const realizedPnL = closeResult.realizedPnL ?? (oppositePosition.currentSize * priceProfitPercent) / 100
|
||||
|
||||
await updateTradeExit({
|
||||
positionId: oppositePosition.positionId,
|
||||
|
||||
Reference in New Issue
Block a user