feat: implement complete automation system with real trading connection
This commit is contained in:
20
app/api/automation/learning-insights/route.js
Normal file
20
app/api/automation/learning-insights/route.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const insights = await automationService.getLearningInsights('default-user')
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
insights
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Get learning insights error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
21
app/api/automation/pause/route.js
Normal file
21
app/api/automation/pause/route.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const success = await automationService.pauseAutomation()
|
||||
|
||||
if (success) {
|
||||
return NextResponse.json({ success: true, message: 'Automation paused successfully' })
|
||||
} else {
|
||||
return NextResponse.json({ success: false, error: 'Failed to pause automation' }, { status: 500 })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Pause automation error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
31
app/api/automation/recent-trades/route.js
Normal file
31
app/api/automation/recent-trades/route.js
Normal file
@@ -0,0 +1,31 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const trades = await prisma.trade.findMany({
|
||||
where: {
|
||||
userId: 'default-user',
|
||||
isAutomated: true
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
},
|
||||
take: 10
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
trades
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Get recent trades error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
21
app/api/automation/resume/route.js
Normal file
21
app/api/automation/resume/route.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const success = await automationService.resumeAutomation()
|
||||
|
||||
if (success) {
|
||||
return NextResponse.json({ success: true, message: 'Automation resumed successfully' })
|
||||
} else {
|
||||
return NextResponse.json({ success: false, error: 'Failed to resume automation' }, { status: 500 })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Resume automation error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
29
app/api/automation/start/route.js
Normal file
29
app/api/automation/start/route.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function POST(request) {
|
||||
try {
|
||||
const config = await request.json()
|
||||
|
||||
// Add a default userId for now (in production, get from auth)
|
||||
const automationConfig = {
|
||||
userId: 'default-user',
|
||||
...config
|
||||
}
|
||||
|
||||
const success = await automationService.startAutomation(automationConfig)
|
||||
|
||||
if (success) {
|
||||
return NextResponse.json({ success: true, message: 'Automation started successfully' })
|
||||
} else {
|
||||
return NextResponse.json({ success: false, error: 'Failed to start automation' }, { status: 500 })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Start automation error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
20
app/api/automation/status/route.js
Normal file
20
app/api/automation/status/route.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const status = await automationService.getStatus()
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
status: status || null
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Get status error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
21
app/api/automation/stop/route.js
Normal file
21
app/api/automation/stop/route.js
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import { automationService } from '@/lib/automation-service-simple'
|
||||
|
||||
export async function POST() {
|
||||
try {
|
||||
const success = await automationService.stopAutomation()
|
||||
|
||||
if (success) {
|
||||
return NextResponse.json({ success: true, message: 'Automation stopped successfully' })
|
||||
} else {
|
||||
return NextResponse.json({ success: false, error: 'Failed to stop automation' }, { status: 500 })
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Stop automation error:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: 'Internal server error',
|
||||
message: error.message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
64
app/api/automation/test/route.ts
Normal file
64
app/api/automation/test/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { automationService } from '../../../../lib/automation-service-simple'
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
console.log('🧪 Testing Automation Service Connection...')
|
||||
|
||||
// Test configuration
|
||||
const testConfig = {
|
||||
userId: 'test-user-123',
|
||||
mode: 'SIMULATION' as const,
|
||||
symbol: 'SOLUSD',
|
||||
timeframe: '1h',
|
||||
tradingAmount: 10, // $10 for simulation
|
||||
maxLeverage: 2,
|
||||
stopLossPercent: 2,
|
||||
takeProfitPercent: 6,
|
||||
maxDailyTrades: 5,
|
||||
riskPercentage: 1
|
||||
}
|
||||
|
||||
console.log('📋 Config:', testConfig)
|
||||
|
||||
// Test starting automation
|
||||
console.log('\n🚀 Starting automation...')
|
||||
const startResult = await automationService.startAutomation(testConfig)
|
||||
console.log('✅ Start result:', startResult)
|
||||
|
||||
// Test getting status
|
||||
console.log('\n📊 Getting status...')
|
||||
const status = await automationService.getStatus()
|
||||
console.log('✅ Status:', status)
|
||||
|
||||
// Test getting learning insights
|
||||
console.log('\n🧠 Getting learning insights...')
|
||||
const insights = await automationService.getLearningInsights(testConfig.userId)
|
||||
console.log('✅ Learning insights:', insights)
|
||||
|
||||
// Test stopping
|
||||
console.log('\n🛑 Stopping automation...')
|
||||
const stopResult = await automationService.stopAutomation()
|
||||
console.log('✅ Stop result:', stopResult)
|
||||
|
||||
console.log('\n🎉 All automation tests passed!')
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Automation service connection test passed!',
|
||||
results: {
|
||||
startResult,
|
||||
status,
|
||||
insights,
|
||||
stopResult
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Test failed:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Unknown error'
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user