'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', timeframe: '1h', // Primary timeframe for backwards compatibility selectedTimeframes: ['60'], // Multi-timeframe support tradingAmount: 100, maxLeverage: 5, stopLossPercent: 2, takeProfitPercent: 6, riskPercentage: 2 }) 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() if (data.success) { setStatus(data.status) } } 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 () => { setLoading(true) try { const response = await fetch('/api/automation/start', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(config) }) const data = await response.json() if (data.success) { fetchStatus() } else { alert('Failed to start automation: ' + data.error) } } catch (error) { console.error('Failed to start automation:', error) alert('Failed to start automation') } finally { setLoading(false) } } const handleStop = async () => { setLoading(true) try { const response = await fetch('/api/automation/stop', { method: 'POST' }) const data = await response.json() if (data.success) { fetchStatus() } else { alert('Failed to stop automation: ' + data.error) } } catch (error) { console.error('Failed to stop automation:', error) alert('Failed to stop automation') } finally { setLoading(false) } } return (
🚀 NEW AUTOMATION V2 - MULTI-TIMEFRAME READY 🚀

Automated Trading V2

Drift Protocol - Multi-Timeframe Analysis

{status?.isActive ? ( ) : ( )}
{/* Configuration Panel */}

Configuration

{/* Trading Mode */}
{/* Symbol and Position Size */}
setConfig({...config, tradingAmount: parseFloat(e.target.value)})} disabled={status?.isActive} /> {balance && (

Available: ${parseFloat(balance.availableBalance).toFixed(2)} • Using {((config.tradingAmount / balance.availableBalance) * 100).toFixed(1)}% of balance

)} {balance && config.maxLeverage > 1 && (

With {config.maxLeverage}x leverage: ${(config.tradingAmount * config.maxLeverage).toFixed(2)} position size

)}
{balance && (

Quick calculation based on ${parseFloat(balance.availableBalance).toFixed(2)} balance

)}
{/* 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).filter(Boolean).join(', ')}
💡 Multiple timeframes provide more robust analysis
)} {/* Quick Selection Buttons */}
{/* Risk Management */}
setConfig({...config, stopLossPercent: parseFloat(e.target.value)})} disabled={status?.isActive} />
setConfig({...config, takeProfitPercent: parseFloat(e.target.value)})} disabled={status?.isActive} />
setConfig({...config, riskPercentage: parseFloat(e.target.value)})} disabled={status?.isActive} />
{/* Status Panels */}
{/* Account Status */}

Account Status

{balance ? (
Available Balance: ${parseFloat(balance.availableBalance).toFixed(2)}
Account Value: ${parseFloat(balance.accountValue || balance.availableBalance).toFixed(2)}
Unrealized P&L: 0 ? 'text-green-400' : balance.unrealizedPnl < 0 ? 'text-red-400' : 'text-gray-400'}`}> ${parseFloat(balance.unrealizedPnl || 0).toFixed(2)}
Open Positions: {positions.length}
) : (
Loading account data...
)}
{/* Bot Status */}

Bot Status

{status ? (
Status: {status.isActive ? 'ACTIVE' : 'STOPPED'}
Mode: {status.mode}
Protocol: DRIFT
Symbol: {status.symbol}
Leverage: {config.maxLeverage}x
) : (

Loading bot status...

)}
{/* Trading Metrics */}

Trading Metrics

${balance ? parseFloat(balance.accountValue || balance.availableBalance).toFixed(2) : '0.00'}
Portfolio
{balance ? parseFloat(balance.leverage || 0).toFixed(1) : '0.0'}%
Leverage Used
${balance ? parseFloat(balance.unrealizedPnl || 0).toFixed(2) : '0.00'}
Unrealized P&L
{positions.length}
Open Positions
) }