Skip to main content

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)
Parameters:
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.
Returns: BN - Position PnL in QUOTE_PRECISION (1e6)

calculateBaseAssetValue

Calculates the market value of closing the entire position.
Parameters:
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.
Returns: BN - Base asset value in QUOTE_PRECISION (1e6)

calculateClaimablePnl

Calculates the claimable (settleable) PnL for a position, accounting for pool limitations.
Parameters:
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.
Returns: BN - Claimable PnL in QUOTE_PRECISION (1e6)

Funding Calculations

calculateUnsettledFundingPnl

Calculates the unsettled funding payment PnL for a position.
Parameters:
PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s perpetual position.
Returns: BN - Unsettled funding PnL in QUOTE_PRECISION (1e6)

calculateFeesAndFundingPnl

Returns total fees and funding PnL for a position.
Parameters:
PerpMarketAccount
required
The perpetual market account.
PerpPosition
required
The user’s perpetual position.
boolean
default:"true"
Whether to include unsettled funding in the result.
Returns: BN - Total fees and funding PnL in QUOTE_PRECISION (1e6)

Price Calculations

calculateBreakEvenPrice

Calculates the break-even price for a position (entry price + fees + funding).
Parameters:
PerpPosition
required
The user’s perpetual position.
Returns: BN - Break-even price in PRICE_PRECISION (1e6)

calculateEntryPrice

Calculates the average entry price for a position.
Parameters:
PerpPosition
required
The user’s perpetual position.
Returns: BN - Average entry price in PRICE_PRECISION (1e6)

calculateCostBasis

Calculates the cost basis of a position.
Parameters:
PerpPosition
required
The user’s perpetual position.
boolean
default:"false"
Whether to include settled PnL in the calculation.
Returns: BN - Cost basis in PRICE_PRECISION (1e10)

Position State Functions

findDirectionToClose

Determines the direction needed to close a position.
Parameters:
PerpPosition
required
The user’s perpetual position.
Returns: PositionDirection - Direction to close the position

positionCurrentDirection

Returns the current direction of a position.
Parameters:
PerpPosition
required
The user’s perpetual position.
Returns: PositionDirection - Current position direction

positionIsAvailable

Checks if a position slot is available (no position or orders).
Parameters:
PerpPosition
required
The perpetual position to check.
Returns: boolean - True if position slot is available

positionIsBeingLiquidated

Checks if a position is currently being liquidated.
Parameters:
PerpPosition
required
The perpetual position to check.
Returns: boolean - True if position has BeingLiquidated or Bankruptcy flag set

isEmptyPosition

Checks if a position is empty (no base asset and no open orders).
Parameters:
PerpPosition
required
The user’s perpetual position.
Returns: boolean - True if position is empty

hasOpenOrders

Checks if a position has any open orders.
Parameters:
PerpPosition
required
The position to check.
Returns: boolean - True if position has open orders

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.
BN
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 position
  • PositionFlag.BeingLiquidated (2) - Position is being liquidated
  • PositionFlag.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