feat: implement dual stop system and database tracking

- Add PostgreSQL database with Prisma ORM
  - Trade model: tracks entry/exit, P&L, order signatures, config snapshots
  - PriceUpdate model: tracks price movements for drawdown analysis
  - SystemEvent model: logs errors and system events
  - DailyStats model: aggregated performance metrics

- Implement dual stop loss system (enabled by default)
  - Soft stop (TRIGGER_LIMIT) at -1.5% to avoid wicks
  - Hard stop (TRIGGER_MARKET) at -2.5% to guarantee exit
  - Configurable via USE_DUAL_STOPS, SOFT_STOP_PERCENT, HARD_STOP_PERCENT
  - Backward compatible with single stop modes

- Add database service layer (lib/database/trades.ts)
  - createTrade(): save new trades with all details
  - updateTradeExit(): close trades with P&L calculations
  - addPriceUpdate(): track price movements during trade
  - getTradeStats(): calculate win rate, profit factor, avg win/loss
  - logSystemEvent(): log errors and system events

- Update execute endpoint to use dual stops and save to database
  - Calculate dual stop prices when enabled
  - Pass dual stop parameters to placeExitOrders
  - Save complete trade record to database after execution

- Add test trade button to settings page
  - New /api/trading/test endpoint for executing test trades
  - Displays detailed results including dual stop prices
  - Confirmation dialog before execution
  - Shows entry price, position size, stops, and TX signature

- Generate Prisma client in Docker build
- Update DATABASE_URL for container networking
This commit is contained in:
mindesbunister
2025-10-26 21:29:27 +01:00
parent 33821eae0c
commit d64f6d84c4
13 changed files with 2616 additions and 78 deletions

245
lib/database/trades.ts Normal file
View File

