ADVANCED SYSTEM KNOWLEDGE: - Superior parallel screenshot system (60% performance gain) - AI learning system architecture and decision flow - Orphaned order cleanup integration patterns - Critical technical fixes and troubleshooting guide - Database schema best practices - Memory leak prevention strategies - AI learning system patterns and functions - Error handling best practices for trading systems - Integration patterns for position monitoring - Performance optimization rules - UI/UX consistency requirements - Critical anti-patterns to avoid - Added links to new knowledge base documents - Comprehensive documentation structure - Development guides and best practices - Performance optimizations summary - 60% screenshot performance improvement techniques - AI learning system that adapts trading decisions - Container stability and crash prevention - Frontend-backend consistency requirements - Integration strategies for existing infrastructure This documentation preserves critical insights from complex debugging sessions and provides patterns for future development.
8.2 KiB
8.2 KiB
🧠 AI Learning & Advanced System Knowledge
🎯 Critical System Components (Learned from Session)
📊 Superior Parallel Screenshot System
BREAKTHROUGH: 60% Performance Improvement
// Key Implementation in lib/superior-screenshot-service.ts
- Parallel capture vs sequential: 71s vs 180s for 3 timeframes
- Trading Presets Must Match Frontend UI EXACTLY:
* Scalp: 5m,15m,30m (NOT 5m,15m,1h)
* Day: 1h,2h (NOT 1h,4h,1d)
* Swing: 4h,1D
* Extended: 1m-1D comprehensive
Critical Lesson: Frontend UI is the source of truth for preset definitions.
🧹 Orphaned Order Cleanup Integration
PROBLEM SOLVED: Drift always leaves opposite positions open after SL/TP hits
// Integration Point: app/api/automation/position-monitor/route.js
- Triggers cleanup ONLY when hasPosition: false
- Uses existing frequent position monitoring (no redundant polling)
- Provides detailed cleanup results in monitoring response
Key Insight: Leverage existing monitoring infrastructure vs creating separate timers
🤖 AI Learning System Architecture
CRITICAL COMPONENT: Actual learning system that adapts trading decisions
// lib/simplified-stop-loss-learner.js - Core Learning Functions:
1. recordDecision() - Logs every risk management choice
2. assessDecisionOutcome() - Tracks what actually happened
3. getSmartRecommendation() - AI suggestions based on learned patterns
4. generateLearningReport() - 15-minute learning progress reports
// Learning Flow:
Risk Manager -> Records Decision -> Waits 5min -> Assesses Outcome -> Updates Thresholds
Key Learning: This isn't just statistics - it actively influences trading decisions!
🔧 Critical Technical Fixes
Database Schema Issues
// ISSUE: Prisma validation errors crashed container
// FIX: Always provide unique ID for ai_learning_data records
await prisma.ai_learning_data.create({
data: {
id: `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
// ... other fields
}
});
Memory Leak Prevention
// ISSUE: Unhandled promise rejections caused EventEmitter overflow
// FIX: Proper error handling with try/catch and function existence checks
if (typeof this.learner.generateLearningReport === 'function') {
const report = await this.learner.generateLearningReport();
}
Import Path Corrections
// ISSUE: Module not found errors
// FIX: Use correct relative paths for database utilities
const { getDB } = require('./db'); // NOT './database-util'
🎯 AI Learning System Deep Dive
How The AI Actually Learns
-
Pattern Recognition:
"When SOL is 4% from SL with bullish momentum, holding works 73% of time" -
Threshold Optimization:
Original: Emergency=1%, Risk=2% After Learning: Emergency=0.7%, Risk=1.8% (based on outcomes) -
Smart Recommendations:
// AI analyzes similar historical situations const recommendation = await learner.getSmartRecommendation({ distanceFromSL: 3.5, symbol: 'SOL-PERP', marketConditions: { /* current state */ } }); // Returns: EMERGENCY_EXIT vs HOLD_CONFIDENT based on learned patterns
Learning Confidence Progression
0-5 decisions: 30% confidence (LOW)
5-20 decisions: 40-60% confidence (MEDIUM)
20-50 decisions: 60-80% confidence (HIGH)
50+ decisions: 80-95% confidence (EXPERT)
🚨 Critical Error Patterns & Solutions
Container Crash Root Causes
- Database Schema Violations → Add unique IDs to all Prisma records
- Missing Function Calls → Implement all required interfaces
- Memory Leaks from Unhandled Errors → Comprehensive error handling
- Configuration Deprecations → Keep configs updated with framework changes
Next.js Common Issues
// Issue: serverComponentsExternalPackages deprecated
// Old: experimental.serverComponentsExternalPackages
// New: serverExternalPackages
// Issue: Module resolution in Docker
// Fix: Ensure correct relative paths for all imports
💡 Development Best Practices Discovered
1. Integration Strategy
- Leverage Existing Infrastructure: Don't create redundant polling when monitoring already exists
- Gradual Enhancement: Add features to existing endpoints vs creating new ones
- Fail Gracefully: Always provide fallbacks for AI/learning features
2. Testing Approach
// Always test critical components in isolation
node test-learning-system.js // Test AI learning
node test-orphaned-cleanup.js // Test cleanup integration
curl /api/automation/position-monitor // Test monitoring
3. Error Handling Philosophy
// Defensive Programming for AI Systems
try {
const aiResult = await aiFunction();
return aiResult;
} catch (error) {
logger.error(`AI function failed: ${error.message}`);
return fallbackFunction(); // Always have a fallback
}
🎯 Performance Optimizations
Screenshot Capture
- Parallel Processing: 60% time savings over sequential
- Session Reuse: Avoid repeated logins/captchas
- Error Isolation: One layout failure doesn't break others
Database Operations
- Batch Inserts: For multiple learning records
- Indexed Queries: On frequently searched fields (symbol, createdAt)
- Connection Pooling: Reuse database connections
Container Optimization
# Multi-stage builds for smaller images
# Non-root user for security
# Health checks for monitoring
# Proper signal handling for graceful shutdowns
🧪 Testing Protocols
AI Learning System
# Test learning functions
node test-learning-system.js
# Expected output:
✅ Learning report generated: 0 decisions, 30% confidence
✅ Smart recommendation: MONITOR at 3.5% distance
Integration Testing
# Test orphaned cleanup integration
curl /api/automation/position-monitor | jq '.monitor.orphanedOrderCleanup'
# Test parallel screenshots
curl -X POST /api/superior-screenshot -d '{"timeframes":["5m","15m","30m"]}'
System Health
# Monitor for critical errors
docker logs trader_dev --since="1m" | grep -E "(Error|unhandled|crash)"
# Should return: 0 errors
📈 Future Enhancement Opportunities
1. Advanced Learning Features
- Market Condition Clustering: Group similar market states
- Volatility Adaptation: Adjust thresholds based on VIX/volatility
- Time-of-Day Learning: Different strategies for different sessions
2. Performance Improvements
- WebSocket Integration: Real-time position monitoring
- Caching Layer: Redis for frequently accessed data
- GPU Acceleration: For complex AI computations
3. Risk Management Enhancements
- Portfolio-Level Learning: Cross-symbol pattern recognition
- Drawdown Protection: Automatic position sizing reduction
- Correlation Analysis: Avoid over-concentration
🔍 Debugging Guide
Common Issues & Solutions
-
Container Won't Start
# Check syntax errors find . -name "*.js" -exec node -c {} \; # Check Docker logs docker logs trader_dev --tail=50 -
AI Learning Not Working
# Test learning functions node -e " const Learner = require('./lib/simplified-stop-loss-learner'); const l = new Learner(); l.generateLearningReport().then(console.log); " -
Database Connection Issues
# Test database connectivity node -e " const { getDB } = require('./lib/db'); getDB().then(() => console.log('DB connected')); "
🎓 Key Learnings for Future Development
- Always Verify Frontend-Backend Consistency: UI defines truth
- Implement Comprehensive Error Handling: Prevent cascade failures
- Use Existing Infrastructure: Don't reinvent monitoring/polling
- Test AI Components Independently: Isolate learning system testing
- Document Integration Points: Critical for maintenance
- Monitor System Health: Proactive error detection
- Version Control Critical Fixes: Always commit stability improvements
This knowledge base captures critical insights that took significant debugging to discover. Use it to avoid repeating complex troubleshooting and to guide future enhancements.