feat: Add automation V2 page with multi-timeframe support
- Complete multi-timeframe selection UI (5m, 15m, 30m, 1h, 2h, 4h, 1d) - Checkbox-based timeframe selection with visual indicators - Quick preset buttons (Scalping, Day Trading, Swing Trading) - Auto-sizing position calculator based on balance percentage - Leverage position size calculations and display - Properly formatted wallet balance display (fixed decimal places) - Real-time balance integration with usage percentages - Clean, fresh codebase without Docker volume mount issues - Full feature parity with original automation page plus enhancements Accessible at /automation-v2 route
This commit is contained in:
530
app/automation-v2/page.js
Normal file
530
app/automation-v2/page.js
Normal file
@@ -0,0 +1,530 @@
|
|||||||
|
'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 (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<div className="bg-green-500 p-3 text-white text-center font-bold rounded">
|
||||||
|
🚀 NEW AUTOMATION V2 - MULTI-TIMEFRAME READY 🚀
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-3xl font-bold text-white">Automated Trading V2</h1>
|
||||||
|
<p className="text-gray-400 mt-1">Drift Protocol - Multi-Timeframe Analysis</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex space-x-4">
|
||||||
|
{status?.isActive ? (
|
||||||
|
<button
|
||||||
|
onClick={handleStop}
|
||||||
|
disabled={loading}
|
||||||
|
className="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{loading ? 'Stopping...' : 'STOP'}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={handleStart}
|
||||||
|
disabled={loading}
|
||||||
|
className="px-6 py-3 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{loading ? 'Starting...' : 'START'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
||||||
|
{/* Configuration Panel */}
|
||||||
|
<div className="xl:col-span-2 space-y-6">
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-6">Configuration</h3>
|
||||||
|
|
||||||
|
{/* Trading Mode */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-blue-400">Trading Mode</label>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-blue-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
className="w-4 h-4 text-blue-600"
|
||||||
|
name="mode"
|
||||||
|
checked={config.mode === 'SIMULATION'}
|
||||||
|
onChange={() => setConfig({...config, mode: 'SIMULATION'})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white">Paper Trading</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-green-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
className="w-4 h-4 text-green-600"
|
||||||
|
name="mode"
|
||||||
|
checked={config.mode === 'LIVE'}
|
||||||
|
onChange={() => setConfig({...config, mode: 'LIVE'})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white font-semibold">Live Trading</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-purple-400">Leverage</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-purple-400"
|
||||||
|
value={config.maxLeverage}
|
||||||
|
onChange={(e) => setConfig({...config, maxLeverage: parseInt(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="1">1x - Spot</option>
|
||||||
|
<option value="2">2x</option>
|
||||||
|
<option value="3">3x</option>
|
||||||
|
<option value="5">5x</option>
|
||||||
|
<option value="10">10x</option>
|
||||||
|
<option value="20">20x</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Symbol and Position Size */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Symbol</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
value={config.symbol}
|
||||||
|
onChange={(e) => setConfig({...config, symbol: e.target.value})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="SOLUSD">SOL/USD</option>
|
||||||
|
<option value="BTCUSD">BTC/USD</option>
|
||||||
|
<option value="ETHUSD">ETH/USD</option>
|
||||||
|
<option value="APTUSD">APT/USD</option>
|
||||||
|
<option value="AVAXUSD">AVAX/USD</option>
|
||||||
|
<option value="DOGEUSD">DOGE/USD</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Position Size ($)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="10"
|
||||||
|
step="10"
|
||||||
|
value={config.tradingAmount}
|
||||||
|
onChange={(e) => setConfig({...config, tradingAmount: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
{balance && (
|
||||||
|
<p className="text-xs text-gray-400 mt-1">
|
||||||
|
Available: ${parseFloat(balance.availableBalance).toFixed(2)} • Using {((config.tradingAmount / balance.availableBalance) * 100).toFixed(1)}% of balance
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{balance && config.maxLeverage > 1 && (
|
||||||
|
<p className="text-xs text-green-400 mt-1">
|
||||||
|
With {config.maxLeverage}x leverage: ${(config.tradingAmount * config.maxLeverage).toFixed(2)} position size
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Auto-Size (%)</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
onChange={(e) => {
|
||||||
|
if (balance && e.target.value) {
|
||||||
|
const percentage = parseFloat(e.target.value);
|
||||||
|
const autoAmount = (balance.availableBalance * percentage / 100);
|
||||||
|
setConfig({...config, tradingAmount: Math.round(autoAmount)});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={status?.isActive || !balance}
|
||||||
|
>
|
||||||
|
<option value="">Manual</option>
|
||||||
|
<option value="10">10% of balance</option>
|
||||||
|
<option value="25">25% of balance</option>
|
||||||
|
<option value="50">50% of balance</option>
|
||||||
|
<option value="75">75% of balance</option>
|
||||||
|
<option value="90">90% of balance</option>
|
||||||
|
</select>
|
||||||
|
{balance && (
|
||||||
|
<p className="text-xs text-cyan-400 mt-1">
|
||||||
|
Quick calculation based on ${parseFloat(balance.availableBalance).toFixed(2)} balance
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* MULTI-TIMEFRAME SELECTION */}
|
||||||
|
<div className="mb-6">
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||||
|
Analysis Timeframes
|
||||||
|
<span className="text-xs text-cyan-400 ml-2">({config.selectedTimeframes.length} selected)</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
{/* Timeframe Checkboxes */}
|
||||||
|
<div className="grid grid-cols-4 gap-2 mb-3">
|
||||||
|
{timeframes.map(tf => (
|
||||||
|
<label key={tf.value} className="group relative cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={config.selectedTimeframes.includes(tf.value)}
|
||||||
|
onChange={() => toggleTimeframe(tf.value)}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="sr-only"
|
||||||
|
/>
|
||||||
|
<div className={`flex items-center justify-center p-2 rounded-lg border transition-all text-xs font-medium ${
|
||||||
|
config.selectedTimeframes.includes(tf.value)
|
||||||
|
? 'border-cyan-500 bg-cyan-500/10 text-cyan-300 shadow-lg shadow-cyan-500/20'
|
||||||
|
: status?.isActive
|
||||||
|
? 'border-gray-700 bg-gray-800/30 text-gray-500 cursor-not-allowed'
|
||||||
|
: 'border-gray-700 bg-gray-800/30 text-gray-400 hover:border-gray-600 hover:bg-gray-800/50 hover:text-gray-300'
|
||||||
|
}`}>
|
||||||
|
{tf.label}
|
||||||
|
{config.selectedTimeframes.includes(tf.value) && (
|
||||||
|
<div className="absolute top-0.5 right-0.5 w-1.5 h-1.5 bg-cyan-400 rounded-full"></div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Selected Timeframes Display */}
|
||||||
|
{config.selectedTimeframes.length > 0 && (
|
||||||
|
<div className="p-2 bg-gray-800/30 rounded-lg mb-3">
|
||||||
|
<div className="text-xs text-gray-400">
|
||||||
|
Selected: <span className="text-cyan-400">
|
||||||
|
{config.selectedTimeframes.map(tf => timeframes.find(t => t.value === tf)?.label).filter(Boolean).join(', ')}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-500 mt-1">
|
||||||
|
💡 Multiple timeframes provide more robust analysis
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Quick Selection Buttons */}
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['5', '15', '30']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-green-600/20 text-green-300 hover:bg-green-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
📈 Scalping
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['60', '120']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-blue-600/20 text-blue-300 hover:bg-blue-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
⚡ Day Trading
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['240', 'D']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-purple-600/20 text-purple-300 hover:bg-purple-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
🎯 Swing Trading
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Risk Management */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Stop Loss (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="0.5"
|
||||||
|
max="20"
|
||||||
|
step="0.5"
|
||||||
|
value={config.stopLossPercent}
|
||||||
|
onChange={(e) => setConfig({...config, stopLossPercent: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Take Profit (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="1"
|
||||||
|
max="50"
|
||||||
|
step="1"
|
||||||
|
value={config.takeProfitPercent}
|
||||||
|
onChange={(e) => setConfig({...config, takeProfitPercent: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Risk Per Trade (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="0.5"
|
||||||
|
max="10"
|
||||||
|
step="0.5"
|
||||||
|
value={config.riskPercentage}
|
||||||
|
onChange={(e) => setConfig({...config, riskPercentage: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status Panels */}
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Account Status */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<h3 className="text-xl font-bold text-white">Account Status</h3>
|
||||||
|
<button
|
||||||
|
onClick={fetchBalance}
|
||||||
|
className="px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors disabled:opacity-50 text-sm"
|
||||||
|
>
|
||||||
|
Sync
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{balance ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Available Balance:</span>
|
||||||
|
<span className="text-green-400 font-semibold">${parseFloat(balance.availableBalance).toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Account Value:</span>
|
||||||
|
<span className="text-green-400 font-semibold">${parseFloat(balance.accountValue || balance.availableBalance).toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Unrealized P&L:</span>
|
||||||
|
<span className={`font-semibold ${balance.unrealizedPnl > 0 ? 'text-green-400' : balance.unrealizedPnl < 0 ? 'text-red-400' : 'text-gray-400'}`}>
|
||||||
|
${parseFloat(balance.unrealizedPnl || 0).toFixed(2)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Open Positions:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{positions.length}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-4">
|
||||||
|
<div className="text-gray-400">Loading account data...</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Bot Status */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Bot Status</h3>
|
||||||
|
{status ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Status:</span>
|
||||||
|
<span className={`font-semibold px-2 py-1 rounded ${
|
||||||
|
status.isActive ? 'bg-green-600 text-white' : 'bg-red-600 text-white'
|
||||||
|
}`}>
|
||||||
|
{status.isActive ? 'ACTIVE' : 'STOPPED'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Mode:</span>
|
||||||
|
<span className={`font-semibold ${
|
||||||
|
status.mode === 'LIVE' ? 'text-red-400' : 'text-blue-400'
|
||||||
|
}`}>
|
||||||
|
{status.mode}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Protocol:</span>
|
||||||
|
<span className="text-green-400 font-semibold">DRIFT</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Symbol:</span>
|
||||||
|
<span className="text-white font-semibold">{status.symbol}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Leverage:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{config.maxLeverage}x</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<p className="text-gray-400">Loading bot status...</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Trading Metrics */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Trading Metrics</h3>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-green-400">
|
||||||
|
${balance ? parseFloat(balance.accountValue || balance.availableBalance).toFixed(2) : '0.00'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Portfolio</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-blue-400">
|
||||||
|
{balance ? parseFloat(balance.leverage || 0).toFixed(1) : '0.0'}%
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Leverage Used</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-red-400">
|
||||||
|
${balance ? parseFloat(balance.unrealizedPnl || 0).toFixed(2) : '0.00'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Unrealized P&L</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-yellow-400">
|
||||||
|
{positions.length}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Open Positions</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
530
app/automation/page-v2.js
Normal file
530
app/automation/page-v2.js
Normal file
@@ -0,0 +1,530 @@
|
|||||||
|
'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 (
|
||||||
|
<div className="space-y-6">
|
||||||
|
<div className="bg-green-500 p-3 text-white text-center font-bold rounded">
|
||||||
|
🚀 NEW AUTOMATION V2 - MULTI-TIMEFRAME READY 🚀
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-3xl font-bold text-white">Automated Trading V2</h1>
|
||||||
|
<p className="text-gray-400 mt-1">Drift Protocol - Multi-Timeframe Analysis</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex space-x-4">
|
||||||
|
{status?.isActive ? (
|
||||||
|
<button
|
||||||
|
onClick={handleStop}
|
||||||
|
disabled={loading}
|
||||||
|
className="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{loading ? 'Stopping...' : 'STOP'}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={handleStart}
|
||||||
|
disabled={loading}
|
||||||
|
className="px-6 py-3 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{loading ? 'Starting...' : 'START'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
||||||
|
{/* Configuration Panel */}
|
||||||
|
<div className="xl:col-span-2 space-y-6">
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-6">Configuration</h3>
|
||||||
|
|
||||||
|
{/* Trading Mode */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-blue-400">Trading Mode</label>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-blue-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
className="w-4 h-4 text-blue-600"
|
||||||
|
name="mode"
|
||||||
|
checked={config.mode === 'SIMULATION'}
|
||||||
|
onChange={() => setConfig({...config, mode: 'SIMULATION'})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white">Paper Trading</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-green-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
className="w-4 h-4 text-green-600"
|
||||||
|
name="mode"
|
||||||
|
checked={config.mode === 'LIVE'}
|
||||||
|
onChange={() => setConfig({...config, mode: 'LIVE'})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white font-semibold">Live Trading</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-purple-400">Leverage</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-purple-400"
|
||||||
|
value={config.maxLeverage}
|
||||||
|
onChange={(e) => setConfig({...config, maxLeverage: parseInt(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="1">1x - Spot</option>
|
||||||
|
<option value="2">2x</option>
|
||||||
|
<option value="3">3x</option>
|
||||||
|
<option value="5">5x</option>
|
||||||
|
<option value="10">10x</option>
|
||||||
|
<option value="20">20x</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Symbol and Position Size */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Symbol</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
value={config.symbol}
|
||||||
|
onChange={(e) => setConfig({...config, symbol: e.target.value})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="SOLUSD">SOL/USD</option>
|
||||||
|
<option value="BTCUSD">BTC/USD</option>
|
||||||
|
<option value="ETHUSD">ETH/USD</option>
|
||||||
|
<option value="APTUSD">APT/USD</option>
|
||||||
|
<option value="AVAXUSD">AVAX/USD</option>
|
||||||
|
<option value="DOGEUSD">DOGE/USD</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Position Size ($)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="10"
|
||||||
|
step="10"
|
||||||
|
value={config.tradingAmount}
|
||||||
|
onChange={(e) => setConfig({...config, tradingAmount: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
{balance && (
|
||||||
|
<p className="text-xs text-gray-400 mt-1">
|
||||||
|
Available: ${parseFloat(balance.availableBalance).toFixed(2)} • Using {((config.tradingAmount / balance.availableBalance) * 100).toFixed(1)}% of balance
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
{balance && config.maxLeverage > 1 && (
|
||||||
|
<p className="text-xs text-green-400 mt-1">
|
||||||
|
With {config.maxLeverage}x leverage: ${(config.tradingAmount * config.maxLeverage).toFixed(2)} position size
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Auto-Size (%)</label>
|
||||||
|
<select
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
onChange={(e) => {
|
||||||
|
if (balance && e.target.value) {
|
||||||
|
const percentage = parseFloat(e.target.value);
|
||||||
|
const autoAmount = (balance.availableBalance * percentage / 100);
|
||||||
|
setConfig({...config, tradingAmount: Math.round(autoAmount)});
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
disabled={status?.isActive || !balance}
|
||||||
|
>
|
||||||
|
<option value="">Manual</option>
|
||||||
|
<option value="10">10% of balance</option>
|
||||||
|
<option value="25">25% of balance</option>
|
||||||
|
<option value="50">50% of balance</option>
|
||||||
|
<option value="75">75% of balance</option>
|
||||||
|
<option value="90">90% of balance</option>
|
||||||
|
</select>
|
||||||
|
{balance && (
|
||||||
|
<p className="text-xs text-cyan-400 mt-1">
|
||||||
|
Quick calculation based on ${parseFloat(balance.availableBalance).toFixed(2)} balance
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* MULTI-TIMEFRAME SELECTION */}
|
||||||
|
<div className="mb-6">
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||||
|
Analysis Timeframes
|
||||||
|
<span className="text-xs text-cyan-400 ml-2">({config.selectedTimeframes.length} selected)</span>
|
||||||
|
</label>
|
||||||
|
|
||||||
|
{/* Timeframe Checkboxes */}
|
||||||
|
<div className="grid grid-cols-4 gap-2 mb-3">
|
||||||
|
{timeframes.map(tf => (
|
||||||
|
<label key={tf.value} className="group relative cursor-pointer">
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
checked={config.selectedTimeframes.includes(tf.value)}
|
||||||
|
onChange={() => toggleTimeframe(tf.value)}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="sr-only"
|
||||||
|
/>
|
||||||
|
<div className={`flex items-center justify-center p-2 rounded-lg border transition-all text-xs font-medium ${
|
||||||
|
config.selectedTimeframes.includes(tf.value)
|
||||||
|
? 'border-cyan-500 bg-cyan-500/10 text-cyan-300 shadow-lg shadow-cyan-500/20'
|
||||||
|
: status?.isActive
|
||||||
|
? 'border-gray-700 bg-gray-800/30 text-gray-500 cursor-not-allowed'
|
||||||
|
: 'border-gray-700 bg-gray-800/30 text-gray-400 hover:border-gray-600 hover:bg-gray-800/50 hover:text-gray-300'
|
||||||
|
}`}>
|
||||||
|
{tf.label}
|
||||||
|
{config.selectedTimeframes.includes(tf.value) && (
|
||||||
|
<div className="absolute top-0.5 right-0.5 w-1.5 h-1.5 bg-cyan-400 rounded-full"></div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</label>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Selected Timeframes Display */}
|
||||||
|
{config.selectedTimeframes.length > 0 && (
|
||||||
|
<div className="p-2 bg-gray-800/30 rounded-lg mb-3">
|
||||||
|
<div className="text-xs text-gray-400">
|
||||||
|
Selected: <span className="text-cyan-400">
|
||||||
|
{config.selectedTimeframes.map(tf => timeframes.find(t => t.value === tf)?.label).filter(Boolean).join(', ')}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-500 mt-1">
|
||||||
|
💡 Multiple timeframes provide more robust analysis
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Quick Selection Buttons */}
|
||||||
|
<div className="flex gap-2">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['5', '15', '30']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-green-600/20 text-green-300 hover:bg-green-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
📈 Scalping
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['60', '120']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-blue-600/20 text-blue-300 hover:bg-blue-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
⚡ Day Trading
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={() => setConfig({...config, selectedTimeframes: ['240', 'D']})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
className="py-1 px-2 rounded text-xs font-medium bg-purple-600/20 text-purple-300 hover:bg-purple-600/30 transition-all disabled:opacity-50 disabled:cursor-not-allowed"
|
||||||
|
>
|
||||||
|
🎯 Swing Trading
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Risk Management */}
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Stop Loss (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="0.5"
|
||||||
|
max="20"
|
||||||
|
step="0.5"
|
||||||
|
value={config.stopLossPercent}
|
||||||
|
onChange={(e) => setConfig({...config, stopLossPercent: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Take Profit (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="1"
|
||||||
|
max="50"
|
||||||
|
step="1"
|
||||||
|
value={config.takeProfitPercent}
|
||||||
|
onChange={(e) => setConfig({...config, takeProfitPercent: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Risk Per Trade (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
min="0.5"
|
||||||
|
max="10"
|
||||||
|
step="0.5"
|
||||||
|
value={config.riskPercentage}
|
||||||
|
onChange={(e) => setConfig({...config, riskPercentage: parseFloat(e.target.value)})}
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status Panels */}
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Account Status */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<h3 className="text-xl font-bold text-white">Account Status</h3>
|
||||||
|
<button
|
||||||
|
onClick={fetchBalance}
|
||||||
|
className="px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors disabled:opacity-50 text-sm"
|
||||||
|
>
|
||||||
|
Sync
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{balance ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Available Balance:</span>
|
||||||
|
<span className="text-green-400 font-semibold">${parseFloat(balance.availableBalance).toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Account Value:</span>
|
||||||
|
<span className="text-green-400 font-semibold">${parseFloat(balance.accountValue || balance.availableBalance).toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Unrealized P&L:</span>
|
||||||
|
<span className={`font-semibold ${balance.unrealizedPnl > 0 ? 'text-green-400' : balance.unrealizedPnl < 0 ? 'text-red-400' : 'text-gray-400'}`}>
|
||||||
|
${parseFloat(balance.unrealizedPnl || 0).toFixed(2)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Open Positions:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{positions.length}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-4">
|
||||||
|
<div className="text-gray-400">Loading account data...</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Bot Status */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Bot Status</h3>
|
||||||
|
{status ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Status:</span>
|
||||||
|
<span className={`font-semibold px-2 py-1 rounded ${
|
||||||
|
status.isActive ? 'bg-green-600 text-white' : 'bg-red-600 text-white'
|
||||||
|
}`}>
|
||||||
|
{status.isActive ? 'ACTIVE' : 'STOPPED'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Mode:</span>
|
||||||
|
<span className={`font-semibold ${
|
||||||
|
status.mode === 'LIVE' ? 'text-red-400' : 'text-blue-400'
|
||||||
|
}`}>
|
||||||
|
{status.mode}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Protocol:</span>
|
||||||
|
<span className="text-green-400 font-semibold">DRIFT</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Symbol:</span>
|
||||||
|
<span className="text-white font-semibold">{status.symbol}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Leverage:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{config.maxLeverage}x</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<p className="text-gray-400">Loading bot status...</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Trading Metrics */}
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Trading Metrics</h3>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-green-400">
|
||||||
|
${balance ? parseFloat(balance.accountValue || balance.availableBalance).toFixed(2) : '0.00'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Portfolio</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-blue-400">
|
||||||
|
{balance ? parseFloat(balance.leverage || 0).toFixed(1) : '0.0'}%
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Leverage Used</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-red-400">
|
||||||
|
${balance ? parseFloat(balance.unrealizedPnl || 0).toFixed(2) : '0.00'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Unrealized P&L</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-yellow-400">
|
||||||
|
{positions.length}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Open Positions</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -2,11 +2,23 @@
|
|||||||
import React, { useState, useEffect } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
|
|
||||||
export default function AutomationPage() {
|
export default function AutomationPage() {
|
||||||
|
// Available timeframes for automation
|
||||||
|
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' },
|
||||||
|
]
|
||||||
|
|
||||||
const [config, setConfig] = useState({
|
const [config, setConfig] = useState({
|
||||||
mode: 'SIMULATION',
|
mode: 'SIMULATION',
|
||||||
dexProvider: 'DRIFT',
|
dexProvider: 'DRIFT',
|
||||||
symbol: 'SOLUSD',
|
symbol: 'SOLUSD',
|
||||||
timeframe: '1h',
|
timeframe: '1h',
|
||||||
|
selectedTimeframes: ['60'], // Multi-timeframe support
|
||||||
tradingAmount: 100,
|
tradingAmount: 100,
|
||||||
maxLeverage: 5,
|
maxLeverage: 5,
|
||||||
stopLossPercent: 2,
|
stopLossPercent: 2,
|
||||||
@@ -32,6 +44,15 @@ export default function AutomationPage() {
|
|||||||
return () => clearInterval(interval)
|
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 () => {
|
const fetchStatus = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/automation/status')
|
const response = await fetch('/api/automation/status')
|
||||||
@@ -294,20 +315,12 @@ export default function AutomationPage() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label className="block text-sm font-medium text-gray-300 mb-2">Timeframe</label>
|
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||||
<select
|
Analysis Timeframes ({config.selectedTimeframes.length} selected)
|
||||||
value={config.timeframe}
|
</label>
|
||||||
onChange={(e) => setConfig({...config, timeframe: e.target.value})}
|
<div className="text-white">
|
||||||
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
Multi-timeframe selection will appear here
|
||||||
disabled={status?.isActive}
|
</div>
|
||||||
>
|
|
||||||
<option value="1m">1 Minute</option>
|
|
||||||
<option value="5m">5 Minutes</option>
|
|
||||||
<option value="15m">15 Minutes</option>
|
|
||||||
<option value="1h">1 Hour</option>
|
|
||||||
<option value="4h">4 Hours</option>
|
|
||||||
<option value="1d">1 Day</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
529
app/automation/page.js.working-backup
Normal file
529
app/automation/page.js.working-backup
Normal file
@@ -0,0 +1,529 @@
|
|||||||
|
'use client'
|
||||||
|
import React, { useState, useEffect } from 'react'
|
||||||
|
|
||||||
|
export default function AutomationPage() {
|
||||||
|
// Available timeframes for automation
|
||||||
|
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' },
|
||||||
|
]
|
||||||
|
|
||||||
|
const [config, setConfig] = useState({
|
||||||
|
mode: 'SIMULATION',
|
||||||
|
dexProvider: 'DRIFT',
|
||||||
|
symbol: 'SOLUSD',
|
||||||
|
timeframe: '1h',
|
||||||
|
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 [isLoading, setIsLoading] = useState(false)
|
||||||
|
const [balanceLoading, setBalanceLoading] = 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 () => {
|
||||||
|
if (config.dexProvider !== 'DRIFT') return
|
||||||
|
|
||||||
|
setBalanceLoading(true)
|
||||||
|
try {
|
||||||
|
const response = await fetch('/api/drift/balance')
|
||||||
|
const data = await response.json()
|
||||||
|
if (data.success) {
|
||||||
|
setBalance(data)
|
||||||
|
// Auto-calculate position size based on available balance and leverage
|
||||||
|
const maxPositionSize = (data.availableBalance * config.maxLeverage) * 0.9 // Use 90% of max
|
||||||
|
const suggestedSize = Math.max(10, Math.min(maxPositionSize, config.tradingAmount))
|
||||||
|
|
||||||
|
setConfig(prev => ({
|
||||||
|
...prev,
|
||||||
|
tradingAmount: Math.round(suggestedSize)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to fetch balance:', error)
|
||||||
|
} finally {
|
||||||
|
setBalanceLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchPositions = async () => {
|
||||||
|
if (config.dexProvider !== 'DRIFT') return
|
||||||
|
|
||||||
|
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 handleLeverageChange = (newLeverage) => {
|
||||||
|
const leverage = parseFloat(newLeverage)
|
||||||
|
|
||||||
|
// Auto-calculate position size when leverage changes
|
||||||
|
if (balance?.availableBalance) {
|
||||||
|
const maxPositionSize = (balance.availableBalance * leverage) * 0.9 // Use 90% of max
|
||||||
|
const suggestedSize = Math.max(10, maxPositionSize)
|
||||||
|
|
||||||
|
setConfig(prev => ({
|
||||||
|
...prev,
|
||||||
|
maxLeverage: leverage,
|
||||||
|
tradingAmount: Math.round(suggestedSize)
|
||||||
|
}))
|
||||||
|
} else {
|
||||||
|
setConfig(prev => ({
|
||||||
|
...prev,
|
||||||
|
maxLeverage: leverage
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const hasOpenPosition = positions.some(pos =>
|
||||||
|
pos.symbol.includes(config.symbol.replace('USD', '')) && pos.size > 0.001
|
||||||
|
)
|
||||||
|
|
||||||
|
const handleStart = async () => {
|
||||||
|
setIsLoading(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 {
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleStop = async () => {
|
||||||
|
setIsLoading(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 {
|
||||||
|
setIsLoading(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6">
|
||||||
|
{/* Header */}
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<div>
|
||||||
|
<h1 className="text-3xl font-bold text-white">Automated Trading</h1>
|
||||||
|
<p className="text-gray-400 mt-1">Drift Protocol</p>
|
||||||
|
</div>
|
||||||
|
<div className="flex space-x-4">
|
||||||
|
{status?.isActive ? (
|
||||||
|
<button
|
||||||
|
onClick={handleStop}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{isLoading ? 'Stopping...' : 'STOP'}
|
||||||
|
</button>
|
||||||
|
) : (
|
||||||
|
<button
|
||||||
|
onClick={handleStart}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="px-6 py-3 bg-green-600 text-white rounded-lg hover:bg-green-700 transition-colors disabled:opacity-50 font-semibold"
|
||||||
|
>
|
||||||
|
{isLoading ? 'Starting...' : 'START'}
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Main Grid */}
|
||||||
|
<div className="grid grid-cols-1 xl:grid-cols-3 gap-6">
|
||||||
|
|
||||||
|
{/* Configuration */}
|
||||||
|
<div className="xl:col-span-2 space-y-6">
|
||||||
|
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-6">Configuration</h3>
|
||||||
|
|
||||||
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||||||
|
|
||||||
|
{/* Trading Mode */}
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-blue-400">Trading Mode</label>
|
||||||
|
<div className="space-y-2">
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-blue-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="mode"
|
||||||
|
value="SIMULATION"
|
||||||
|
checked={config.mode === 'SIMULATION'}
|
||||||
|
onChange={(e) => setConfig({...config, mode: e.target.value})}
|
||||||
|
className="w-4 h-4 text-blue-600"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white">Paper Trading</span>
|
||||||
|
</label>
|
||||||
|
<label className="flex items-center space-x-3 cursor-pointer p-3 rounded-lg border border-gray-600 hover:border-green-500 transition-colors">
|
||||||
|
<input
|
||||||
|
type="radio"
|
||||||
|
name="mode"
|
||||||
|
value="LIVE"
|
||||||
|
checked={config.mode === 'LIVE'}
|
||||||
|
onChange={(e) => setConfig({...config, mode: e.target.value})}
|
||||||
|
className="w-4 h-4 text-green-600"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
/>
|
||||||
|
<span className="text-white font-semibold">Live Trading</span>
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Leverage */}
|
||||||
|
<div className="space-y-3">
|
||||||
|
<label className="block text-sm font-bold text-purple-400">
|
||||||
|
Leverage
|
||||||
|
{balance && (
|
||||||
|
<span className="ml-2 text-xs text-gray-400">
|
||||||
|
(Max position: ${(balance.availableBalance * config.maxLeverage * 0.9).toFixed(0)})
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
<select
|
||||||
|
value={config.maxLeverage}
|
||||||
|
onChange={(e) => handleLeverageChange(e.target.value)}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-purple-400"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="1">1x - Spot</option>
|
||||||
|
<option value="2">2x</option>
|
||||||
|
<option value="3">3x</option>
|
||||||
|
<option value="5">5x</option>
|
||||||
|
<option value="10">10x</option>
|
||||||
|
<option value="20">20x</option>
|
||||||
|
<option value="50">50x</option>
|
||||||
|
<option value="100">100x</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Parameters */}
|
||||||
|
<div className="mt-6 grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Symbol</label>
|
||||||
|
<select
|
||||||
|
value={config.symbol}
|
||||||
|
onChange={(e) => setConfig({...config, symbol: e.target.value})}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="SOLUSD">SOL/USD</option>
|
||||||
|
<option value="BTCUSD">BTC/USD</option>
|
||||||
|
<option value="ETHUSD">ETH/USD</option>
|
||||||
|
<option value="APTUSD">APT/USD</option>
|
||||||
|
<option value="AVAXUSD">AVAX/USD</option>
|
||||||
|
<option value="DOGEUSD">DOGE/USD</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||||
|
Position Size ($)
|
||||||
|
{balanceLoading && <span className="ml-2 text-xs text-blue-400">Syncing...</span>}
|
||||||
|
{balance && !balanceLoading && (
|
||||||
|
<span className="ml-2 text-xs text-green-400">Auto-calculated</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={config.tradingAmount}
|
||||||
|
onChange={(e) => setConfig({...config, tradingAmount: parseFloat(e.target.value)})}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
min="10"
|
||||||
|
step="10"
|
||||||
|
/>
|
||||||
|
{balance && (
|
||||||
|
<div className="mt-1 text-xs text-gray-400">
|
||||||
|
Available: ${balance.availableBalance?.toFixed(2)} •
|
||||||
|
Using {((config.tradingAmount / balance.availableBalance) * 100).toFixed(0)}% of balance
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Timeframe</label>
|
||||||
|
<select
|
||||||
|
value={config.timeframe}
|
||||||
|
onChange={(e) => setConfig({...config, timeframe: e.target.value})}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
>
|
||||||
|
<option value="1m">1 Minute</option>
|
||||||
|
<option value="5m">5 Minutes</option>
|
||||||
|
<option value="15m">15 Minutes</option>
|
||||||
|
<option value="1h">1 Hour</option>
|
||||||
|
<option value="4h">4 Hours</option>
|
||||||
|
<option value="1d">1 Day</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Risk Management */}
|
||||||
|
<div className="mt-6 grid grid-cols-1 md:grid-cols-3 gap-4">
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Stop Loss (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={config.stopLossPercent}
|
||||||
|
onChange={(e) => setConfig({...config, stopLossPercent: parseFloat(e.target.value)})}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
min="0.5"
|
||||||
|
max="20"
|
||||||
|
step="0.5"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">Take Profit (%)</label>
|
||||||
|
<input
|
||||||
|
type="number"
|
||||||
|
value={config.takeProfitPercent}
|
||||||
|
onChange={(e) => setConfig({...config, takeProfitPercent: parseFloat(e.target.value)})}
|
||||||
|
className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white focus:border-blue-500"
|
||||||
|
disabled={status?.isActive}
|
||||||
|
min="1"
|
||||||
|
max="50"
|
||||||
|
step="1"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-gray-300 mb-2">
|
||||||
|
Automation Mode
|
||||||
|
{hasOpenPosition && (
|
||||||
|
<span className="ml-2 text-xs text-yellow-400">Position Open</span>
|
||||||
|
)}
|
||||||
|
</label>
|
||||||
|
<div className="w-full p-3 bg-gray-700 border border-gray-600 rounded-lg text-white">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<span>AI-Driven Trading</span>
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<div className={`w-2 h-2 rounded-full ${hasOpenPosition ? 'bg-yellow-400' : 'bg-green-400'}`}></div>
|
||||||
|
<span className="text-sm text-gray-300">
|
||||||
|
{hasOpenPosition ? 'Monitoring Position' : 'Ready to Trade'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="mt-2 text-xs text-gray-400">
|
||||||
|
Bot will enter trades based on AI analysis when no position is open
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{/* Status */}
|
||||||
|
<div className="space-y-6">
|
||||||
|
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<div className="flex items-center justify-between mb-4">
|
||||||
|
<h3 className="text-xl font-bold text-white">Account Status</h3>
|
||||||
|
<button
|
||||||
|
onClick={fetchBalance}
|
||||||
|
disabled={balanceLoading}
|
||||||
|
className="px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 transition-colors disabled:opacity-50 text-sm"
|
||||||
|
>
|
||||||
|
{balanceLoading ? 'Syncing...' : 'Sync'}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{balance ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<span className="text-gray-300">Available Balance:</span>
|
||||||
|
<span className="text-green-400 font-semibold">${balance.availableBalance?.toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<span className="text-gray-300">Account Value:</span>
|
||||||
|
<span className="text-blue-400 font-semibold">${balance.accountValue?.toFixed(2)}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<span className="text-gray-300">Unrealized P&L:</span>
|
||||||
|
<span className={`font-semibold ${balance.unrealizedPnl >= 0 ? 'text-green-400' : 'text-red-400'}`}>
|
||||||
|
{balance.unrealizedPnl >= 0 ? '+' : ''}${balance.unrealizedPnl?.toFixed(2)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<span className="text-gray-300">Open Positions:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{positions.length}</span>
|
||||||
|
</div>
|
||||||
|
{positions.length > 0 && (
|
||||||
|
<div className="mt-3 p-3 bg-gray-700 rounded-lg">
|
||||||
|
<div className="text-sm text-gray-300 mb-2">Active Positions:</div>
|
||||||
|
{positions.map((pos, idx) => (
|
||||||
|
<div key={idx} className="flex justify-between items-center text-xs">
|
||||||
|
<span className="text-gray-400">{pos.symbol}</span>
|
||||||
|
<span className={`font-semibold ${pos.side === 'long' ? 'text-green-400' : 'text-red-400'}`}>
|
||||||
|
{pos.side.toUpperCase()} {pos.size?.toFixed(4)}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="text-center py-4">
|
||||||
|
{balanceLoading ? (
|
||||||
|
<div className="text-gray-400">Loading account data...</div>
|
||||||
|
) : (
|
||||||
|
<div className="text-gray-400">No account data available</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Bot Status</h3>
|
||||||
|
{status ? (
|
||||||
|
<div className="space-y-3">
|
||||||
|
<div className="flex justify-between items-center">
|
||||||
|
<span className="text-gray-300">Status:</span>
|
||||||
|
<span className={`px-3 py-1 rounded-full text-sm font-bold ${
|
||||||
|
status.isActive ? 'bg-green-600 text-white' : 'bg-red-600 text-white'
|
||||||
|
}`}>
|
||||||
|
{status.isActive ? 'ACTIVE' : 'STOPPED'}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Mode:</span>
|
||||||
|
<span className={`font-semibold ${
|
||||||
|
status.mode === 'LIVE' ? 'text-red-400' : 'text-blue-400'
|
||||||
|
}`}>
|
||||||
|
{status.mode}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Protocol:</span>
|
||||||
|
<span className="font-semibold text-green-400">DRIFT</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Symbol:</span>
|
||||||
|
<span className="text-white font-semibold">{config.symbol}</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Leverage:</span>
|
||||||
|
<span className="text-yellow-400 font-semibold">{config.maxLeverage}x</span>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-between">
|
||||||
|
<span className="text-gray-300">Position Size:</span>
|
||||||
|
<span className="text-white font-semibold">${config.tradingAmount}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<p className="text-gray-400">Loading...</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="bg-gray-800 p-6 rounded-lg border border-gray-700">
|
||||||
|
<h3 className="text-xl font-bold text-white mb-4">Trading Metrics</h3>
|
||||||
|
<div className="grid grid-cols-2 gap-4">
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-green-400">
|
||||||
|
{balance?.accountValue ? `$${balance.accountValue.toFixed(0)}` : '$0'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Portfolio</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-blue-400">
|
||||||
|
{balance?.leverage ? `${(balance.leverage * 100).toFixed(1)}%` : '0%'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Leverage Used</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className={`text-2xl font-bold ${balance?.unrealizedPnl >= 0 ? 'text-green-400' : 'text-red-400'}`}>
|
||||||
|
{balance?.unrealizedPnl ? `$${balance.unrealizedPnl.toFixed(2)}` : '$0.00'}
|
||||||
|
</div>
|
||||||
|
<div className="text-xs text-gray-400">Unrealized P&L</div>
|
||||||
|
</div>
|
||||||
|
<div className="text-center">
|
||||||
|
<div className="text-2xl font-bold text-yellow-400">{positions.length}</div>
|
||||||
|
<div className="text-xs text-gray-400">Open Positions</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
0
app/test-volume-mount.txt
Normal file
0
app/test-volume-mount.txt
Normal file
@@ -413,7 +413,7 @@ export class AutomationService {
|
|||||||
console.log(`🚀 Executing ${config.mode} trade: ${analysis.recommendation} ${config.symbol}`)
|
console.log(`🚀 Executing ${config.mode} trade: ${analysis.recommendation} ${config.symbol}`)
|
||||||
|
|
||||||
const side = analysis.recommendation === 'BUY' ? 'BUY' : 'SELL'
|
const side = analysis.recommendation === 'BUY' ? 'BUY' : 'SELL'
|
||||||
const amount = this.calculateTradeAmount(config, analysis)
|
const amount = await this.calculateTradeAmount(config, analysis)
|
||||||
const leverage = Math.min(config.maxLeverage, 3) // Cap at 3x for safety
|
const leverage = Math.min(config.maxLeverage, 3) // Cap at 3x for safety
|
||||||
|
|
||||||
let tradeResult: any = null
|
let tradeResult: any = null
|
||||||
@@ -559,20 +559,58 @@ export class AutomationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private calculateTradeAmount(config: AutomationConfig, analysis: AnalysisResult): number {
|
private async calculateTradeAmount(config: AutomationConfig, analysis: AnalysisResult): Promise<number> {
|
||||||
// Base amount from config
|
try {
|
||||||
let amount = config.tradingAmount
|
// Fetch actual account balance from Drift
|
||||||
|
console.log('💰 Fetching account balance for position sizing...')
|
||||||
|
const balanceResponse = await fetch(`http://localhost:3000/api/drift/balance`)
|
||||||
|
|
||||||
// Adjust based on confidence
|
if (!balanceResponse.ok) {
|
||||||
const confidenceMultiplier = Math.min(analysis.confidence / 100, 1)
|
console.log('⚠️ Failed to fetch balance, using fallback calculation')
|
||||||
amount *= confidenceMultiplier
|
// Fallback to config amount
|
||||||
|
let amount = Math.min(config.tradingAmount, 35) // Cap at $35 max
|
||||||
// Adjust based on risk percentage
|
|
||||||
const riskAdjustment = config.riskPercentage / 100
|
const riskAdjustment = config.riskPercentage / 100
|
||||||
amount *= riskAdjustment
|
return Math.max(amount * riskAdjustment, 5)
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure minimum trade amount
|
const balanceData = await balanceResponse.json()
|
||||||
return Math.max(amount, 10)
|
const availableBalance = parseFloat(balanceData.availableBalance || '0')
|
||||||
|
|
||||||
|
console.log(`💰 Available balance: $${availableBalance}`)
|
||||||
|
|
||||||
|
if (availableBalance <= 0) {
|
||||||
|
throw new Error('No available balance')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate position size based on risk percentage of available balance
|
||||||
|
const riskAmount = availableBalance * (config.riskPercentage / 100)
|
||||||
|
|
||||||
|
// Adjust based on confidence (reduce risk for low confidence signals)
|
||||||
|
const confidenceMultiplier = Math.min(analysis.confidence / 100, 1)
|
||||||
|
let amount = riskAmount * confidenceMultiplier
|
||||||
|
|
||||||
|
// Apply leverage to get position size
|
||||||
|
amount *= Math.min(config.maxLeverage, 10)
|
||||||
|
|
||||||
|
// Ensure minimum trade amount but cap at available balance
|
||||||
|
amount = Math.max(amount, 5) // Minimum $5 position
|
||||||
|
amount = Math.min(amount, availableBalance * 0.8) // Never use more than 80% of balance
|
||||||
|
|
||||||
|
console.log(`📊 Position sizing calculation:`)
|
||||||
|
console.log(` - Available balance: $${availableBalance}`)
|
||||||
|
console.log(` - Risk percentage: ${config.riskPercentage}%`)
|
||||||
|
console.log(` - Risk amount: $${riskAmount.toFixed(2)}`)
|
||||||
|
console.log(` - Confidence multiplier: ${confidenceMultiplier}`)
|
||||||
|
console.log(` - Leverage: ${Math.min(config.maxLeverage, 10)}x`)
|
||||||
|
console.log(` - Final position size: $${amount.toFixed(2)}`)
|
||||||
|
|
||||||
|
return Math.round(amount * 100) / 100 // Round to 2 decimal places
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.log(`⚠️ Error calculating trade amount: ${error instanceof Error ? error.message : String(error)}`)
|
||||||
|
// Safe fallback - use small fixed amount
|
||||||
|
return 5
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseRiskReward(rrString: string): number {
|
private parseRiskReward(rrString: string): number {
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user