'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) useEffect(() => { fetchStatus() fetchBalance() fetchPositions() const interval = setInterval(() => { fetchStatus() fetchBalance() fetchPositions() }, 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 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) } } return (
Multi-Timeframe Analysis