Files
trading_bot_v3/app/api/safe-paper-trading/create-trade/route.js
mindesbunister 284e1c8b8c feat: fix Safe Paper Trading display formatting and API sync
- Fixed field mapping between API and frontend (amount→positionSize, entry→entryPrice, createdAt→timestamp)
- Updated API sync function to properly convert API trade format to frontend format
- Resolved display issues: 'Invalid Date', missing entry price, missing trade size
- Added trade monitoring system and automation improvements
- Enhanced automation with simple-automation.js for reliable 24/7 operation
- Working automation now detecting 85% confidence BUY signals and executing trades
2025-08-07 16:55:41 +02:00

117 lines
3.4 KiB
JavaScript

import { NextResponse } from 'next/server'
// Simple in-memory storage for paper trades (in production, use database)
let paperTrades = []
let tradeIdCounter = 1
// Export function to get trades (for other API routes)
export function getAllPaperTrades() {
return paperTrades
}
export async function POST(request) {
try {
const tradeData = await request.json()
// Validate required fields
const required = ['symbol', 'side', 'amount', 'entry', 'confidence']
for (const field of required) {
if (!tradeData[field]) {
return NextResponse.json({
success: false,
message: `Missing required field: ${field}`
}, { status: 400 })
}
}
// Create paper trade
const trade = {
id: `PAPER_${Date.now()}_${tradeIdCounter++}`,
symbol: tradeData.symbol,
side: tradeData.side,
amount: tradeData.amount,
entry: tradeData.entry,
stopLoss: tradeData.stopLoss,
takeProfit: tradeData.takeProfit,
confidence: tradeData.confidence,
reasoning: tradeData.reasoning,
source: tradeData.source || 'manual',
status: 'OPEN',
createdAt: new Date().toISOString(),
pnl: 0,
fees: 0
}
// Store trade
paperTrades.push(trade)
console.log(`📄 Paper trade created: ${trade.id} - ${trade.side} ${trade.symbol} at $${trade.entry} (${trade.confidence}% confidence)`)
// Log to AI learning system if available
try {
const learningData = {
id: `decision_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
symbol: trade.symbol,
timeframe: '60',
side: trade.side,
confidence: trade.confidence,
entry: trade.entry,
stopLoss: trade.stopLoss,
takeProfit: trade.takeProfit,
reasoning: trade.reasoning,
source: 'paper_trade_automation',
createdAt: new Date().toISOString()
}
// Store in learning system (try to call learning API)
fetch('http://localhost:9001/api/ai-learning/record-decision', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(learningData)
}).catch(error => {
console.log('⚠️ Could not log to learning system:', error.message)
})
} catch (error) {
console.log('⚠️ Learning system integration error:', error.message)
}
return NextResponse.json({
success: true,
message: 'Paper trade created successfully',
trade: trade
})
} catch (error) {
console.error('❌ Create paper trade error:', error)
return NextResponse.json({
success: false,
message: 'Failed to create paper trade',
error: error.message
}, { status: 500 })
}
}
export async function GET(request) {
try {
// Return all paper trades
return NextResponse.json({
success: true,
trades: paperTrades,
summary: {
total: paperTrades.length,
open: paperTrades.filter(t => t.status === 'OPEN').length,
closed: paperTrades.filter(t => t.status === 'CLOSED').length,
totalPnL: paperTrades.reduce((sum, t) => sum + (t.pnl || 0), 0)
}
})
} catch (error) {
console.error('❌ Get paper trades error:', error)
return NextResponse.json({
success: false,
message: 'Failed to get paper trades',
error: error.message
}, { status: 500 })
}
}