Overview
The position management module provides functions for calculating position metrics, unrealized PnL, funding payments, and other position-related data.Position Calculation Functions
calculatePositionPNL
Calculates the unrealized PnL for a perpetual position. Formula: BaseAssetAmount × (Avg Exit Price - Avg Entry Price)PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s perpetual position.
boolean
default:"false"
Whether to include unrealized funding payment PnL in the result.
OraclePriceData
required
Oracle price data containing the current price.
calculateBaseAssetValue
Calculates the market value of closing the entire position.PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s position.
MMOraclePriceData
required
Market maker oracle price data.
boolean
default:"true"
Whether to apply AMM spread to the calculation.
boolean
default:"false"
Whether to skip AMM updates before calculation.
BN
Latest slot for accurate AMM state.
calculateClaimablePnl
Calculates the claimable (settleable) PnL for a position, accounting for pool limitations.PerpMarketAccount
required
The perpetual market account.
SpotMarketAccount
required
The quote spot market account (usually USDC).
PerpPosition
required
The user’s perpetual position.
OraclePriceData
required
Oracle price data.
Funding Calculations
calculateUnsettledFundingPnl
Calculates the unsettled funding payment PnL for a position.PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s perpetual position.
calculateFeesAndFundingPnl
Returns total fees and funding PnL for a position.PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s perpetual position.
boolean
default:"true"
Whether to include unsettled funding in the result.
Price Calculations
calculateBreakEvenPrice
Calculates the break-even price for a position (entry price + fees + funding).PerpPosition
required
The user’s perpetual position.
calculateEntryPrice
Calculates the average entry price for a position.PerpPosition
required
The user’s perpetual position.
calculateCostBasis
Calculates the cost basis of a position.PerpPosition
required
The user’s perpetual position.
boolean
default:"false"
Whether to include settled PnL in the calculation.
Position State Functions
findDirectionToClose
Determines the direction needed to close a position.PerpPosition
required
The user’s perpetual position.
positionCurrentDirection
Returns the current direction of a position.PerpPosition
required
The user’s perpetual position.
positionIsAvailable
Checks if a position slot is available (no position or orders).PerpPosition
required
The perpetual position to check.
positionIsBeingLiquidated
Checks if a position is currently being liquidated.PerpPosition
required
The perpetual position to check.
isEmptyPosition
Checks if a position is empty (no base asset and no open orders).PerpPosition
required
The user’s perpetual position.
hasOpenOrders
Checks if a position has any open orders.PerpPosition
required
The position to check.
PerpPosition Type
The PerpPosition type contains all data for a perpetual position:BN
Current base asset amount held (positive for long, negative for short).
BN
Last cumulative funding rate when position was updated.
number
Index of the perpetual market.
BN
Current quote asset amount (cost basis).
BN
Quote amount at entry (before fees and funding).
BN
Quote amount including fees and funding (break-even point).
number
Number of open orders for this position.
BN
Total size of open bid orders.
BN
Total size of open ask orders.
BN
Settled PnL for this position.
LP shares if user is providing liquidity.
number
Maximum margin ratio for this position.
BN
Last quote asset amount per LP share.
number
Base asset per LP (i8).
number
Position status flags.PositionFlag values:
PositionFlag.IsolatedPosition(1) - Isolated margin positionPositionFlag.BeingLiquidated(2) - Position is being liquidatedPositionFlag.Bankruptcy(4) - Position is bankrupt
BN
Scaled balance for isolated positions.
Example: Position Dashboard
See Also
- OrderParams - Configure and place orders
- DLOB - Decentralized Limit Order Book