'use client' import React, { useState, useEffect } from 'react' // Available timeframes for automation (matching analysis page format) const timeframes = [ { label: '5m', value: '5' }, { label: '15m', value: '15' }, { label: '30m', value: '30' }, { label: '1h', value: '60' }, { label: '2h', value: '120' }, { label: '4h', value: '240' }, { label: '1d', value: 'D' }, ] export default function AutomationPageV2() { const [config, setConfig] = useState({ mode: 'SIMULATION', dexProvider: 'DRIFT', symbol: 'SOLUSD', selectedTimeframes: ['60'], // Multi-timeframe support tradingAmount: 100, balancePercentage: 50, // Default to 50% of available balance }) const [status, setStatus] = useState(null) const [balance, setBalance] = useState(null) const [positions, setPositions] = useState([]) const [loading, setLoading] = useState(false) const [monitorData, setMonitorData] = useState(null) useEffect(() => { fetchStatus() fetchBalance() fetchPositions() fetchMonitorData() fetchMonitorData() const interval = setInterval(() => { fetchStatus() fetchBalance() fetchPositions() fetchMonitorData() fetchMonitorData() }, 30000) return () => clearInterval(interval) }, []) const toggleTimeframe = (timeframe) => { setConfig(prev => ({ ...prev, selectedTimeframes: prev.selectedTimeframes.includes(timeframe) ? prev.selectedTimeframes.filter(tf => tf !== timeframe) : [...prev.selectedTimeframes, timeframe] })) } const fetchStatus = async () => { try { const response = await fetch('/api/automation/status') const data = await response.json() console.log('Status response:', data) // Debug log if (response.ok && !data.error) { setStatus(data) // Status data is returned directly, not wrapped in 'success' } else { console.error('Status API error:', data.error || 'Unknown error') } } catch (error) { console.error('Failed to fetch status:', error) } } const fetchBalance = async () => { try { const response = await fetch('/api/drift/balance') const data = await response.json() if (data.success) { setBalance(data) } } catch (error) { console.error('Failed to fetch balance:', error) } } const fetchMonitorData = async () => { try { const response = await fetch('/api/automation/position-monitor') const data = await response.json() if (data.success) { setMonitorData(data.monitor) } } catch (error) { console.error('Failed to fetch monitor data:', error) } } const fetchPositions = async () => { try { const response = await fetch('/api/drift/positions') const data = await response.json() if (data.success) { setPositions(data.positions || []) } } catch (error) { console.error('Failed to fetch positions:', error) } } const handleStart = async () => { console.log('🚀 Starting automation...') setLoading(true) try { if (config.selectedTimeframes.length === 0) { console.error('No timeframes selected') setLoading(false) return } const automationConfig = { symbol: config.symbol, selectedTimeframes: config.selectedTimeframes, mode: config.mode, tradingAmount: config.tradingAmount, leverage: config.leverage, stopLoss: config.stopLoss, takeProfit: config.takeProfit } const response = await fetch('/api/automation/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(automationConfig) }) const data = await response.json() if (data.success) { console.log('✅ Automation started successfully') fetchStatus() } else { console.error('Failed to start automation:', data.error) } } catch (error) { console.error('Failed to start automation:', error) } finally { setLoading(false) } } const handleStop = async () => { console.log('🛑 Stopping automation...') setLoading(true) try { const response = await fetch('/api/automation/stop', { method: 'POST', headers: { 'Content-Type': 'application/json' } }) const data = await response.json() if (data.success) { console.log('✅ Automation stopped successfully') fetchStatus() } else { console.error('Failed to stop automation:', data.error) } } catch (error) { console.error('Failed to stop automation:', error) } finally { setLoading(false) } } const handleEmergencyStop = async () => { console.log('🚨 Emergency stop triggered!') setLoading(true) try { const response = await fetch('/api/automation/emergency-stop', { method: 'POST', headers: { 'Content-Type': 'application/json' } }) const data = await response.json() if (data.success) { console.log('✅ Emergency stop completed successfully') fetchStatus() fetchPositions() fetchMonitorData() fetchMonitorData() } else { console.error('Emergency stop failed:', data.error) } } catch (error) { console.error('Emergency stop error:', error) } finally { setLoading(false) } } return (
{/* 🤖 Automation Control Panel */}
{/* Header with Start/Stop Button */} {/* Header with Start/Stop Button */}

🤖 Automation Control

{status?.isActive ? ( <> ) : ( )}
{/* Trading Mode - Side by Side Radio Buttons with Logos */}
{/* Symbol and Position Size */}
{ const percentage = parseFloat(e.target.value); const newAmount = balance ? (parseFloat(balance.availableBalance) * percentage / 100) : 100; setConfig({ ...config, balancePercentage: percentage, tradingAmount: Math.round(newAmount) }); }} disabled={status?.isActive} />
10% 50% 100%
{/* MULTI-TIMEFRAME SELECTION */}
{/* Timeframe Checkboxes */}
{timeframes.map(tf => ( ))}
{/* Selected Timeframes Display */} {config.selectedTimeframes.length > 0 && (
Selected: {config.selectedTimeframes.map(tf => timeframes.find(t => t.value === tf)?.label || tf).filter(Boolean).join(', ')}
💡 Multiple timeframes provide more robust analysis
)} {/* Quick Selection Buttons - Made Bigger */}
{/* Status and Info Panel */}
{/* Status */}

🤖 Bot Status

Status: {status?.isActive ? 'RUNNING' : 'STOPPED'}
{status?.isActive && ( <>
Symbol: {status.symbol}
Mode: {status.mode}
Timeframes: {status.timeframes?.map(tf => timeframes.find(t => t.value === tf)?.label || tf).join(', ')}
)} {/* Rate Limit Notification */} {status?.rateLimitHit && (
⚠️ Rate Limit Reached
{status.rateLimitMessage && (

{status.rateLimitMessage}

)}

Automation stopped automatically. Please recharge your OpenAI account to continue.

)}
{/* Position Monitor */} {monitorData && (

📊 Position Monitor

Has Position: {monitorData.hasPosition ? '✅ YES' : '❌ NO'}
Risk Level: {monitorData.riskLevel}
Next Action: {monitorData.nextAction}
{monitorData.orphanedOrderCleanup && (
{monitorData.orphanedOrderCleanup.success ? '✅ Cleanup Success' : '❌ Cleanup Failed'}
{monitorData.orphanedOrderCleanup.message}
)}
)} {/* Balance */} {balance && (

� Account Balance

Available: ${parseFloat(balance.availableBalance).toFixed(2)}
Total: ${parseFloat(balance.totalCollateral).toFixed(2)}
Positions: {balance.positions || 0}
)} {/* Positions */} {positions.length > 0 && (

📈 Open Positions

{positions.map((position, index) => (
{position.symbol} {position.side}
Size: ${position.size}
{position.entryPrice && (
Entry: ${position.entryPrice}
)} {position.markPrice && (
Mark: ${position.markPrice}
)} {position.pnl !== undefined && (
PnL: = 0 ? 'text-green-400' : 'text-red-400' }`}> ${position.pnl >= 0 ? '+' : ''}${position.pnl}
)}
))}
)}
) }