Files
trading_bot_v3/app/api/safe-paper-trading/create-trade/route.js
mindesbunister 532c5c888e feat: implement 24/7 server-side automation with AI learning integration
Core Features:
- True 24/7 automation runs without browser dependency
- Server-side process in Docker container
- Auto-executes paper trades with ≥60% confidence
- Integrates with existing AI learning system
- Safe paper trading mode only (zero real money risk)

- working-24x7.js: Main automation process (currently running)
- check-automation.js: Status monitoring and health checks
- app/api/safe-paper-trading/create-trade/route.js: Paper trade API
- app/api/automation-24x7/route.js: Automation control API

- Fixed continuous learning state persistence issues
- Added force enable function for debugging: window.forceEnableLearning()
- Enhanced state restoration logic with immediate and delayed checks
- Auto-execute toggle now properly unlocks when continuous learning active

- System running successfully (PID: 3922502)
- Already executed first automated paper trade (80% confidence SELL)
- Scheduled to run every 60 minutes automatically
- Logs all activity for monitoring and debugging

ical Implementation:
- Uses curl for HTTP requests (no fetch dependencies)
- Background process with proper signal handling
- Comprehensive error handling and logging
- Integration with existing analysis pipeline
- Maintains compatibility with browser-based safe paper trading

This completes the 24/7 automation requirement - system now runs continuously
in Docker container without requiring browser tabs to remain open.
2025-08-05 23:41:29 +02:00

112 lines
3.3 KiB
JavaScript

import { NextResponse } from 'next/server'
// Simple in-memory storage for paper trades (in production, use database)
let paperTrades = []
let tradeIdCounter = 1
export async function POST(request) {
try {
const tradeData = await request.json()
// Validate required fields
const required = ['symbol', 'side', 'amount', 'entry', 'confidence']
for (const field of required) {
if (!tradeData[field]) {
return NextResponse.json({
success: false,
message: `Missing required field: ${field}`
}, { status: 400 })
}
}
// Create paper trade
const trade = {
id: `PAPER_${Date.now()}_${tradeIdCounter++}`,
symbol: tradeData.symbol,
side: tradeData.side,
amount: tradeData.amount,
entry: tradeData.entry,
stopLoss: tradeData.stopLoss,
takeProfit: tradeData.takeProfit,
confidence: tradeData.confidence,
reasoning: tradeData.reasoning,
source: tradeData.source || 'manual',
status: 'OPEN',
createdAt: new Date().toISOString(),
pnl: 0,
fees: 0
}
// Store trade
paperTrades.push(trade)
console.log(`📄 Paper trade created: ${trade.id} - ${trade.side} ${trade.symbol} at $${trade.entry} (${trade.confidence}% confidence)`)
// Log to AI learning system if available
try {
const learningData = {
id: `decision_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
symbol: trade.symbol,
timeframe: '60',
side: trade.side,
confidence: trade.confidence,
entry: trade.entry,
stopLoss: trade.stopLoss,
takeProfit: trade.takeProfit,
reasoning: trade.reasoning,
source: 'paper_trade_automation',
createdAt: new Date().toISOString()
}
// Store in learning system (try to call learning API)
fetch('http://localhost:9001/api/ai-learning/record-decision', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(learningData)
}).catch(error => {
console.log('⚠️ Could not log to learning system:', error.message)
})
} catch (error) {
console.log('⚠️ Learning system integration error:', error.message)
}
return NextResponse.json({
success: true,
message: 'Paper trade created successfully',
trade: trade
})
} catch (error) {
console.error('❌ Create paper trade error:', error)
return NextResponse.json({
success: false,
message: 'Failed to create paper trade',
error: error.message
}, { status: 500 })
}
}
export async function GET(request) {
try {
// Return all paper trades
return NextResponse.json({
success: true,
trades: paperTrades,
summary: {
total: paperTrades.length,
open: paperTrades.filter(t => t.status === 'OPEN').length,
closed: paperTrades.filter(t => t.status === 'CLOSED').length,
totalPnL: paperTrades.reduce((sum, t) => sum + (t.pnl || 0), 0)
}
})
} catch (error) {
console.error('❌ Get paper trades error:', error)
return NextResponse.json({
success: false,
message: 'Failed to get paper trades',
error: error.message
}, { status: 500 })
}
}