#!/usr/bin/env node /** * Position Consolidation Test * Cleans up fragmented orders and creates simple 3-order structure */ async function testConsolidation() { console.log('๐Ÿงน TESTING POSITION CONSOLIDATION'); console.log('='.repeat(50)); try { // 1. Get current position console.log('1๏ธโƒฃ Fetching current position...'); const positionResponse = await fetch('http://localhost:9001/api/drift/positions'); const positionData = await positionResponse.json(); if (!positionData.success || !positionData.positions.length) { console.log('โŒ No active positions found'); return; } const position = positionData.positions[0]; console.log(` ๐Ÿ“Š Position: ${position.side.toUpperCase()} ${position.size} ${position.symbol}`); console.log(` ๐Ÿ’ฐ Entry: $${position.entryPrice.toFixed(4)}`); console.log(` ๐Ÿ“ˆ Current: $${(position.markPrice || position.entryPrice).toFixed(4)}`); console.log(` ๐Ÿ’ธ P&L: $${position.unrealizedPnl.toFixed(2)}`); // 2. Get current orders count console.log('\n2๏ธโƒฃ Checking current orders...'); const ordersResponse = await fetch('http://localhost:9001/api/drift/orders'); const ordersData = await ordersResponse.json(); const activeOrders = ordersData.orders.filter(o => o.status === 'OPEN'); console.log(` ๐Ÿ“‹ Active orders: ${activeOrders.length}`); // 3. Calculate consolidated levels console.log('\n3๏ธโƒฃ Calculating consolidated levels...'); const entryPrice = position.entryPrice; const size = position.size; const side = position.side.toLowerCase(); // Dynamic levels based on position const stopLossPercent = 1.5; // 1.5% protective stop const tp1Percent = 2.6; // 2.6% first target const tp2Percent = 4.2; // 4.2% extended target let stopLoss, takeProfit1, takeProfit2; if (side === 'long') { stopLoss = entryPrice * (1 - stopLossPercent / 100); takeProfit1 = entryPrice * (1 + tp1Percent / 100); takeProfit2 = entryPrice * (1 + tp2Percent / 100); } else { stopLoss = entryPrice * (1 + stopLossPercent / 100); takeProfit1 = entryPrice * (1 - tp1Percent / 100); takeProfit2 = entryPrice * (1 - tp2Percent / 100); } const tp1Size = Math.floor(size * 0.7 * 100) / 100; // 70% const tp2Size = size - tp1Size; // 30% console.log(` ๐Ÿ›‘ Stop Loss: $${stopLoss.toFixed(4)} (${stopLossPercent}% risk)`); console.log(` ๐ŸŽฏ Take Profit 1: $${takeProfit1.toFixed(4)} (${tp1Percent}% gain) - ${tp1Size} SOL`); console.log(` ๐Ÿš€ Take Profit 2: $${takeProfit2.toFixed(4)} (${tp2Percent}% gain) - ${tp2Size} SOL`); console.log(` โš–๏ธ Risk/Reward: ${(tp1Percent / stopLossPercent).toFixed(1)}:1`); // 4. Show consolidation plan console.log('\n4๏ธโƒฃ CONSOLIDATION PLAN:'); console.log(' ๐Ÿ“‰ BEFORE: 24+ fragmented orders'); console.log(' ๐Ÿ“ˆ AFTER: 3 clean orders'); console.log(' โœ… Benefits:'); console.log(' โ€ข Clear risk management'); console.log(' โ€ข Lower transaction costs'); console.log(' โ€ข Better profit optimization'); console.log(' โ€ข Easier monitoring'); console.log('\n๐Ÿ’ก RECOMMENDED NEXT STEPS:'); console.log('1. Cancel all existing orders'); console.log('2. Place single stop loss for full position'); console.log('3. Place two take profit orders (70%/30% split)'); return { success: true, currentOrders: activeOrders.length, consolidatedOrders: 3, position: { symbol: position.symbol, side: position.side, size: position.size, entryPrice: position.entryPrice }, levels: { stopLoss, takeProfit1, takeProfit2, tp1Size, tp2Size } }; } catch (error) { console.error('โŒ Consolidation test failed:', error.message); return { success: false, error: error.message }; } } // Run the test testConsolidation().then(result => { if (result.success) { console.log('\nโœ… CONSOLIDATION ANALYSIS COMPLETE'); console.log(`๐Ÿ“Š Reduction: ${result.currentOrders} โ†’ ${result.consolidatedOrders} orders`); } }).catch(console.error);