// Trading Bot v4 - Database Schema // This is a NEW schema for v4 - keep your existing schema.prisma for v3 generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } // Trade record model Trade { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt // Position identification positionId String @unique symbol String // e.g., 'SOL-PERP' direction String // 'long' or 'short' timeframe String // '5' for 5-minute strategy String @default("5min_scalp_v4") // Entry details entryPrice Float entryTime DateTime entrySlippage Float? // Actual slippage on entry positionSize Float // Total USD size leverage Int // 10x // Exit targets stopLoss Float takeProfit1 Float takeProfit2 Float emergencyStop Float // Exit details exitPrice Float? exitTime DateTime? exitReason String? // 'TP1', 'TP2', 'SL', 'emergency', 'manual' exitSlippage Float? // P&L tracking realizedPnL Float @default(0) realizedPnLPercent Float @default(0) peakUnrealizedPnL Float @default(0) // State tracking status String // 'open', 'partial', 'closed' tp1Hit Boolean @default(false) tp1Time DateTime? slMovedToBreakeven Boolean @default(false) slMovedToProfit Boolean @default(false) // Monitoring stats priceChecks Int @default(0) monitoringStartTime DateTime @default(now()) monitoringEndTime DateTime? averagePriceLatency Float? // Signal metadata signalStrength String? // 'strong', 'moderate', 'weak' signalSource String @default("tradingview") // Relationships priceUpdates PriceUpdate[] notifications TradeNotification[] @@index([symbol, entryTime]) @@index([status, entryTime]) @@index([createdAt]) } // Price update records (for debugging and analysis) model PriceUpdate { id String @id @default(cuid()) timestamp DateTime @default(now()) tradeId String trade Trade @relation(fields: [tradeId], references: [id], onDelete: Cascade) price Float source String // 'pyth', 'drift' confidence Float? slot BigInt? // Solana slot number profitPercent Float? accountPnL Float? @@index([tradeId, timestamp]) } // Notification tracking model TradeNotification { id String @id @default(cuid()) timestamp DateTime @default(now()) tradeId String trade Trade @relation(fields: [tradeId], references: [id], onDelete: Cascade) type String // 'entry', 'tp1', 'tp2', 'sl', 'emergency', 'error' channel String // 'telegram', 'discord', 'email' message String success Boolean error String? @@index([tradeId, timestamp]) } // Daily statistics model DailyStats { id String @id @default(cuid()) date DateTime @unique @db.Date totalTrades Int @default(0) winningTrades Int @default(0) losingTrades Int @default(0) breakEvenTrades Int @default(0) totalPnL Float @default(0) winRate Float @default(0) averageWin Float @default(0) averageLoss Float @default(0) largestWin Float @default(0) largestLoss Float @default(0) averageSlippage Float @default(0) averageHoldTime Int @default(0) // in seconds tp1HitCount Int @default(0) tp2HitCount Int @default(0) slHitCount Int @default(0) emergencyStopCount Int @default(0) updatedAt DateTime @updatedAt @@index([date]) } // Risk management tracking model RiskEvent { id String @id @default(cuid()) timestamp DateTime @default(now()) type String // 'blocked', 'warning', 'emergency' reason String symbol String? dailyPnL Float? tradesInLastHour Int? timeSinceLastTrade Int? blocked Boolean @default(false) @@index([timestamp]) @@index([type]) } // System configuration model SystemConfig { id String @id @default(cuid()) key String @unique value String type String // 'string', 'number', 'boolean', 'json' description String? updatedAt DateTime @updatedAt @@index([key]) } // Price feed health monitoring model PriceFeedHealth { id String @id @default(cuid()) timestamp DateTime @default(now()) symbol String source String // 'pyth', 'drift' isConnected Boolean lastUpdate DateTime? updateCount Int @default(0) errorCount Int @default(0) averageLatency Float? lastError String? @@index([symbol, source, timestamp]) } // Webhook logs (for debugging n8n integration) model WebhookLog { id String @id @default(cuid()) timestamp DateTime @default(now()) source String // 'tradingview', 'n8n' endpoint String method String payload Json response Json? statusCode Int? success Boolean error String? processingTime Int? // milliseconds @@index([timestamp]) @@index([source]) }