usePositionStream provides the current position details of the account, and includes some useful data calculations for showing positions, including:

  • Position value
  • Position liquidation price
  • Position margin
  • Unrealized PnL
  • Unsettled PnL
  • Aggregated position data

If you do not want to use the data provided by usePositionStream, you may use the @orderly.network/perp package. This package implements all position related calculations. See reference.

Position list

usePositionStream returns an array, with the first item of the arry as follows:

interface Position {
  rows: positionsRows,
  aggregated: formatedPositions?.[1] ?? {},
  totalCollateral:number,
  totalValue:number,
  totalUnrealizedROI:number,
}

Position

Aggregated position data

Usually, the UI will display some aggregated position data, for instance the total position value, total collateral, and the total unrealized ROI. These data are all provided by usePositionStream.

interface Aggregated {
  unrealPnL: number;
  notional: number;
  unsettledPnL: number;
}

Position info

The data returned by Orderly positions API also includes the following Position info. One can retrieve these data through the second element of the usePositionStream array response:

interface PositionInfo {
  margin_ratio: number;
  initial_margin_ratio: number;
  maintenance_margin_ratio: number;
  open_margin_ratio: number;
  current_margin_ratio_with_orders: number;
  initial_margin_ratio_with_orders: number;
  maintenance_margin_ratio_with_orders: number;
  total_collateral_value: number;
  free_collateral: number;
  rows: Position[];
  total_pnl_24_h: number;
}

Position management

Close position

To close a position, just place a reduce-only order with the opposite position of the open position. For example, if the open position is a long, close the position by placing a SELL order of the same size.

Positions can be closed by using useOrderEntry. For more details, please see orders