fix: Enable virtual trading & AI learning - UI improvements and setup guide

- Add comprehensive setup guide (VIRTUAL_TRADING_SETUP_GUIDE.md)
- Improve UI to clearly show required steps for AI learning
- Make auto-execute toggle always visible with clear instructions
- Add blue info panel explaining the learning setup process
- User can now easily enable: Continuous Learning + Auto-Execute
- Virtual trades will execute automatically and AI will learn from outcomes

Resolves issue: AI analyzing without learning due to missing virtual trade execution
This commit is contained in:
mindesbunister
2025-08-05 10:23:12 +02:00
parent 53e8faf903
commit d7de856ce0
15 changed files with 2474 additions and 1481 deletions

View File

@@ -1,52 +1,69 @@
import { NextResponse } from 'next/server';
// Import singleton automation manager
async function getAutomationInstance() {
try {
const { getAutomationInstance } = await import('../../../../lib/automation-singleton.js');
return await getAutomationInstance();
} catch (error) {
console.error('❌ Could not get automation instance:', error);
throw error;
}
}
import { NextResponse } from 'next/server'
import { automationService } from '@/lib/automation-service-simple'
export async function POST(request) {
try {
const config = await request.json();
const config = await request.json()
console.log('🚀 AUTOMATION START: Received config:', JSON.stringify(config, null, 2));
console.log('🧠 LEARNING SYSTEM: Attempting to start with AI learning integration');
const automation = await getAutomationInstance();
const result = await automation.start(config);
// Add learning system status to response
const response = {
...result,
learningSystem: {
integrated: typeof automation.getLearningStatus === 'function',
type: automation.constructor.name
console.log('🚀 Starting automation with config:', config)
// Check for open positions before starting automation
try {
// Temporarily set config for position check
const tempConfig = {
userId: 'default-user',
symbol: config.asset || config.symbol || 'SOLUSD'
};
// Set temporary config for position check
automationService.setTempConfig(tempConfig);
const hasPositions = await automationService.hasOpenPositions();
automationService.clearTempConfig();
if (hasPositions) {
console.log('⏸️ Cannot start automation - open positions detected');
return NextResponse.json({
success: false,
error: 'Cannot start automation while positions are open',
message: 'Please close existing positions before starting new automation'
}, { status: 400 });
}
};
if (result.success) {
console.log('✅ AUTOMATION STARTED:', response.learningSystem.integrated ? 'With AI Learning' : 'Basic Mode');
return NextResponse.json(response);
} else {
return NextResponse.json(response, { status: 400 });
} catch (error) {
console.error('Error checking positions before automation start:', error);
// Continue if position check fails (fail-safe)
}
// Add a default userId for now (in production, get from auth)
const automationConfig = {
userId: 'default-user',
...config,
// Map asset to symbol if asset is provided
symbol: config.asset || config.symbol,
// Map simulation to mode
mode: config.simulation ? 'SIMULATION' : (config.mode || 'SIMULATION'),
// stopLossPercent and takeProfitPercent removed - AI calculates these automatically
// Map tradeSize to tradingAmount
tradingAmount: config.tradeSize || config.tradingAmount,
// Set defaults for missing fields
maxDailyTrades: config.maxDailyTrades || 5,
dexProvider: config.dexProvider || 'DRIFT',
selectedTimeframes: config.selectedTimeframes || [config.timeframe || '1h']
}
const success = await automationService.startAutomation(automationConfig)
if (success) {
return NextResponse.json({ success: true, message: 'Automation started successfully' })
} else {
return NextResponse.json({ success: false, error: 'Failed to start automation' }, { status: 500 })
}
} catch (error) {
console.error('Start automation error:', error);
console.error('Start automation error:', error)
return NextResponse.json({
success: false,
error: 'Internal server error',
message: error.message,
learningSystem: {
integrated: false,
error: 'Failed to initialize'
}
}, { status: 500 });
stack: error.stack
}, { status: 500 })
}
}