Skip to main content

Overview

The Trade model stores individual trading records including entry/exit details, P&L, and associated metadata like images, videos, and tags.

Schema Fields

id
String
required
Unique identifier for the trade
  • Constraint: Primary key, unique
  • Default: Auto-generated UUID
accountNumber
String
required
The account number this trade belongs to
  • Indexed: Yes (performance optimization)
  • Used to group trades by account
quantity
Int
required
Number of contracts/shares traded
  • Must be a positive integer
entryId
String
Identifier for the entry order
  • Default: Empty string
  • Optional field for order tracking
closeId
String
Identifier for the closing order
  • Default: Empty string
  • Optional field for order tracking
instrument
String
required
Trading instrument symbol or ticker
  • Examples: “ES”, “NQ”, “AAPL”
entryPrice
String
required
Price at which the position was entered
  • Stored as string for precision
closePrice
String
required
Price at which the position was closed
  • Stored as string for precision
entryDate
String
required
Date and time when the trade was entered
  • Stored as string for flexible date formatting
closeDate
String
required
Date and time when the trade was closed
  • Stored as string for flexible date formatting
pnl
Float
required
Profit and loss for the trade
  • Can be positive (profit) or negative (loss)
timeInPosition
Float
required
Duration the position was held (in hours or minutes)
  • Default: 0
userId
String
required
ID of the user who owns this trade
  • Links to the User model
side
String
Trade direction: “LONG” or “SHORT”
  • Default: Empty string
commission
Float
required
Commission fees paid for this trade
  • Default: 0
createdAt
DateTime
required
Timestamp when the trade record was created
  • Default: Current timestamp
  • Auto-generated on creation
comment
String
User notes or comments about the trade
  • Optional field for trade journaling
tags
String[]
required
Array of tags associated with the trade
  • Default: Empty array
  • Used for categorization and filtering
imageBase64
String
Base64-encoded image of the trade chart
  • Optional screenshot or chart image
  • Legacy field (use images array for new trades)
videoUrl
String
URL to a video recording of the trade
  • Optional field for video analysis
imageBase64Second
String
Base64-encoded secondary image
  • Optional additional screenshot
  • Legacy field (use images array for new trades)
groupId
String
ID of the group this trade belongs to
  • Default: Empty string
  • Indexed: Yes (performance optimization)
  • Links to account groupings
images
String[]
required
Array of image URLs or Base64-encoded images
  • Default: Empty array
  • Replaces legacy imageBase64 fields

Indexes

The Trade model has the following indexes for query optimization:
  • accountNumber - Fast lookup of trades by account
  • groupId - Efficient filtering by group

Relationships

While the Trade model doesn’t have explicit foreign key relationships in the schema, it logically relates to:
  • User: Through the userId field
  • Account: Through the accountNumber field
  • Group: Through the groupId field

Database Location

This model is stored in the public schema of the PostgreSQL database.

Usage Examples

Querying Trades by Account

const trades = await prisma.trade.findMany({
  where: {
    accountNumber: '12345'
  },
  orderBy: {
    closeDate: 'desc'
  }
});

Creating a New Trade

const trade = await prisma.trade.create({
  data: {
    accountNumber: '12345',
    quantity: 2,
    instrument: 'ES',
    entryPrice: '4500.00',
    closePrice: '4520.00',
    entryDate: '2024-01-15T09:30:00Z',
    closeDate: '2024-01-15T10:45:00Z',
    pnl: 1000,
    timeInPosition: 1.25,
    userId: 'user_123',
    side: 'LONG',
    commission: 4.80,
    tags: ['scalp', 'morning'],
    comment: 'Clean breakout trade'
  }
});

Build docs developers (and LLMs) love