#!/usr/bin/env node /** * Test Drift Feedback Loop System * Comprehensive test of the real-trade learning feedback system */ async function testDriftFeedbackLoop() { console.log('๐Ÿงช TESTING DRIFT FEEDBACK LOOP SYSTEM') console.log('='.repeat(60)) try { console.log('๐Ÿ“Š Step 1: Testing Feedback Loop API Endpoints...') // Test 1: Get current status console.log('\n๐Ÿ” Checking current feedback loop status...') const statusResponse = await fetch('http://localhost:3000/api/drift/feedback', { method: 'GET' }) const statusResult = await statusResponse.json() console.log('Status:', statusResult) // Test 2: Start monitoring console.log('\n๐Ÿš€ Starting feedback loop monitoring...') const startResponse = await fetch('http://localhost:3000/api/drift/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'start_monitoring', userId: 'drift-user' }) }) const startResult = await startResponse.json() console.log('Start result:', startResult) // Test 3: Check trades manually console.log('\n๐Ÿ” Triggering manual trade check...') const checkResponse = await fetch('http://localhost:3000/api/drift/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'check_trades', userId: 'drift-user' }) }) const checkResult = await checkResponse.json() console.log('Check result:', checkResult) // Test 4: Get learning insights console.log('\n๐Ÿง  Getting learning insights...') const insightsResponse = await fetch('http://localhost:3000/api/drift/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'get_insights', userId: 'drift-user' }) }) const insightsResult = await insightsResponse.json() console.log('Learning insights:', JSON.stringify(insightsResult, null, 2)) // Test 5: Test with a real small trade (if confirmed) if (process.argv.includes('--place-test-trade')) { console.log('\n๐Ÿ’ฐ Placing small test trade to verify learning capture...') const testTradeResponse = await fetch('http://localhost:3000/api/drift/trade', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'place_order', symbol: 'SOL', side: 'buy', amount: 3, // Small amount for testing leverage: 1, stopLoss: true, takeProfit: true, stopLossPercent: 1.0, takeProfitPercent: 2.0 }) }) const testTradeResult = await testTradeResponse.json() console.log('Test trade result:', JSON.stringify(testTradeResult, null, 2)) if (testTradeResult.success && testTradeResult.result.success) { console.log('โœ… Test trade placed successfully!') console.log('๐Ÿ”— Transaction ID:', testTradeResult.result.transactionId) // Wait a moment, then check if learning record was created console.log('\nโณ Waiting 10 seconds for learning record creation...') await new Promise(resolve => setTimeout(resolve, 10000)) const updatedInsights = await fetch('http://localhost:3000/api/drift/feedback', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ action: 'get_insights', userId: 'drift-user' }) }) const updatedInsightsResult = await updatedInsights.json() console.log('Updated insights after trade:', updatedInsightsResult.insights.recentPerformance) } } console.log('\n๐Ÿ“ˆ Step 2: Testing Database Learning Records...') // Test database connection and recent records const { PrismaClient } = await import('@prisma/client') const prisma = new PrismaClient() // Check recent trades const recentTrades = await prisma.trade.findMany({ where: { userId: 'drift-user', driftTxId: { not: null } }, orderBy: { createdAt: 'desc' }, take: 5 }) console.log(`๐Ÿ“Š Found ${recentTrades.length} recent Drift trades in database`) recentTrades.forEach((trade, index) => { console.log(` ${index + 1}. ${trade.symbol} ${trade.side} - Status: ${trade.status} - Outcome: ${trade.outcome || 'PENDING'}`) }) // Check AI learning data const learningRecords = await prisma.aILearningData.findMany({ where: { userId: 'drift-user' }, orderBy: { createdAt: 'desc' }, take: 3 }) console.log(`๐Ÿง  Found ${learningRecords.length} AI learning records`) await prisma.$disconnect() console.log('\n๐ŸŽฏ Step 3: Feedback Loop Validation...') // Test feedback loop components const components = { 'Trade Execution Capture': recentTrades.length > 0, 'Learning Record Creation': learningRecords.length > 0, 'API Endpoints': statusResult.success, 'Monitoring System': startResult.success, 'Real-time Checking': checkResult.success, 'Insights Generation': insightsResult.success } console.log('\nโœ… Component Status:') Object.entries(components).forEach(([component, status]) => { console.log(` ${status ? 'โœ…' : 'โŒ'} ${component}`) }) const allWorking = Object.values(components).every(status => status) console.log('\n๐ŸŽ‰ FEEDBACK LOOP TEST RESULTS:') console.log('='.repeat(40)) if (allWorking) { console.log('โœ… ALL SYSTEMS OPERATIONAL!') console.log('๐Ÿ”„ Drift feedback loop is ready for real trading') console.log('๐Ÿ“š AI will learn from every real Drift trade') console.log('๐Ÿš€ System will continuously improve based on outcomes') } else { console.log('โš ๏ธ Some components need attention') console.log('๐Ÿ”ง Check the failed components above') } console.log('\n๐Ÿ’ก USAGE INSTRUCTIONS:') console.log('1. Start monitoring: POST /api/drift/feedback {"action":"start_monitoring"}') console.log('2. Place trades normally via /api/drift/trade') console.log('3. System automatically captures outcomes and learns') console.log('4. Get insights: POST /api/drift/feedback {"action":"get_insights"}') if (!process.argv.includes('--place-test-trade')) { console.log('\n๐Ÿ’ฐ To test with real trade: node test-drift-feedback-loop.js --place-test-trade') } } catch (error) { console.error('โŒ Test failed:', error.message) if (error.message.includes('ECONNREFUSED')) { console.log('\n๐Ÿ’ก Solution: Make sure the trading bot is running:') console.log(' docker compose -f docker-compose.dev.yml up') } } } async function demonstrateWorkflow() { console.log('\n๐Ÿ”„ DRIFT FEEDBACK LOOP WORKFLOW DEMONSTRATION') console.log('='.repeat(60)) console.log(` ๐Ÿ“ˆ NORMAL TRADING WORKFLOW WITH FEEDBACK LOOP: 1. ๐Ÿค– AI analyzes chart screenshot โ†’ Generates analysis with confidence score โ†’ Stored in ai_learning_data table 2. ๐Ÿ’ฐ Trade is placed on Drift Protocol โ†’ Creates trade record with AI metadata โ†’ Links to AI analysis via tradeId 3. ๐Ÿ”„ Feedback loop monitors trade outcomes โ†’ Checks every 30 seconds for position changes โ†’ Detects when stop loss/take profit is hit 4. ๐Ÿ“Š Outcome is captured and analyzed โ†’ Updates trade record with outcome (WIN/LOSS/BREAKEVEN) โ†’ Calculates actual P&L and risk/reward ratio โ†’ Links back to original AI analysis 5. ๐Ÿง  AI learning is updated โ†’ Analysis accuracy is measured โ†’ Confidence validation is performed โ†’ Pattern success rates are calculated 6. ๐Ÿš€ AI improves for next trade โ†’ Uses historical outcome data โ†’ Adjusts confidence based on past accuracy โ†’ Optimizes strategies based on what works RESULT: Self-improving AI that gets better with each trade! ๐ŸŽฏ `) console.log('๐Ÿ“š DATABASE SCHEMA FOR LEARNING:') console.log(` Trades Table: - Records every Drift trade with outcome - Links to AI analysis that generated it - Tracks P&L, risk/reward, execution details AI Learning Data Table: - Stores every AI analysis and prediction - Updated with actual outcomes when available - Builds database of AI accuracy over time Feedback Loop Process: - Monitors Drift positions in real-time - Captures exact trade outcomes - Feeds results back to AI learning system `) } if (require.main === module) { testDriftFeedbackLoop().then(() => { if (!process.argv.includes('--no-demo')) { demonstrateWorkflow() } }) } module.exports = { testDriftFeedbackLoop }