#!/usr/bin/env node /** * Simple 24/7 Automation Service * Direct execution without complex daemon management */ const { exec } = require('child_process'); const { promisify } = require('util'); const execAsync = promisify(exec); const fs = require('fs').promises; class SimpleAutomation { constructor() { this.isRunning = false; this.config = { symbol: 'SOLUSD', timeframe: '60', intervalMinutes: 60, autoExecuteThreshold: 60 }; this.stats = { startTime: new Date(), totalCycles: 0, totalTrades: 0 }; } async log(message) { const timestamp = new Date().toISOString(); const logEntry = `[${timestamp}] ${message}`; console.log(logEntry); // Also save to log file try { await fs.appendFile('/tmp/automation-simple.log', logEntry + '\n'); } catch (error) { // Ignore file errors } } async start() { if (this.isRunning) { await this.log('⚠️ Already running'); return; } this.isRunning = true; await this.log('πŸš€ Starting Simple 24/7 Automation...'); await this.log(`πŸ“Š Config: ${this.config.symbol} ${this.config.timeframe}m, every ${this.config.intervalMinutes} minutes`); // Run first cycle immediately await this.runCycle(); // Schedule recurring cycles setInterval(async () => { try { await this.runCycle(); } catch (error) { await this.log(`❌ Cycle error: ${error.message}`); } }, this.config.intervalMinutes * 60 * 1000); await this.log('βœ… 24/7 Automation running'); } async runCycle() { this.stats.totalCycles++; await this.log(`πŸ”„ Analysis cycle #${this.stats.totalCycles}`); try { // Get analysis using curl const { stdout } = await execAsync(`curl -s "http://localhost:9001/api/ai-analysis/latest?symbol=${this.config.symbol}&timeframe=${this.config.timeframe}"`); const data = JSON.parse(stdout); if (data.success && data.data && data.data.analysis) { const analysis = data.data.analysis; await this.log(`πŸ“Š Analysis: ${analysis.recommendation} (${analysis.confidence}% confidence)`); // Check if we should execute trade if (analysis.confidence >= this.config.autoExecuteThreshold) { await this.log(`🎯 Executing trade: confidence ${analysis.confidence}% β‰₯ ${this.config.autoExecuteThreshold}%`); await this.executeTrade(analysis); } else { await this.log(`⏸️ No trade: confidence ${analysis.confidence}% < ${this.config.autoExecuteThreshold}%`); } } else { await this.log('❌ No analysis data received'); } } catch (error) { await this.log(`❌ Cycle error: ${error.message}`); } } async executeTrade(analysis) { try { const tradeData = { symbol: this.config.symbol, side: analysis.recommendation, amount: 100, entry: analysis.entry, stopLoss: analysis.stopLoss, takeProfit: analysis.takeProfit, confidence: analysis.confidence, reasoning: analysis.reasoning, source: 'simple_24x7' }; // Use curl to create paper trade const curlData = JSON.stringify(tradeData).replace(/'/g, "'\\''"); const { stdout } = await execAsync(`curl -s -X POST http://localhost:9001/api/safe-paper-trading/create-trade -H "Content-Type: application/json" -d '${curlData}'`); const result = JSON.parse(stdout); if (result.success) { this.stats.totalTrades++; await this.log(`βœ… Paper trade created: ${result.trade.id}`); } else { await this.log(`❌ Trade failed: ${result.message}`); } } catch (error) { await this.log(`❌ Trade execution error: ${error.message}`); } } getStatus() { const uptime = Math.floor((Date.now() - this.stats.startTime.getTime()) / 1000); return { isRunning: this.isRunning, config: this.config, stats: { ...this.stats, uptime: `${Math.floor(uptime / 3600)}h ${Math.floor((uptime % 3600) / 60)}m`, nextCycle: new Date(Date.now() + (this.config.intervalMinutes * 60 * 1000)) } }; } } // Start automation const automation = new SimpleAutomation(); // Handle graceful shutdown process.on('SIGTERM', async () => { await automation.log('πŸ›‘ Received SIGTERM, shutting down...'); process.exit(0); }); process.on('SIGINT', async () => { await automation.log('πŸ›‘ Received SIGINT, shutting down...'); process.exit(0); }); // Start the automation automation.start().catch(async (error) => { await automation.log(`❌ Startup error: ${error.message}`); process.exit(1); }); // Keep process alive console.log('πŸ€– Simple 24/7 Automation Service'); console.log('βœ… Running in background - press Ctrl+C to stop'); console.log('πŸ“ Logs: /tmp/automation-simple.log'); // Prevent the process from exiting process.stdin.resume();