Overview
Thesimulator.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
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.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 of completed trades, ordered by close date (most recent first).
Unique trade identifier in format
{toolCallId}:{index}.AI model ID that executed the trade.
Human-readable model name (e.g.,
"Apex", "Trendsurfer").Trading pair symbol (e.g.,
"BTC-USD").Trade direction:
"LONG" or "SHORT".Position notional value in USD:
quantity * exitPrice.Realized profit/loss from closing the position.
Leverage multiplier used when opening the position (if tracked).
AI model confidence level (0-1) when opening the position (if tracked).
ISO 8601 timestamp when the position was closed.
AI invocation ID that triggered the trade.
Aggregated performance statistics calculated from the returned trades.
Total number of completed trades.
Sum of all realized P&L values.
Average P&L per trade:
totalRealized / tradeCount.null if no trades.Mean leverage across all trades with non-null leverage values.
null if no leverage data.Median leverage across all trades with non-null leverage values.
null if no leverage data.Maximum leverage value observed across all trades.
null if no leverage data.Array of all non-null leverage values (used for calculations).
Mean confidence level across all trades with non-null confidence values.
null if no confidence data.Median confidence level across all trades with non-null confidence values.
null if no confidence data.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
invocationIdto match opens with closes
Metadata Structure
Tool call metadata is JSON containing:Matching Logic
- Query all CLOSE_POSITION calls for the model (ordered by date)
- Extract closed position records from metadata
- Query all CREATE_POSITION calls for the same invocations
- Build an index of leverage/confidence by symbol from CREATE calls
- Match each closed position with its creation metadata
Statistics Calculation
Expectancy
Average profit per trade:Leverage Statistics
- Average: Mean of all non-null leverage values
- Median: Middle value of sorted leverage values
- Max: Highest leverage used across all trades
Confidence Statistics
- Average: Mean of all non-null confidence values (0-1)
- Median: Middle value of sorted confidence values
Null Handling
Statistics gracefully handle missing data:- Fields with
nullvalues 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 DESCfor 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
| Scenario | Behavior |
|---|---|
No modelId provided | Returns empty trades: [] and zero stats |
Invalid modelId | Returns empty result set (no error) |
limit exceeds 500 | Capped at 500 automatically |
| Database connection error | Throws error (caught by React Query) |
| Malformed JSON metadata | Skips invalid records, logs error |

