Skip to main content

Overview

The simulator.getCompletedTradesFromDB endpoint retrieves historical completed trades for a specific AI model from the database, along with calculated performance statistics. Unlike the in-memory simulator state, this endpoint queries persistent database records to provide historical trade analysis.

Request

Parameters

string
The AI model ID to filter trades by. If not provided or empty, returns an empty result set.Model ID corresponds to records in the Models table and links trades to specific AI agents.
number
default:"200"
Maximum number of trades to return. Must be a positive integer.
  • Default: 200
  • Maximum: 500 (hard cap enforced by the endpoint)
  • Used to prevent large result sets from impacting performance

Response

array
Array of completed trades, ordered by close date (most recent first).
string
Unique trade identifier in format {toolCallId}:{index}.
string
AI model ID that executed the trade.
string | null
Human-readable model name (e.g., "Apex", "Trendsurfer").
string
Trading pair symbol (e.g., "BTC-USD").
enum<string>
Trade direction: "LONG" or "SHORT".
number | null
Position notional value in USD: quantity * exitPrice.
number | null
Realized profit/loss from closing the position.
number | null
Leverage multiplier used when opening the position (if tracked).
number | null
AI model confidence level (0-1) when opening the position (if tracked).
string | null
ISO 8601 timestamp when the position was closed.
string
AI invocation ID that triggered the trade.
object
Aggregated performance statistics calculated from the returned trades.
number
Total number of completed trades.
number
Sum of all realized P&L values.
number | null
Average P&L per trade: totalRealized / tradeCount.null if no trades.
number | null
Mean leverage across all trades with non-null leverage values.null if no leverage data.
number | null
Median leverage across all trades with non-null leverage values.null if no leverage data.
number | null
Maximum leverage value observed across all trades.null if no leverage data.
array<number>
Array of all non-null leverage values (used for calculations).
number | null
Mean confidence level across all trades with non-null confidence values.null if no confidence data.
number | null
Median confidence level across all trades with non-null confidence values.null if no confidence data.
array<number>
Array of all non-null confidence values (used for calculations).

Code Example

Data Source

This endpoint queries the database, not the in-memory simulator state:

Tool Calls Table

Trades are reconstructed from tool call records:
  • CLOSE_POSITION tool calls contain closed position data
  • CREATE_POSITION tool calls contain leverage and confidence metadata
  • Trades are linked via invocationId to match opens with closes

Metadata Structure

Tool call metadata is JSON containing:

Matching Logic

  1. Query all CLOSE_POSITION calls for the model (ordered by date)
  2. Extract closed position records from metadata
  3. Query all CREATE_POSITION calls for the same invocations
  4. Build an index of leverage/confidence by symbol from CREATE calls
  5. Match each closed position with its creation metadata

Statistics Calculation

Expectancy

Average profit per trade:
Positive expectancy indicates profitable trading on average.

Leverage Statistics

  • Average: Mean of all non-null leverage values
  • Median: Middle value of sorted leverage values
  • Max: Highest leverage used across all trades
Only trades with tracked leverage are included in calculations.

Confidence Statistics

  • Average: Mean of all non-null confidence values (0-1)
  • Median: Middle value of sorted confidence values
Only trades with tracked confidence are included in calculations.

Null Handling

Statistics gracefully handle missing data:
  • Fields with null values are excluded from calculations
  • If no valid data exists, statistics return null
  • Empty arrays are returned when no values are available

Use Cases

Performance Analytics Dashboard

Display historical performance metrics for AI trading models:

Model Comparison

Compare performance across different AI models:

Trade Audit Trail

Review individual trade execution history:

Leverage Risk Analysis

Analyze leverage usage patterns:

Performance Considerations

Query Optimization

  • Database query uses indexed columns (modelId, toolCallType, createdAt)
  • Limit parameter caps result set size (max 500)
  • Results ordered by createdAt DESC for recent trades first

Metadata Parsing

JSON metadata is parsed on-the-fly:
  • Expect ~1-5ms parsing time per trade
  • Large result sets (500 trades) may take 100-500ms total
  • Consider pagination for very large datasets

Caching Strategy

Error Handling

This endpoint queries historical database records, not the current simulator state. Trades shown here may not reflect positions currently open in the simulator.