- 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
94 lines
2.2 KiB
Docker
94 lines
2.2 KiB
Docker
# Trading Bot v4 - Production Docker Image
|
|
# Multi-stage build for optimal size and security
|
|
|
|
# ================================
|
|
# Stage 1: Dependencies
|
|
# ================================
|
|
FROM node:20-alpine AS deps
|
|
|
|
# Install system dependencies for native modules
|
|
RUN apk add --no-cache \
|
|
python3 \
|
|
make \
|
|
g++ \
|
|
libc6-compat
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy package files
|
|
COPY package*.json ./
|
|
|
|
# Install dependencies (use npm install since we don't have package-lock.json yet)
|
|
RUN npm install --production && \
|
|
npm cache clean --force
|
|
|
|
# ================================
|
|
# Stage 2: Builder
|
|
# ================================
|
|
FROM node:20-alpine AS builder
|
|
|
|
WORKDIR /app
|
|
|
|
# Copy dependencies from deps stage
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Generate Prisma client before building
|
|
RUN npx prisma generate
|
|
|
|
# Build Next.js application
|
|
ENV NEXT_TELEMETRY_DISABLED 1
|
|
ENV NODE_ENV production
|
|
|
|
RUN npm run build
|
|
|
|
# ================================
|
|
# Stage 3: Runner (Production)
|
|
# ================================
|
|
FROM node:20-alpine AS runner
|
|
|
|
# Install dumb-init for proper signal handling and Docker CLI for restart capability
|
|
RUN apk add --no-cache dumb-init docker-cli
|
|
|
|
WORKDIR /app
|
|
|
|
# Create non-root user
|
|
RUN addgroup --system --gid 1001 nodejs && \
|
|
adduser --system --uid 1001 nextjs && \
|
|
addgroup nextjs root
|
|
|
|
# Copy necessary files from builder
|
|
COPY --from=builder /app/next.config.js ./
|
|
COPY --from=builder /app/package*.json ./
|
|
|
|
# Copy Next.js build output
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
|
# Copy node_modules
|
|
COPY --from=deps --chown=nextjs:nodejs /app/node_modules ./node_modules
|
|
|
|
# Set environment variables
|
|
ENV NODE_ENV production
|
|
ENV NEXT_TELEMETRY_DISABLED 1
|
|
ENV PORT 3000
|
|
ENV HOSTNAME "0.0.0.0"
|
|
|
|
# Expose port
|
|
EXPOSE 3000
|
|
|
|
# Switch to non-root user
|
|
USER nextjs
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=40s --retries=3 \
|
|
CMD node -e "require('http').get('http://localhost:3000/api/health', (r) => {process.exit(r.statusCode === 200 ? 0 : 1)})"
|
|
|
|
# Use dumb-init to handle signals properly
|
|
ENTRYPOINT ["dumb-init", "--"]
|
|
|
|
# Start the application
|
|
CMD ["node", "server.js"]
|