Historical Swaps
Historical swaps represent completed trades - pairs of purchase and sale transactions that form a full trading cycle. They track the entire lifecycle from entry to exit, providing the basis for all performance metrics and tax reporting.
Overview
A historical swap is created automatically when a position is closed. The system pairs the original purchase (including any DCA buys) with the final sale to calculate realized profit or loss.
Lifecycle of a Swap
- Entry: Agent executes a
SWAP(SOL → Token). - Monitoring: Position is tracked; weighted average price updates if DCA buys occur.
- Exit: Position closure is triggered via Manual Close, Stop Loss, or Stale Trade.
- Realization: The system executes the final
SWAP(Token → SOL) and asynchronously generates theAgentHistoricalSwaprecord. - Analytics: Performance metrics (Win Rate, Average Return) are updated instantly.
Swap Data Structure
Each record captures the full context of the trade cycle:
| Field | Description |
|---|---|
| amount | Total token amount traded. |
| purchasePrice | Weighted average cost basis per token (accounts for DCA). |
| salePrice | Execution price per token at the time of exit. |
| changePercent | Total price movement percentage from entry to exit. |
| profitLossUsd/Sol | Realized profit or loss in both fiat and native currency. |
| purchase/saleTime | Precise timestamps for entry and exit. |
| closeReason | Why the position was sold (manual, stop_loss, or stale_trade). |
| transactionIds | UUID links to the raw AgentTransaction records. |
Calculations & Cost Basis
Weighted Average Purchase Price
To accurately track P&L when using DCA (Dollar Cost Averaging), Nexgent calculates a weighted average:
Formula: purchasePrice = totalInvestedSol / totalTokensHeld
This ensures that if you "buy the dip," your cost basis decreases, and your profit/loss reflects the true performance of the entire position size.
Realized Profit/Loss
Profit and loss are calculated after the final sale transaction completes:
- SOL P&L:
(salePrice - purchasePrice) * amount - USD P&L:
profitLossSol * solPriceAtExit - Return %:
((salePrice - purchasePrice) / purchasePrice) * 100
Performance & Reporting
Strategic Review
Use the Recent Trades table to analyze your agent's effectiveness:
- Win Rate: The percentage of swaps with a positive
changePercent. - Hold Time: Calculate the duration between
purchaseTimeandsaleTime. - Signal Efficacy: Identify which
signalIdsources lead to the highest returns.
Best Practices
- Audit Regularly: Compare historical swaps against your Agent Activity log to ensure balance reconciliation.
- Correlation Analysis: Check the
signalIdlinked to your best trades to identify high-conviction signal providers. - Mode Awareness: Remember that simulation mode swaps are virtual; always verify your strategy in simulation for 24-48 hours before assigning a live wallet.

