#!/usr/bin/env node /** * Working 24/7 Automation - No stdin issues */ const { exec } = require('child_process'); const { promisify } = require('util'); const execAsync = promisify(exec); const fs = require('fs').promises; class WorkingAutomation { constructor() { 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); } async runCycle() { this.stats.totalCycles++; await this.log(`πŸ”„ Analysis cycle #${this.stats.totalCycles}`); try { // Get current analysis 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.recommendation} (${analysis.confidence}% confidence)`); if (analysis.confidence >= this.config.autoExecuteThreshold) { await this.log(`🎯 AUTO-EXECUTING: ${analysis.confidence}% β‰₯ ${this.config.autoExecuteThreshold}%`); 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: 'automation_24x7' }; // Create paper trade const curlData = JSON.stringify(tradeData).replace(/"/g, '\\"'); const { stdout: tradeResult } = 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(tradeResult); if (result.success) { this.stats.totalTrades++; await this.log(`βœ… TRADE CREATED: ${result.trade.id}`); } else { await this.log(`❌ TRADE FAILED: ${result.message}`); } } else { await this.log(`⏸️ NO TRADE: ${analysis.confidence}% < ${this.config.autoExecuteThreshold}%`); } } else { await this.log('❌ No analysis data available'); } } catch (error) { await this.log(`❌ Cycle error: ${error.message}`); } } async start() { await this.log('πŸš€ 24/7 AUTOMATION STARTED'); await this.log(`πŸ“Š ${this.config.symbol} every ${this.config.intervalMinutes}m, threshold β‰₯${this.config.autoExecuteThreshold}%`); // Run first cycle await this.runCycle(); // Schedule recurring cycles setInterval(() => this.runCycle().catch(console.error), this.config.intervalMinutes * 60 * 1000); await this.log(`⏰ Next cycle: ${new Date(Date.now() + this.config.intervalMinutes * 60 * 1000).toLocaleTimeString()}`); } } // Create and start automation const automation = new WorkingAutomation(); automation.start();