advanced
+350 XP

Portfolio & P&L

Master position tracking, unrealized and realized P&L calculation, settlement outcomes, and redemption mechanics on DeepBook Predict. Build functions that decode positions, compute profit and loss, determine settlement payouts, and summarize portfolio state.

Lesson Syllabus

Position Tracking
📊

PositionData Interface

Every open position on DeepBook Predict is represented by a PositionData object. It stores the oracle it belongs to, the expiry timestamp, the lower and higher strike bounds (which encode direction via sentinel values), the quantity in micro-DUSDC units, and the entry price scaled by FLOAT_SCALING.

🔄

Position Lifecycle

A position moves through distinct lifecycle stages from the moment it is minted until the user claims their payout. Understanding this lifecycle is essential for building accurate portfolio views.

🧭

Direction Detection

Since direction is encoded via sentinel values in lower_strike and higher_strike, you need utility functions to decode it. getPositionDirection checks which bound is a sentinel. getPositionStrike extracts the meaningful strike value from the non-sentinel bound.

P&L Calculation
📈

Unrealized P&L

Unrealized P&L tells a trader how much they would gain or lose if they exited their position right now. For a live position, you decode the direction and strike, look up the current SVI price for that strike, invert for DOWN positions (price = 1 - sviPrice), and compute (currentPrice - entryPrice) * quantity.

📋

PositionPnL Interface

The PositionPnL interface provides a fully decoded, human-readable view of a position's profit and loss. It converts all on-chain scaled values to standard numbers and includes both absolute and percentage P&L.

💰

Realized P&L from Trades

Realized P&L comes from completed round trips: you minted (bought) at one price and redeemed (sold) at another. The TradeData interface captures each side of the trade, and realized P&L is the sum of (redeem_price - mint_price) * quantity across all matched pairs.

Settlement & Redemption
🎯

Settlement Outcomes

When an oracle settles, each position type resolves to either 1 DUSDC (in-the-money) or 0 DUSDC (out-of-the-money). The outcome depends on the direction and whether the settlement price meets the strike condition.

💸

Redeem Transactions

There are two redemption paths: redeem_live for active positions (early exit at the current SVI price minus a fee) and redeem_permissionless for settled positions (anyone can trigger, zero fee, deterministic 0-or-1 payout).

📊

Portfolio Summary

The ManagerSummaryData interface aggregates all of a manager's financial data into a single object: trading balance, realized and unrealized P&L, total account value, and count of open positions. This is the data model behind every portfolio dashboard.