diff --git a/lib/simple-automation.js b/lib/simple-automation.js index 30aa2ac..f496b8a 100644 --- a/lib/simple-automation.js +++ b/lib/simple-automation.js @@ -352,27 +352,58 @@ class SimpleAutomation { if (!hasPosition) { console.log('๐Ÿƒโ€โ™‚๏ธ SCALPING MODE: No position detected - aggressive re-entry needed'); - // Clean up any remaining orders first - if (activeOrders > 0) { - console.log('๐Ÿงน CLEANUP: Canceling remaining orders before new entry...'); + // ๐Ÿงน MANDATORY CLEANUP: Always clear ALL orders before re-entry (regardless of count) + console.log('๐Ÿงน MANDATORY CLEANUP: Clearing all orphaned orders before re-entry...'); + try { + const cleanupResponse = await fetch(`${baseUrl}/api/drift/cleanup-orders`, { + method: 'POST' + }); + if (cleanupResponse.ok) { + const cleanupResult = await cleanupResponse.json(); + const totalCanceled = cleanupResult.summary?.totalCanceled || 0; + const activeOrdersAfter = cleanupResult.summary?.activeOrders || 0; + + console.log(`โœ… CLEANUP COMPLETE: ${totalCanceled} orders canceled`); + console.log(`๐Ÿ“Š CLEANUP RESULT: ${activeOrdersAfter} orders remaining`); + + // Additional safety check - if orders still remain, try cancel-all endpoint + if (activeOrdersAfter > 0) { + console.log('๐Ÿ”„ ADDITIONAL CLEANUP: Found remaining orders, using cancel-all...'); + const cancelAllResponse = await fetch(`${baseUrl}/api/drift/cancel-all-orders`, { + method: 'POST' + }); + if (cancelAllResponse.ok) { + const cancelAllResult = await cancelAllResponse.json(); + console.log(`โœ… CANCEL-ALL COMPLETE: ${cancelAllResult.totalCanceled || 0} additional orders canceled`); + } + } + } else { + console.warn(`โš ๏ธ Cleanup API error: ${cleanupResponse.status}`); + } + } catch (cleanupError) { + console.warn('โš ๏ธ Order cleanup failed:', cleanupError.message); + + // Fallback: Try direct cancel-all as backup try { - const cleanupResponse = await fetch(`${baseUrl}/api/drift/cleanup-orders`, { + console.log('๐Ÿ”„ FALLBACK CLEANUP: Attempting direct cancel-all...'); + const fallbackResponse = await fetch(`${baseUrl}/api/drift/cancel-all-orders`, { method: 'POST' }); - if (cleanupResponse.ok) { - const cleanupResult = await cleanupResponse.json(); - console.log(`โœ… CLEANUP COMPLETE: ${cleanupResult.summary?.totalCanceled || 0} orders canceled`); - } else { - console.warn(`โš ๏ธ Cleanup API error: ${cleanupResponse.status}`); + if (fallbackResponse.ok) { + const fallbackResult = await fallbackResponse.json(); + console.log(`โœ… FALLBACK COMPLETE: ${fallbackResult.totalCanceled || 0} orders canceled`); } - } catch (cleanupError) { - console.warn('โš ๏ธ Order cleanup failed:', cleanupError.message); + } catch (fallbackError) { + console.warn('โš ๏ธ Fallback cleanup also failed:', fallbackError.message); } } + // ๐Ÿ” VERIFY CLEANUP: Wait a moment and verify orders are cleared + await new Promise(resolve => setTimeout(resolve, 1000)); // 1 second delay + // For scalping, if no position, we ALWAYS analyze for immediate re-entry if (recommendation === 'START_TRADING' || !hasPosition) { - console.log('๐Ÿš€ IMMEDIATE ANALYSIS: Market is clear, scanning for entry opportunities...'); + console.log('๐Ÿš€ IMMEDIATE ANALYSIS: Market cleared, scanning for entry opportunities...'); // Continue with analysis below } } else { @@ -801,6 +832,38 @@ class SimpleAutomation { console.log(`๐Ÿ”ง DEBUG: Final leverage to use: ${optimalLeverage}x`); + // ๐Ÿงน MANDATORY CLEANUP: Clear all orders before new trade execution + console.log('๐Ÿงน PRE-TRADE CLEANUP: Ensuring no orphaned orders before new trade...'); + try { + const cleanupResponse = await fetch(`${apiBaseUrl}/api/drift/cleanup-orders`, { + method: 'POST' + }); + if (cleanupResponse.ok) { + const cleanupResult = await cleanupResponse.json(); + const totalCanceled = cleanupResult.summary?.totalCanceled || 0; + console.log(`โœ… PRE-TRADE CLEANUP: ${totalCanceled} orders cleared before trade execution`); + + // Additional safety: If orders still exist, use cancel-all + if (cleanupResult.summary?.activeOrders > 0) { + console.log('๐Ÿ”„ ADDITIONAL CLEANUP: Remaining orders found, using cancel-all...'); + const cancelAllResponse = await fetch(`${apiBaseUrl}/api/drift/cancel-all-orders`, { + method: 'POST' + }); + if (cancelAllResponse.ok) { + const cancelAllResult = await cancelAllResponse.json(); + console.log(`โœ… CANCEL-ALL COMPLETE: ${cancelAllResult.totalCanceled || 0} additional orders cleared`); + } + } + } else { + console.warn(`โš ๏ธ Pre-trade cleanup failed: ${cleanupResponse.status}`); + } + + // Brief delay to ensure cleanup is processed + await new Promise(resolve => setTimeout(resolve, 500)); + } catch (cleanupError) { + console.warn('โš ๏ธ Pre-trade cleanup error:', cleanupError.message); + } + // Use the trading API with proper fields for Drift const tradePayload = { symbol: this.config.symbol,