diff --git a/app/api/price-monitor/route.js b/app/api/price-monitor/route.js index ccc7b92..a6e3595 100644 --- a/app/api/price-monitor/route.js +++ b/app/api/price-monitor/route.js @@ -7,14 +7,22 @@ export async function GET() { const monitoringData = await priceMonitor.getTradeMonitoringData() const activeAlerts = priceMonitor.getActiveAlerts() - // Get price cache for quick reference - const priceData = {} - const symbols = [...new Set(monitoringData.map(trade => trade.symbol))] + // Get price cache for all common trading symbols + active trade symbols + const baseSymbols = ['SOLUSD', 'BTCUSD', 'ETHUSD'] // Always show these + const tradeSymbols = monitoringData.map(trade => trade.symbol) + const allSymbols = [...new Set([...baseSymbols, ...tradeSymbols])] - for (const symbol of symbols) { + const priceData = {} + for (const symbol of allSymbols) { const price = priceMonitor.getCurrentPrice(symbol) if (price) { priceData[symbol] = price + } else { + // Force fetch price if not cached + const fetchedPrice = await priceMonitor.forceUpdatePrice(symbol) + if (fetchedPrice) { + priceData[symbol] = fetchedPrice + } } } @@ -25,7 +33,7 @@ export async function GET() { alerts: activeAlerts, prices: priceData, lastUpdated: new Date().toISOString(), - monitoringActive: true + monitoringActive: priceMonitor.isMonitoring() } }) } catch (error) { diff --git a/create-demo-trades.js b/create-demo-trades.js new file mode 100644 index 0000000..dffce28 --- /dev/null +++ b/create-demo-trades.js @@ -0,0 +1,99 @@ +const { PrismaClient } = require('@prisma/client') + +const prisma = new PrismaClient() + +async function createDemoTrade() { + try { + console.log('šŸŽÆ Creating demo trade for price monitoring...') + + // Create a demo user if doesn't exist + const user = await prisma.user.upsert({ + where: { id: 'demo-user' }, + update: {}, + create: { + id: 'demo-user', + email: 'demo@trading-bot.com', + name: 'Demo User' + } + }) + + // Create a demo trade - SOLUSD Buy position + const currentSolPrice = 190.82 // Current SOL price + const trade = await prisma.trade.create({ + data: { + userId: user.id, + symbol: 'SOLUSD', + side: 'BUY', + amount: 100, // $100 investment + price: currentSolPrice, + entryPrice: currentSolPrice, + status: 'OPEN', + stopLoss: currentSolPrice * 0.98, // 2% stop loss + takeProfit: currentSolPrice * 1.06, // 6% take profit + leverage: 1, + timeframe: '1h', + tradingMode: 'SIMULATION', + confidence: 85, + isAutomated: true, + createdAt: new Date(), + updatedAt: new Date() + } + }) + + console.log('āœ… Demo trade created:') + console.log(` ID: ${trade.id}`) + console.log(` Symbol: ${trade.symbol}`) + console.log(` Side: ${trade.side}`) + console.log(` Entry Price: $${trade.entryPrice}`) + console.log(` Stop Loss: $${trade.stopLoss?.toFixed(2)}`) + console.log(` Take Profit: $${trade.takeProfit?.toFixed(2)}`) + console.log(` Amount: $${trade.amount}`) + console.log() + + // Also create one for BTC + const currentBtcPrice = 119050 + const btcTrade = await prisma.trade.create({ + data: { + userId: user.id, + symbol: 'BTCUSD', + side: 'SELL', + amount: 200, + price: currentBtcPrice, + entryPrice: currentBtcPrice, + status: 'OPEN', + stopLoss: currentBtcPrice * 1.02, // 2% stop loss (higher for short) + takeProfit: currentBtcPrice * 0.94, // 6% take profit (lower for short) + leverage: 2, + timeframe: '4h', + tradingMode: 'SIMULATION', + confidence: 78, + isAutomated: true, + createdAt: new Date(), + updatedAt: new Date() + } + }) + + console.log('āœ… Second demo trade created:') + console.log(` ID: ${btcTrade.id}`) + console.log(` Symbol: ${btcTrade.symbol}`) + console.log(` Side: ${btcTrade.side}`) + console.log(` Entry Price: $${btcTrade.entryPrice}`) + console.log(` Stop Loss: $${btcTrade.stopLoss?.toFixed(2)}`) + console.log(` Take Profit: $${btcTrade.takeProfit?.toFixed(2)}`) + console.log(` Amount: $${btcTrade.amount}`) + + console.log('\nšŸŽ‰ Demo trades ready! Now you can test the price monitoring system.') + console.log('šŸ’” Visit http://localhost:9001/automation to see them in action!') + + } catch (error) { + console.error('āŒ Error creating demo trade:', error) + } finally { + await prisma.$disconnect() + } +} + +if (require.main === module) { + createDemoTrade() +} + +module.exports = { createDemoTrade } diff --git a/lib/price-monitor.ts b/lib/price-monitor.ts index 8608a19..6a194ab 100644 --- a/lib/price-monitor.ts +++ b/lib/price-monitor.ts @@ -91,6 +91,10 @@ class PriceMonitor extends EventEmitter { console.log('ā¹ļø Price monitoring service stopped') } + isMonitoring(): boolean { + return this.isRunning + } + private async updatePricesAndAnalyze(): Promise { console.log('šŸ“Š Price monitor: Updating prices and checking positions...') @@ -98,15 +102,24 @@ class PriceMonitor extends EventEmitter { // Get all active trades const activeTrades = await this.getActiveTradesForMonitoring() + // Always fetch prices for common trading symbols, even without active trades + const baseSymbols = ['SOLUSD', 'BTCUSD', 'ETHUSD'] // Common trading pairs + const tradeSymbols = activeTrades.map(trade => trade.symbol) + const symbols = [...new Set([...baseSymbols, ...tradeSymbols])] + + console.log(`šŸ“Š Updating prices for symbols: ${symbols.join(', ')}`) + if (activeTrades.length === 0) { - console.log('šŸ“Š No active trades to monitor') + console.log('šŸ“Š No active trades to monitor, but updating base symbol prices') + // Still update prices for base symbols + const priceUpdates = await this.fetchPricesForSymbols(symbols) + + // Emit price updates for UI + this.emit('pricesUpdated', priceUpdates) return } console.log(`šŸ“Š Monitoring ${activeTrades.length} active trades`) - - // Get unique symbols - const symbols = [...new Set(activeTrades.map(trade => trade.symbol))] // Update prices for all symbols const priceUpdates = await this.fetchPricesForSymbols(symbols) diff --git a/prisma/prisma/dev.db b/prisma/prisma/dev.db index b79865d..b3b7f36 100644 Binary files a/prisma/prisma/dev.db and b/prisma/prisma/dev.db differ