@@ -0,0 +1,245 @@
/**
* Database Service for Trade Tracking and Analytics
*/
import { PrismaClient } from '@prisma/client'
// Singleton Prisma client
let prisma: PrismaClient | null = null
export function getPrismaClient(): PrismaClient {
if (!prisma) {
prisma = new PrismaClient({
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
})
console.log('✅ Prisma client initialized')
}
return prisma
}
export interface CreateTradeParams {
positionId: string
symbol: string
direction: 'long' | 'short'
entryPrice: number
entrySlippage?: number
positionSizeUSD: number
leverage: number
stopLossPrice: number
softStopPrice?: number
hardStopPrice?: number
takeProfit1Price: number
takeProfit2Price: number
tp1SizePercent: number
tp2SizePercent: number
entryOrderTx: string
tp1OrderTx?: string
tp2OrderTx?: string
slOrderTx?: string
softStopOrderTx?: string
hardStopOrderTx?: string
configSnapshot: any
signalSource?: string
signalStrength?: string
timeframe?: string
}
export interface UpdateTradeExitParams {
positionId: string
exitPrice: number
exitReason: 'TP1' | 'TP2' | 'SL' | 'SOFT_SL' | 'HARD_SL' | 'manual' | 'emergency'
realizedPnL: number
exitOrderTx: string
holdTimeSeconds: number
maxDrawdown?: number
maxGain?: number
}
/**
* Create a new trade record
*/
export async function createTrade(params: CreateTradeParams) {
const prisma = getPrismaClient()
try {
const trade = await prisma.trade.create({
data: {
positionId: params.positionId,
symbol: params.symbol,
direction: params.direction,
entryPrice: params.entryPrice,
entryTime: new Date(),
entrySlippage: params.entrySlippage,
positionSizeUSD: params.positionSizeUSD,
leverage: params.leverage,
stopLossPrice: params.stopLossPrice,
softStopPrice: params.softStopPrice,
hardStopPrice: params.hardStopPrice,
takeProfit1Price: params.takeProfit1Price,
takeProfit2Price: params.takeProfit2Price,
tp1SizePercent: params.tp1SizePercent,
tp2SizePercent: params.tp2SizePercent,
entryOrderTx: params.entryOrderTx,
tp1OrderTx: params.tp1OrderTx,
tp2OrderTx: params.tp2OrderTx,
slOrderTx: params.slOrderTx,
softStopOrderTx: params.softStopOrderTx,
hardStopOrderTx: params.hardStopOrderTx,
configSnapshot: params.configSnapshot,
signalSource: params.signalSource,
signalStrength: params.signalStrength,
timeframe: params.timeframe,
status: 'open',
},
})
console.log(`📊 Trade record created: ${trade.id}`)
return trade
} catch (error) {
console.error('❌ Failed to create trade record:', error)
throw error
}
}
/**
* Update trade when position exits
*/
export async function updateTradeExit(params: UpdateTradeExitParams) {
const prisma = getPrismaClient()
try {
// First fetch the trade to get positionSizeUSD
const existingTrade = await prisma.trade.findUnique({
where: { positionId: params.positionId },
select: { positionSizeUSD: true },
})
if (!existingTrade) {
throw new Error(`Trade not found: ${params.positionId}`)
}
const trade = await prisma.trade.update({
where: { positionId: params.positionId },
data: {
exitPrice: params.exitPrice,
exitTime: new Date(),
exitReason: params.exitReason,
realizedPnL: params.realizedPnL,
realizedPnLPercent: (params.realizedPnL / existingTrade.positionSizeUSD) * 100,
exitOrderTx: params.exitOrderTx,
holdTimeSeconds: params.holdTimeSeconds,
maxDrawdown: params.maxDrawdown,
maxGain: params.maxGain,
status: 'closed',
},
})
console.log(`📊 Trade closed: ${trade.id} | P&L: $${params.realizedPnL.toFixed(2)}`)
return trade
} catch (error) {
console.error('❌ Failed to update trade exit:', error)
throw error
}
}
/**
* Add price update for a trade (for tracking max gain/drawdown)
*/
export async function addPriceUpdate(
tradeId: string,
price: number,
pnl: number,
pnlPercent: number
) {
const prisma = getPrismaClient()
try {
await prisma.priceUpdate.create({
data: {
tradeId,
price,
pnl,
pnlPercent,
},
})
} catch (error) {
console.error('❌ Failed to add price update:', error)
// Don't throw - price updates are non-critical
}
}
/**
* Log system event
*/
export async function logSystemEvent(
eventType: string,
message: string,
details?: any
) {
const prisma = getPrismaClient()
try {
await prisma.systemEvent.create({
data: {
eventType,
message,
details: details ? JSON.parse(JSON.stringify(details)) : null,
},
})
} catch (error) {
console.error('❌ Failed to log system event:', error)
}
}
/**
* Get trade statistics
*/
export async function getTradeStats(days: number = 30) {
const prisma = getPrismaClient()
const since = new Date()
since.setDate(since.getDate() - days)
const trades = await prisma.trade.findMany({
where: {
createdAt: { gte: since },
status: 'closed',
},
})
const winning = trades.filter((t) => (t.realizedPnL ?? 0) > 0)
const losing = trades.filter((t) => (t.realizedPnL ?? 0) < 0)
const totalPnL = trades.reduce((sum, t) => sum + (t.realizedPnL ?? 0), 0)
const winRate = trades.length > 0 ? (winning.length / trades.length) * 100 : 0
const avgWin = winning.length > 0
? winning.reduce((sum, t) => sum + (t.realizedPnL ?? 0), 0) / winning.length
: 0
const avgLoss = losing.length > 0
? losing.reduce((sum, t) => sum + (t.realizedPnL ?? 0), 0) / losing.length
: 0
return {
totalTrades: trades.length,
winningTrades: winning.length,
losingTrades: losing.length,
winRate: winRate.toFixed(2),
totalPnL: totalPnL.toFixed(2),
avgWin: avgWin.toFixed(2),
avgLoss: avgLoss.toFixed(2),
profitFactor: avgLoss !== 0 ? (avgWin / Math.abs(avgLoss)).toFixed(2) : 'N/A',
}
}
/**
* Disconnect Prisma client (for graceful shutdown)
*/
export async function disconnectPrisma() {
if (prisma) {
await prisma.$disconnect()
prisma = null
console.log('✅ Prisma client disconnected')
}
}

