OS Trading Engine
Platform Guide
Transactions & History
Historical Swaps

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.

Recent Trades Table
Recent trades table showing completed swaps with P/L and close reasons

Lifecycle of a Swap

  1. Entry: Agent executes a SWAP (SOL → Token).
  2. Monitoring: Position is tracked; weighted average price updates if DCA buys occur.
  3. Exit: Position closure is triggered via Manual Close, Stop Loss, or Stale Trade.
  4. Realization: The system executes the final SWAP (Token → SOL) and asynchronously generates the AgentHistoricalSwap record.
  5. Analytics: Performance metrics (Win Rate, Average Return) are updated instantly.

Swap Data Structure

Each record captures the full context of the trade cycle:

FieldDescription
amountTotal token amount traded.
purchasePriceWeighted average cost basis per token (accounts for DCA).
salePriceExecution price per token at the time of exit.
changePercentTotal price movement percentage from entry to exit.
profitLossUsd/SolRealized profit or loss in both fiat and native currency.
purchase/saleTimePrecise timestamps for entry and exit.
closeReasonWhy the position was sold (manual, stop_loss, or stale_trade).
transactionIdsUUID links to the raw AgentTransaction records.
Historical Swap Details
Detailed view of a completed trade including cost basis and signal correlation

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 purchaseTime and saleTime.
  • Signal Efficacy: Identify which signalId sources 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 signalId linked 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.