TradingEntry Price & PnL

Entry Price and PnL

GX Exchange tracks entry prices and computes profit and loss (PnL) for all perpetual positions using integer arithmetic to ensure exact, deterministic results across all validators.

Entry Price

The entry price is the volume-weighted average price at which a position was opened or increased.

Initial Entry

When a new position is opened:

entry_price = fill_price

Averaging on Increase

When adding to an existing position in the same direction:

new_entry_price = (old_size * old_entry_price + fill_size * fill_price) / (old_size + fill_size)

Example: Hold 10 BTC-USD long at $60,000, buy 5 more at $62,000:

new_entry = (10 * 60,000 + 5 * 62,000) / (10 + 5)
          = (600,000 + 310,000) / 15
          = 910,000 / 15
          = $60,666.67

Reduction and Close

When reducing a position (selling part of a long, or buying back part of a short), the entry price does not change. The PnL on the closed portion is realized at the difference between the fill price and the entry price.

Position Flip

When an order flips a position from long to short (or vice versa):

  1. The existing position is fully closed at the fill price, realizing PnL
  2. A new position is opened in the opposite direction at the fill price
  3. The new entry price equals the fill price

Unrealized PnL

Unrealized PnL (uPnL) is the profit or loss on the open position at the current mark price:

unrealized_pnl = position_size * (mark_price - entry_price)
  • Long position (size > 0): Positive uPnL when mark > entry
  • Short position (size < 0): Positive uPnL when mark < entry (because size is negative)

Example

PositionSizeEntry PriceMark PriceUnrealized PnL
Long BTC+10$60,000$62,000+10 * ($62,000 - $60,000) = +$20,000
Short ETH-50$3,000$2,800-50 * ($2,800 - $3,000) = +$10,000
Long SOL+100$150$140+100 * ($140 - $150) = -$1,000

Realized PnL

Realized PnL (rPnL) is locked in when a position is partially or fully closed:

realized_pnl = closed_size * (fill_price - entry_price)

Realized PnL is immediately added to the account’s collateral balance.

Example

Close 5 of the 10 BTC long (entry $60,000) at $65,000:

realized_pnl = 5 * ($65,000 - $60,000) = +$25,000
remaining position: 5 BTC long at $60,000 entry (unchanged)

Funding PnL

In addition to trade PnL, each position tracks cumulative funding payments:

total_pnl = realized_trade_pnl + unrealized_trade_pnl + funding_pnl

Funding PnL is accumulated in the position’s funding_accumulated field and reflects the net funding payments made or received over the position’s lifetime.

Equity

Account equity combines collateral with all unrealized PnL:

equity = collateral + sum(position_size * (mark_price - entry_price) for all positions)

Equity is the primary metric for:

  • Margin health checks (initial and maintenance)
  • Liquidation triggers
  • ADL ranking
  • Free collateral computation

Integer Arithmetic

All PnL calculations use integer arithmetic with atomic units (e.g., 6-decimal USDC). This ensures:

  • Deterministic results across all validators (no floating-point divergence)
  • Exact reproducibility of state transitions
  • Auditable, verifiable computations