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

33
.env
View File

@@ -72,6 +72,25 @@ LEVERAGE=5
# Example: -1.5% on 10x = -15% account loss
STOP_LOSS_PERCENT=-2.0
# ================================
# DUAL STOP SYSTEM (Advanced)
# ================================
# Enable dual stop system to avoid wicks while guaranteeing exit
# When enabled, places TWO stop orders:
# 1. Soft Stop (TRIGGER_LIMIT) - Avoids false breakouts/wicks
# 2. Hard Stop (TRIGGER_MARKET) - Guarantees exit if price keeps falling
USE_DUAL_STOPS=true
# Soft Stop (Primary, Stop-Limit)
# Triggers first, tries to avoid wicks
SOFT_STOP_PERCENT=-1.5
SOFT_STOP_BUFFER=0.4 # Buffer between trigger and limit (0.4% = limit at -1.9%)
# Hard Stop (Backup, Stop-Market)
# Only triggers if soft stop doesn't fill
# Guarantees exit during strong breakdowns
HARD_STOP_PERCENT=-2.5
# Take Profit 1: Close 50% of position at this profit level
# Example: +0.7% on 10x = +7% account gain
TAKE_PROFIT_1_PERCENT=0.5
@@ -186,11 +205,15 @@ EMAIL_PASSWORD=your_16_character_app_password
# PostgreSQL connection string
# Format: postgresql://username:password@host:port/database
#
# Local setup:
# 1. Install PostgreSQL: https://www.postgresql.org/download/
# 2. Create database: createdb trading_bot_v4
# 3. Update connection string below
DATABASE_URL=postgresql://postgres:password@localhost:5432/trading_bot_v4
# IMPORTANT: Use different URLs for different environments:
# - Docker container (runtime): trading-bot-postgres (container name)
# - Local development (Prisma CLI): localhost:5432
#
# The URL below is for Docker runtime. For Prisma migrations from host:
# DATABASE_URL="postgresql://postgres:postgres@localhost:5432/trading_bot_v4" npx prisma migrate dev
#
# PostgreSQL Database (for trade history and analytics)
DATABASE_URL=postgresql://postgres:postgres@trading-bot-postgres:5432/trading_bot_v4
# Cloud PostgreSQL providers:
# - Supabase: https://supabase.com (free tier available)