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; take-profit sales may execute if enabled.
  3. Exit: Position closure is triggered via Manual Close, Stop Loss, Stale Trade, or final Take-Profit level.
  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.

Positions with Take-Profit: If take-profit was enabled, partial sales may have occurred before the final close. The historical swap record captures the full position lifecycle, including all take-profit sales aggregated into the final P/L.


Swap Data Structure

Each record captures the full context of the trade cycle:

FieldDescription
amountTotal token amount traded (original position size).
purchasePriceWeighted average cost basis per token (accounts for DCA).
salePriceEffective average sale price per token (accounts for take-profit + final sale).
changePercentTotal return percentage from entry to exit (based on original cost).
profitLossUsd/SolTotal realized profit or loss (includes all take-profit sales + final close).
purchase/saleTimePrecise timestamps for entry and exit.
closeReasonWhy the position was sold (manual, stop_loss, stale_trade, or take_profit).
transactionIdsUUID links to the raw AgentTransaction records.

Positions with Take-Profit: The amount reflects the original position size, not the final remaining amount. The salePrice is the effective average across all sales (take-profit + final close), and profitLossSol includes profit from all partial sales.

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

Positions with Take-Profit Sales

For positions where take-profit executed partial sales before final close:

Effective Average Sale Price:

salePrice = totalSolReceived / totalTokensSold

Where totalSolReceived = sum of SOL from all take-profit sales + final close.

Total P/L:

profitLossSol = realizedProfitFromTakeProfits + profitFromFinalSale

Return % is calculated against the original cost basis, not the remaining position:

changePercent = (totalProfitSol / originalCostBasisSol) * 100

This ensures the historical record reflects the true performance of the entire trading cycle.


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.