View File

@@ -63,6 +63,11 @@ export interface PlaceExitOrdersOptions {
direction: 'long' | 'short'
useStopLimit?: boolean // Optional: use TRIGGER_LIMIT instead of TRIGGER_MARKET for SL
stopLimitBuffer?: number // Optional: buffer percentage for stop-limit (default 0.5%)
// Dual Stop System
useDualStops?: boolean // Enable dual stop system
softStopPrice?: number // Soft stop trigger price (TRIGGER_LIMIT)
softStopBuffer?: number // Buffer for soft stop limit price
hardStopPrice?: number // Hard stop trigger price (TRIGGER_MARKET)
}
/**
@@ -273,64 +278,127 @@ export async function placeExitOrders(options: PlaceExitOrdersOptions): Promise<
}
}
// Place Stop-Loss order
// Default: TRIGGER_MARKET (guaranteed execution, RECOMMENDED for most traders)
// Optional: TRIGGER_LIMIT with buffer (only for very liquid markets to avoid extreme wicks)
// Place Stop-Loss order(s)
// Supports three modes:
// 1. Dual Stop System (soft stop-limit + hard stop-market)
// 2. Single TRIGGER_LIMIT (for liquid markets)
// 3. Single TRIGGER_MARKET (default, guaranteed execution)
const slUSD = options.positionSizeUSD
const slBaseAmount = usdToBase(slUSD, options.stopLossPrice)
if (slBaseAmount >= Math.floor(marketConfig.minOrderSize * 1e9)) {
const useStopLimit = options.useStopLimit ?? false
const stopLimitBuffer = options.stopLimitBuffer ?? 0.5 // default 0.5% buffer
const useDualStops = options.useDualStops ?? false
if (useStopLimit) {
// TRIGGER_LIMIT: Protects against extreme wicks but may not fill during fast moves
const limitPriceMultiplier = options.direction === 'long'
? (1 - stopLimitBuffer / 100) // Long: limit below trigger
: (1 + stopLimitBuffer / 100) // Short: limit above trigger
if (useDualStops && options.softStopPrice && options.hardStopPrice) {
// ============== DUAL STOP SYSTEM ==============
console.log('🛡️🛡️ Placing DUAL STOP SYSTEM...')
const orderParams: any = {
// 1. Soft Stop (TRIGGER_LIMIT) - Avoids wicks
const softStopBuffer = options.softStopBuffer ?? 0.4
const softStopMultiplier = options.direction === 'long'
? (1 - softStopBuffer / 100)
: (1 + softStopBuffer / 100)
const softStopParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
price: new BN(Math.floor(options.stopLossPrice * limitPriceMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
triggerPrice: new BN(Math.floor(options.softStopPrice * 1e6)),
price: new BN(Math.floor(options.softStopPrice * softStopMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
console.log(`🛡️ Placing SL as TRIGGER_LIMIT (${stopLimitBuffer}% buffer)...`)
console.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
console.log(` Limit: $${(options.stopLossPrice * limitPriceMultiplier).toFixed(4)}`)
console.log(` ⚠️ May not fill during fast moves - use for liquid markets only!`)
console.log(` 1⃣ Soft Stop (TRIGGER_LIMIT):`)
console.log(` Trigger: $${options.softStopPrice.toFixed(4)}`)
console.log(` Limit: $${(options.softStopPrice * softStopMultiplier).toFixed(4)}`)
console.log(` Purpose: Avoid false breakouts/wicks`)
const sig = await (driftClient as any).placePerpOrder(orderParams)
console.log('✅ SL trigger-limit order placed:', sig)
signatures.push(sig)
} else {
// TRIGGER_MARKET: Guaranteed execution (RECOMMENDED)
const orderParams: any = {
const softStopSig = await (driftClient as any).placePerpOrder(softStopParams)
console.log(` ✅ Soft stop placed: ${softStopSig}`)
signatures.push(softStopSig)
// 2. Hard Stop (TRIGGER_MARKET) - Guarantees exit
const hardStopParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
triggerPrice: new BN(Math.floor(options.hardStopPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
console.log(`🛡️ Placing SL as TRIGGER_MARKET (guaranteed execution - RECOMMENDED)...`)
console.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
console.log(` ✅ Will execute at market price when triggered (may slip but WILL fill)`)
console.log(` 2⃣ Hard Stop (TRIGGER_MARKET):`)
console.log(` Trigger: $${options.hardStopPrice.toFixed(4)}`)
console.log(` Purpose: Guaranteed exit if soft stop doesn't fill`)
const sig = await (driftClient as any).placePerpOrder(orderParams)
console.log('✅ SL trigger-market order placed:', sig)
signatures.push(sig)
const hardStopSig = await (driftClient as any).placePerpOrder(hardStopParams)
console.log(` ✅ Hard stop placed: ${hardStopSig}`)
signatures.push(hardStopSig)
console.log(`🎯 Dual stop system active: Soft @ $${options.softStopPrice.toFixed(2)} | Hard @ $${options.hardStopPrice.toFixed(2)}`)
} else {
// ============== SINGLE STOP SYSTEM ==============
const useStopLimit = options.useStopLimit ?? false
const stopLimitBuffer = options.stopLimitBuffer ?? 0.5
if (useStopLimit) {
// TRIGGER_LIMIT: For liquid markets
const limitPriceMultiplier = options.direction === 'long'
? (1 - stopLimitBuffer / 100)
: (1 + stopLimitBuffer / 100)
const orderParams: any = {
orderType: OrderType.TRIGGER_LIMIT,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
price: new BN(Math.floor(options.stopLossPrice * limitPriceMultiplier * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
console.log(`🛡️ Placing SL as TRIGGER_LIMIT (${stopLimitBuffer}% buffer)...`)
console.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
console.log(` Limit: $${(options.stopLossPrice * limitPriceMultiplier).toFixed(4)}`)
console.log(` ⚠️ May not fill during fast moves - use for liquid markets only!`)
const sig = await (driftClient as any).placePerpOrder(orderParams)
console.log('✅ SL trigger-limit order placed:', sig)
signatures.push(sig)
} else {
// TRIGGER_MARKET: Default, guaranteed execution
const orderParams: any = {
orderType: OrderType.TRIGGER_MARKET,
marketIndex: marketConfig.driftMarketIndex,
direction: orderDirection,
baseAssetAmount: new BN(slBaseAmount),
triggerPrice: new BN(Math.floor(options.stopLossPrice * 1e6)),
triggerCondition: options.direction === 'long'
? OrderTriggerCondition.BELOW
: OrderTriggerCondition.ABOVE,
reduceOnly: true,
}
console.log(`🛡️ Placing SL as TRIGGER_MARKET (guaranteed execution - RECOMMENDED)...`)
console.log(` Trigger: ${options.direction === 'long' ? 'BELOW' : 'ABOVE'} $${options.stopLossPrice.toFixed(4)}`)
console.log(` ✅ Will execute at market price when triggered (may slip but WILL fill)`)
const sig = await (driftClient as any).placePerpOrder(orderParams)
console.log('✅ SL trigger-market order placed:', sig)
signatures.push(sig)
}
}
} else {
console.log('⚠️ SL size below market min, skipping on-chain SL')