Files
trading_bot_v3/app/api/automation/trade/route.js
mindesbunister 33690f51fa feat: implement real data paper trading system
- Replace mock data with real market analysis in paper trading
- Safe paper trading API now uses live TradingView screenshots and OpenAI analysis
- Maintain complete isolation from live trading while using real market conditions
- Fix Docker build error in automation trade route (removed unreachable code)
- Add safety redirects to prevent accidental live trading access
- Real data includes: live charts, technical indicators, current market conditions
- Analysis time: 30-180s for genuine market analysis vs 5s for mock data
- All safety blocks maintained for zero trading risk learning environment

Tested and verified:
 Container builds and runs successfully
 Real screenshot capture working (TradingView integration)
 OpenAI analysis processing real market data
 Safety systems prevent any actual trading
 Paper trading provides realistic learning experience
2025-08-02 10:22:36 +02:00

117 lines
3.0 KiB
JavaScript

import { NextResponse } from 'next/server'
export async function POST(request) {
try {
console.log('🔄 Unified trading endpoint called...')
const {
dexProvider,
action,
symbol,
amount,
side,
leverage = 1,
mode = 'SIMULATION'
} = await request.json()
// Validate required parameters
if (!dexProvider) {
return NextResponse.json({
success: false,
error: 'DEX provider not specified'
}, { status: 400 })
}
console.log(`📊 Trading request:`, {
dexProvider,
action,
symbol,
amount,
side,
leverage,
mode
})
// Route to appropriate DEX based on provider
let response
if (dexProvider === 'DRIFT') {
console.log('🌊 Routing to Drift Protocol...')
// Call Drift API with correct action for trading
const driftResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/drift/trade`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
action: 'place_order', // This was missing! Was defaulting to 'get_balance'
symbol: symbol.replace('USD', ''), // Convert SOLUSD to SOL
amount,
side,
leverage,
// Add stop loss and take profit parameters
stopLoss: true,
takeProfit: true,
riskPercent: 2 // 2% risk per trade
})
})
response = await driftResponse.json()
if (response.success) {
response.dexProvider = 'DRIFT'
response.leverageUsed = leverage
}
} else if (dexProvider === 'JUPITER') {
console.log('🪐 Routing to Jupiter DEX...')
// Call Jupiter API (you may need to implement this endpoint)
const jupiterResponse = await fetch(`${process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000'}/api/jupiter/trade`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
action,
symbol,
amount,
side
})
})
if (jupiterResponse.ok) {
response = await jupiterResponse.json()
response.dexProvider = 'JUPITER'
response.leverageUsed = 1 // Jupiter is spot only
} else {
response = {
success: false,
error: 'Jupiter DEX integration not yet implemented',
dexProvider: 'JUPITER'
}
}
} else {
return NextResponse.json({
success: false,
error: `Unsupported DEX provider: ${dexProvider}`
}, { status: 400 })
}
console.log('✅ DEX response received:', response.success ? 'SUCCESS' : 'FAILED')
return NextResponse.json(response)
} catch (error) {
console.error('❌ Unified trading error:', error)
return NextResponse.json({
success: false,
error: 'Trading execution failed',
details: error.message
}, { status: 500 })
}
}