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
This commit is contained in:
mindesbunister
2025-08-07 16:55:41 +02:00
parent ce42b8cade
commit 284e1c8b8c
13 changed files with 901 additions and 11 deletions

View File

@@ -440,6 +440,68 @@ export default function SafePaperTradingPage() {
console.log('📂 Restored paper balance from localStorage')
}
// Sync with API trades (from automation)
const syncApiTrades = async () => {
try {
console.log('🔄 Syncing trades from API...')
const response = await fetch('/api/safe-paper-trading/trades')
if (response.ok) {
const data = await response.json()
if (data.success && data.trades.length > 0) {
console.log(`📈 Found ${data.trades.length} API trades, syncing with localStorage...`)
// Get existing localStorage trades
const existingTrades = JSON.parse(localStorage.getItem('safePaperTrading_paperTrades') || '[]')
// Convert API trades to frontend format and filter out existing ones
const existingIds = new Set(existingTrades.map(t => t.id))
const newTrades = data.trades
.filter(t => !existingIds.has(t.id))
.map(apiTrade => ({
id: apiTrade.id,
symbol: apiTrade.symbol,
side: apiTrade.side,
positionSize: apiTrade.amount, // Map amount to positionSize
entryPrice: apiTrade.entry, // Map entry to entryPrice
confidence: apiTrade.confidence,
reasoning: apiTrade.reasoning,
source: apiTrade.source,
status: apiTrade.status,
timestamp: apiTrade.createdAt, // Map createdAt to timestamp
pnl: apiTrade.pnl || 0,
fees: apiTrade.fees || 0
}))
if (newTrades.length > 0) {
const combinedTrades = [...existingTrades, ...newTrades]
setPaperTrades(combinedTrades)
localStorage.setItem('safePaperTrading_paperTrades', JSON.stringify(combinedTrades))
// Update balance based on new trades
const additionalValue = newTrades.reduce((sum, trade) => {
return sum + (trade.side === 'BUY' ? -trade.positionSize : trade.positionSize)
}, 0)
const newBalance = paperBalance + additionalValue
setPaperBalance(newBalance)
localStorage.setItem('safePaperTrading_paperBalance', newBalance.toString())
console.log(`✅ Synced ${newTrades.length} new trades from API`)
} else {
console.log('📊 All API trades already in localStorage')
}
} else {
console.log('📊 No API trades found')
}
}
} catch (error) {
console.log('❌ Failed to sync API trades:', error.message)
}
}
// Sync API trades after loading localStorage
syncApiTrades()
// Fetch AI learning status
fetchLearningStatus()