feat: integrate AI learning system with trading automation

- AutomationWithLearning class with decision recording and outcome assessment
- Enhanced API endpoints with learning status visibility
- Singleton automation manager for seamless learning system integration
- EnhancedAILearningPanel component for real-time learning visibility
- Learning-enhanced trade execution with AI adjustments to SL/TP
- Automatic decision tracking and outcome-based learning

 Key Features:
- Records trading decisions before execution
- Enhances analysis with learned patterns
- Tracks trade outcomes for continuous improvement
- Provides full visibility into AI decision-making process
- Integrates SimplifiedStopLossLearner with real trading flow

- Whether learning system is active
- How many decisions are being tracked
- Real-time learning statistics and insights
- AI enhancements applied to trading decisions
This commit is contained in:
mindesbunister
2025-07-27 12:11:40 +02:00
parent 5017a63db5
commit 44968c3bb3
10 changed files with 1488 additions and 134 deletions

View File

@@ -0,0 +1,71 @@
// API route to get detailed learning system status and visibility
import { NextResponse } from 'next/server';
// Import the automation instance to get learning status
async function getAutomationInstance() {
try {
// Import the singleton automation instance
const { getAutomationInstance } = await import('../../../lib/automation-singleton.js');
return getAutomationInstance();
} catch (error) {
console.error('❌ Could not get automation instance:', error);
return null;
}
}
export async function GET() {
try {
const automation = await getAutomationInstance();
if (!automation) {
return NextResponse.json({
success: false,
message: 'Automation instance not available'
}, { status: 503 });
}
// Check if automation has learning capabilities
if (typeof automation.getLearningStatus !== 'function') {
return NextResponse.json({
success: true,
learningSystem: {
enabled: false,
message: 'Basic automation running - learning system not integrated',
recommendation: 'Restart automation to enable AI learning system'
}
});
}
// Get detailed learning status
const learningStatus = await automation.getLearningStatus();
const automationStatus = automation.getStatus();
return NextResponse.json({
success: true,
learningSystem: {
...learningStatus,
automationRunning: automationStatus.isRunning,
totalCycles: automationStatus.stats.totalCycles,
totalTrades: automationStatus.stats.totalTrades
},
visibility: {
decisionTrackingActive: learningStatus.activeDecisions > 0,
learningDatabaseConnected: learningStatus.enabled,
aiEnhancementsActive: learningStatus.learningActive,
lastUpdateTime: new Date().toISOString()
}
});
} catch (error) {
console.error('❌ Error getting learning system status:', error);
return NextResponse.json({
success: false,
error: error.message,
learningSystem: {
enabled: false,
error: 'Failed to retrieve learning status'
}
}, { status: 500 });
}
}

View File

@@ -1,26 +1,52 @@
import { NextResponse } from 'next/server';
import { simpleAutomation } from '@/lib/simple-automation';
// 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;
}
}
export async function POST(request) {
try {
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 result = await simpleAutomation.start(config);
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
}
};
if (result.success) {
return NextResponse.json(result);
console.log('✅ AUTOMATION STARTED:', response.learningSystem.integrated ? 'With AI Learning' : 'Basic Mode');
return NextResponse.json(response);
} else {
return NextResponse.json(result, { status: 400 });
return NextResponse.json(response, { status: 400 });
}
} 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
message: error.message,
learningSystem: {
integrated: false,
error: 'Failed to initialize'
}
}, { status: 500 });
}
}

View File

@@ -1,15 +1,56 @@
import { NextResponse } from 'next/server';
import { simpleAutomation } from '@/lib/simple-automation';
// 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);
return null;
}
}
export async function GET() {
try {
const status = simpleAutomation.getStatus();
const automation = await getAutomationInstance();
if (!automation) {
return NextResponse.json({
error: 'No automation instance available',
isRunning: false,
learningSystem: { enabled: false }
}, { status: 503 });
}
const status = automation.getStatus();
// Add learning system status if available
if (typeof automation.getLearningStatus === 'function') {
try {
const learningStatus = await automation.getLearningStatus();
status.learningSystem = learningStatus;
} catch (learningError) {
status.learningSystem = {
enabled: false,
error: learningError.message
};
}
} else {
status.learningSystem = {
enabled: false,
message: 'Basic automation - learning not integrated'
};
}
return NextResponse.json(status);
} catch (error) {
console.error('Status error:', error);
console.error('Status error:', error);
return NextResponse.json({
error: 'Failed to get status',
message: error.message
message: error.message,
isRunning: false,
learningSystem: { enabled: false, error: 'Status check failed' }
}, { status: 500 });
}
}

View File

@@ -1,10 +1,30 @@
import { simpleAutomation } from '@/lib/simple-automation';
// 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);
return null;
}
}
export async function POST() {
try {
console.log('🛑 AUTOMATION STOP: Request received');
const result = await simpleAutomation.stop();
const automation = await getAutomationInstance();
let result = { success: false, message: 'No automation instance available' };
if (automation) {
result = await automation.stop();
// Check if learning system was active
if (typeof automation.getLearningStatus === 'function') {
const learningStatus = await automation.getLearningStatus();
console.log('🧠 LEARNING SYSTEM: Stopped with', learningStatus.activeDecisions, 'active decisions');
}
}
// Additional cleanup
try {
@@ -17,7 +37,7 @@ export async function POST() {
return Response.json(result);
} catch (error) {
console.error('Stop automation error:', error);
console.error('Stop automation error:', error);
return Response.json({
success: false,
message: error.message