#!/usr/bin/env node // EMERGENCY: Add stop loss to unprotected position const axios = require('axios'); async function emergencyStopLoss() { console.log('🚨 EMERGENCY STOP LOSS PROTECTION'); console.log('='.repeat(50)); const baseUrl = 'http://localhost:9001'; try { // 1. Check current position console.log('šŸ“Š Checking current position...'); const positionsResponse = await axios.get(`${baseUrl}/api/drift/positions`); const positions = positionsResponse.data.positions; if (positions && positions.length > 0) { const position = positions[0]; console.log('āš ļø DANGEROUS POSITION FOUND:'); console.log(` Symbol: ${position.symbol}`); console.log(` Side: ${position.side.toUpperCase()}`); console.log(` Size: ${position.size}`); console.log(` Entry: $${position.entryPrice}`); console.log(` Current: $${position.markPrice}`); console.log(` PnL: $${position.unrealizedPnl.toFixed(4)}`); console.log(` Value: $${position.notionalValue.toFixed(2)}`); // Calculate emergency stop loss (2% risk) const entryPrice = position.entryPrice; const isShort = position.side.toLowerCase() === 'short'; let stopLossPrice; if (isShort) { // SHORT position - stop loss ABOVE entry (if price goes up) stopLossPrice = entryPrice * 1.02; // 2% above entry } else { // LONG position - stop loss BELOW entry (if price goes down) stopLossPrice = entryPrice * 0.98; // 2% below entry } console.log(`\nšŸ›”ļø PLACING EMERGENCY STOP LOSS:`); console.log(` Stop Loss Price: $${stopLossPrice.toFixed(3)}`); console.log(` Risk: 2% of position value`); // Place stop loss order try { const stopLossResponse = await axios.post(`${baseUrl}/api/drift/place-order`, { symbol: position.symbol, side: isShort ? 'BUY' : 'SELL', // Opposite side to close position orderType: 'STOP_MARKET', triggerPrice: stopLossPrice, amount: Math.abs(position.size), reduceOnly: true }); if (stopLossResponse.data.success) { console.log('āœ… EMERGENCY STOP LOSS PLACED SUCCESSFULLY!'); console.log(` Order ID: ${stopLossResponse.data.orderId || 'N/A'}`); } else { console.log('āŒ Failed to place stop loss:', stopLossResponse.data.error); } } catch (slError) { console.log('āŒ Stop loss placement failed:', slError.message); console.log('āš ļø MANUAL ACTION REQUIRED: Place stop loss immediately in Drift app'); } // Also try to place a take profit (4% gain) let takeProfitPrice; if (isShort) { // SHORT position - take profit BELOW entry (if price goes down) takeProfitPrice = entryPrice * 0.96; // 4% below entry } else { // LONG position - take profit ABOVE entry (if price goes up) takeProfitPrice = entryPrice * 1.04; // 4% above entry } console.log(`\nšŸ“ˆ PLACING TAKE PROFIT:`); console.log(` Take Profit Price: $${takeProfitPrice.toFixed(3)}`); console.log(` Target: 4% profit`); try { const tpResponse = await axios.post(`${baseUrl}/api/drift/place-order`, { symbol: position.symbol, side: isShort ? 'BUY' : 'SELL', orderType: 'LIMIT', price: takeProfitPrice, amount: Math.abs(position.size), reduceOnly: true }); if (tpResponse.data.success) { console.log('āœ… TAKE PROFIT PLACED SUCCESSFULLY!'); } else { console.log('āš ļø Take profit placement failed:', tpResponse.data.error); } } catch (tpError) { console.log('āš ļø Take profit placement failed:', tpError.message); } } else { console.log('āœ… No positions found - account is safe'); } } catch (error) { console.log('āŒ Emergency protection failed:', error.message); console.log('\nšŸ†˜ MANUAL ACTION REQUIRED:'); console.log(' 1. Open Drift app immediately'); console.log(' 2. Place stop loss order manually'); console.log(' 3. Set stop loss 2% from entry price'); console.log(' 4. Consider closing position entirely'); } console.log('\nšŸ” INVESTIGATING BUG:'); console.log(' • Paper trading page should NOT place real trades'); console.log(' • This is a critical system bug'); console.log(' • Need to identify why paper trading used live API'); console.log(' • Must fix immediately before any more damage'); } emergencyStopLoss().catch(console.error);