Files
trading_bot_v3/app/api/trading/history/route.js
mindesbunister b0b63d5db0 Fix critical balance validation and add comprehensive trading features
- Fixed CoinGecko API rate limiting with fallback SOL price (68.11)
- Corrected internal API calls to use proper Docker container ports
- Fixed balance validation to prevent trades exceeding wallet funds
- Blocked 0.5 SOL trades with only 0.073 SOL available (~2.24)

- Added persistent storage for positions, trades, and pending orders
- Implemented limit order system with auto-fill monitoring
- Created pending orders panel and management API
- Added trades history tracking and display panel
- Enhanced position tracking with P&L calculations
- Added wallet balance validation API endpoint

- Positions stored in data/positions.json
- Trade history stored in data/trades.json
- Pending orders with auto-fill logic
- Real-time balance validation before trades

- All trades now validate against actual wallet balance
- Insufficient balance trades are properly blocked
- Added comprehensive error handling and logging
- Fixed Docker networking for internal API calls

- SPOT and leveraged trading modes
- Limit orders with price monitoring
- Stop loss and take profit support
- DEX integration with Jupiter
- Real-time position updates and P&L tracking

 Tested and verified all balance validation works correctly
2025-07-14 17:19:58 +02:00

124 lines
3.2 KiB
JavaScript

import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
// Persistent storage for trades using JSON file
const TRADES_FILE = path.join(process.cwd(), 'data', 'trades.json')
// Ensure data directory exists
const dataDir = path.join(process.cwd(), 'data')
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true })
}
// Helper functions for persistent storage
function loadTrades() {
try {
if (fs.existsSync(TRADES_FILE)) {
const data = fs.readFileSync(TRADES_FILE, 'utf8')
return JSON.parse(data)
}
} catch (error) {
console.error('Error loading trades:', error)
}
return []
}
function saveTrades(trades) {
try {
fs.writeFileSync(TRADES_FILE, JSON.stringify(trades, null, 2))
} catch (error) {
console.error('Error saving trades:', error)
}
}
export async function GET() {
try {
// Load trades from persistent storage
const tradesHistory = loadTrades()
// Sort trades by timestamp (newest first)
const sortedTrades = tradesHistory.sort((a, b) => b.timestamp - a.timestamp)
return NextResponse.json({
success: true,
trades: sortedTrades,
totalTrades: sortedTrades.length
})
} catch (error) {
console.error('Error fetching trades history:', error)
return NextResponse.json({
success: false,
error: 'Failed to fetch trades history',
trades: []
}, { status: 500 })
}
}
export async function POST(request) {
try {
const body = await request.json()
const { action, ...tradeData } = body
if (action === 'add') {
// Load existing trades
const tradesHistory = loadTrades()
// Add new trade to history
const newTrade = {
id: `trade_${Date.now()}_${Math.random().toString(36).substr(2, 8)}`,
symbol: tradeData.symbol,
side: tradeData.side,
amount: parseFloat(tradeData.amount),
price: parseFloat(tradeData.price),
type: tradeData.type || 'market',
status: tradeData.status || 'executed',
timestamp: Date.now(),
txId: tradeData.txId || null,
fee: tradeData.fee || 0,
pnl: tradeData.pnl || null, // For closing trades
dex: tradeData.dex || 'JUPITER',
notes: tradeData.notes || null
}
tradesHistory.push(newTrade)
// Keep only last 100 trades to prevent memory issues
if (tradesHistory.length > 100) {
tradesHistory.splice(0, tradesHistory.length - 100)
}
// Save to persistent storage
saveTrades(tradesHistory)
return NextResponse.json({
success: true,
trade: newTrade,
message: `Trade added to history: ${newTrade.side} ${newTrade.amount} ${newTrade.symbol}`
})
} else if (action === 'clear') {
// Clear trade history
saveTrades([])
return NextResponse.json({
success: true,
message: 'Trade history cleared'
})
}
return NextResponse.json({
success: false,
error: 'Invalid action'
}, { status: 400 })
} catch (error) {
console.error('Error managing trades history:', error)
return NextResponse.json({
success: false,
error: 'Failed to manage trades history'
}, { status: 500 })
}
}