🚨 CRITICAL FIX: Stop cleanup from canceling SL/TP orders

- Fixed cleanup-orders logic to NEVER cancel reduce-only orders (SL/TP)
- Updated position monitor to be more conservative with cleanup
- This was causing SL/TP orders to be canceled after position creation
- Positions were left unprotected due to aggressive cleanup logic
This commit is contained in:
mindesbunister
2025-07-29 19:36:52 +02:00
parent 7522baa0f1
commit ce170b319e
3 changed files with 15 additions and 6 deletions

View File

@@ -98,9 +98,13 @@ export async function GET() {
if (activeOrders.length > 0) {
console.log('📋 No active positions detected - checking for truly orphaned orders...');
// Filter for truly orphaned orders (non-reduce-only orders without positions)
// Do NOT clean up reduce-only orders as these could be legitimate SL/TP from recently closed positions
const trulyOrphanedOrders = activeOrders.filter(order => !order.reduceOnly);
// Filter for truly orphaned orders (ONLY non-reduce-only orders without positions)
// 🛡️ CRITICAL: NEVER clean up reduce-only orders as these are SL/TP protecting positions
const trulyOrphanedOrders = activeOrders.filter(order => {
// Only consider non-reduce-only orders for cleanup
// Reduce-only orders (SL/TP) should NEVER be automatically canceled
return !order.reduceOnly
});
if (trulyOrphanedOrders.length > 0) {
console.log(`🎯 Found ${trulyOrphanedOrders.length} truly orphaned orders (non-reduce-only) - triggering cleanup...`);

View File

@@ -99,9 +99,14 @@ export async function POST() {
// Check if this order is for a market where we have no position
const hasPosition = positionMarkets.has(order.marketIndex)
// CORRECTED LOGIC: Cancel ALL orders when no position exists
// If we have no position, then ALL orders (including SL/TP) are orphaned
return !hasPosition
// 🛡️ CRITICAL FIX: Only cancel non-reduce-only orders when no position exists
// NEVER cancel reduce-only orders (SL/TP) as they protect existing positions
if (!hasPosition) {
// Only cancel orders that are NOT reduce-only (market makers, limit orders)
return !order.reduceOnly
}
return false // Don't cancel any orders when position exists
})
// Additionally, find lingering SL/TP orders when position has changed significantly