Use percentage aware sizing in execute endpoint

This commit is contained in:
mindesbunister
2025-11-10 20:27:52 +01:00
parent 43b688d9f2
commit ee89d15b8b

View File

@@ -107,9 +107,18 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
// Get trading configuration // Get trading configuration
const config = getMergedConfig() const config = getMergedConfig()
// Get symbol-specific position sizing // Initialize Drift service and check account health before sizing
const { getPositionSizeForSymbol } = await import('@/config/trading') const driftService = await initializeDriftService()
const { size: positionSize, leverage, enabled } = getPositionSizeForSymbol(driftSymbol, config) const health = await driftService.getAccountHealth()
console.log(`🩺 Account health: Free collateral $${health.freeCollateral.toFixed(2)}`)
// Get symbol-specific position sizing (supports percentage-based sizing)
const { getActualPositionSizeForSymbol } = await import('@/config/trading')
const { size: positionSize, leverage, enabled, usePercentage } = await getActualPositionSizeForSymbol(
driftSymbol,
config,
health.freeCollateral
)
// Check if trading is enabled for this symbol // Check if trading is enabled for this symbol
if (!enabled) { if (!enabled) {
@@ -126,16 +135,9 @@ export async function POST(request: NextRequest): Promise<NextResponse<ExecuteTr
console.log(`📐 Symbol-specific sizing for ${driftSymbol}:`) console.log(`📐 Symbol-specific sizing for ${driftSymbol}:`)
console.log(` Enabled: ${enabled}`) console.log(` Enabled: ${enabled}`)
console.log(` Position size: $${positionSize}`) console.log(` Position size: $${positionSize.toFixed(2)} (${usePercentage ? 'percentage' : 'fixed'})`)
console.log(` Leverage: ${leverage}x`) console.log(` Leverage: ${leverage}x`)
// Initialize Drift service if not already initialized
const driftService = await initializeDriftService()
// Check account health before trading
const health = await driftService.getAccountHealth()
console.log('💊 Account health:', health)
if (health.freeCollateral <= 0) { if (health.freeCollateral <= 0) {
return NextResponse.json( return NextResponse.json(
{ {