feat: Complete AI Learning Integration & Position Scaling DCA System
- Integrated SimplifiedStopLossLearner into automation - Every AI decision now recorded for learning (stop loss, take profit, confidence) - Trade outcomes tracked and compared to AI predictions - Learning patterns improve future AI decisions - Enhanced status dashboard with learning insights - Proper DCA: increase position size + adjust existing SL/TP (not create new) - AI-calculated optimal levels for scaled positions - Prevents order fragmentation (fixes 24+ order problem) - Unified risk management for entire scaled position TIMEFRAME-AWARE INTERVALS: - Scalping (5m/15m): 5-15 minute analysis intervals - Day Trading (1h/4h): 10-30 minute intervals - Swing Trading (4h/1d): 23-68 minute intervals - Perfect for 5-minute scalping with DCA protection - 2-hour DCA cooldown prevents order spam - Position existence checks before new trades - Direction matching validation - Learning-based decision improvements - AI calculates ALL levels (entry, SL, TP, leverage, scaling) - Every calculation recorded and learned from - Position scaling uses AI intelligence - Timeframe-appropriate analysis frequency - Professional order management - Continuous learning and improvement ADDRESSES ALL USER CONCERNS: - 5-minute scalping compatibility ✅ - Position scaling DCA (adjust existing SL/TP) ✅ - AI calculations being learned from ✅ - No order fragmentation ✅ - Intelligent automation with learning ✅ Files: automation, consolidation APIs, learning integration, tests, documentation
This commit is contained in:
81
app/api/drift/consolidate-position/route.js
Normal file
81
app/api/drift/consolidate-position/route.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { dryRun = true, analysis = null } = body;
|
||||
|
||||
console.log('🧹 CONSOLIDATING POSITION ORDERS');
|
||||
console.log(`Mode: ${dryRun ? 'DRY RUN' : 'LIVE EXECUTION'}`);
|
||||
console.log(`AI Analysis: ${analysis ? 'Provided - Using AI optimal levels' : 'Not provided - Using adaptive levels'}`);
|
||||
|
||||
// Get current position data
|
||||
const positionsResponse = await fetch('http://localhost:9001/api/drift/positions');
|
||||
const positionsData = await positionsResponse.json();
|
||||
|
||||
if (!positionsData.success || !positionsData.positions.length) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'No active positions found to consolidate'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
const position = positionsData.positions[0]; // Get first position
|
||||
|
||||
// Import the consolidator
|
||||
const PositionConsolidator = await import('../../../../lib/position-consolidator.js');
|
||||
|
||||
if (dryRun) {
|
||||
// Dry run: analyze only with AI analysis if provided
|
||||
const consolidatedPlan = await PositionConsolidator.default.analyzeAndConsolidate(analysis);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
mode: 'dry_run',
|
||||
plan: consolidatedPlan,
|
||||
message: analysis ? 'AI-optimized consolidation plan ready' : 'Adaptive consolidation plan ready',
|
||||
position: {
|
||||
symbol: position.symbol,
|
||||
side: position.side,
|
||||
size: position.size,
|
||||
entryPrice: position.entryPrice
|
||||
}
|
||||
});
|
||||
|
||||
} else {
|
||||
// Live execution with AI analysis if provided
|
||||
const consolidationResult = await PositionConsolidator.default.executeConsolidation(analysis);
|
||||
|
||||
if (consolidationResult.success) {
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: analysis ? 'Position consolidated using AI optimal levels' : 'Position consolidated using adaptive levels',
|
||||
consolidation: {
|
||||
ordersBefore: consolidationResult.ordersBefore,
|
||||
ordersAfter: consolidationResult.ordersAfter,
|
||||
position: {
|
||||
symbol: position.symbol,
|
||||
side: position.side,
|
||||
size: position.size,
|
||||
entryPrice: position.entryPrice
|
||||
}
|
||||
},
|
||||
orders: consolidationResult.results
|
||||
});
|
||||
} else {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: consolidationResult.error
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Position consolidation error:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Failed to consolidate position',
|
||||
details: error.message
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user