feat: Complete Phase 2 - Autonomous Trading System
- Add Pyth Network price monitoring (WebSocket + polling fallback) - Add Position Manager with automatic exit logic (TP1/TP2/SL) - Implement dynamic stop-loss adjustment (breakeven + profit lock) - Add real-time P&L tracking and multi-position support - Create comprehensive test suite (3 test scripts) - Add 5 detailed documentation files (2500+ lines) - Update configuration to $50 position size for safe testing - All Phase 2 features complete and tested Core Components: - v4/lib/pyth/price-monitor.ts - Real-time price monitoring - v4/lib/trading/position-manager.ts - Autonomous position management - v4/app/api/trading/positions/route.ts - Query positions endpoint - v4/test-*.ts - Comprehensive testing suite Documentation: - PHASE_2_COMPLETE_REPORT.md - Implementation summary - v4/PHASE_2_SUMMARY.md - Detailed feature overview - v4/TESTING.md - Testing guide - v4/QUICKREF_PHASE2.md - Quick reference - install-phase2.sh - Automated installation script
This commit is contained in:
210
prisma/schema-v4.prisma
Normal file
210
prisma/schema-v4.prisma
Normal file
@@ -0,0 +1,210 @@
|
||||
// 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])
|
||||
}
|
||||
Reference in New Issue
Block a user