---
title: "Position history"
description: "List a sub-account's closed and partly closed positions."
---

Returns both fully closed positions and positions that are still open but have been partially reduced (`PARTIALLY_CLOSED`).

Results are ordered as follows: partially closed positions (most recently opened first), then fully closed positions (most recently closed first).

Partially closed positions are included only when all of the following are true:

- `start_time` is unset (partially closed positions have no close time)
- `end_time` is unset (partially closed positions have no close time)
- `cursor` is unset (they are only returned on the initial page)
- `status` is nil or includes `PARTIALLY_CLOSED`

Since these positions have no close time, query-row limits, as well as time-range and cursor-based pagination, do not apply to them.

Pagination works as follows:

- We perform a reverse chronological lookup by position-close time, starting from `end_time`. If `end_time` is not set, we start from the most recent data.
- The lookup is limited to `limit` records. If more data is requested, the response will contain a `next` cursor for you to query the next page.
- If a `cursor` is provided, it will be used to fetch results from that point onwards.
- Pagination will continue until the `start_time` is reached. If `start_time` is not set, pagination will continue as far back as our data retention policy allows.

| | |
|---|---|
| SDK | `client.position_history(...)` |
| HTTP | `POST https://trades.<domain>/full/v1/position_history`, or `/lite/v1/position_history` for [lite names](../../guides/environments.md#encoding-full-or-lite) (the SDKs' default) |
| WebSocket | method `v1/position_history` on `wss://trades.<domain>/ws/full`, or `/ws/lite` for lite names |
| Auth | Session required ([Authentication](../../guides/authentication.md)) |
| Retries | Read: the SDKs retry retryable errors ([rules](../../guides/errors-and-retries.md)) |
| Contract id | `position.position_history` (`RPCPositionHistoryV1`) |

## Request

Body type `ApiPositionHistoryRequest`. Fields the server fills are left out.

- `sub_account_id` · lite `sa` · string (uint64) · required — The sub-account ID to request for
- `start_time` · lite `st` · string (int64 unix ns) · optional, default `0` — Start of the close-time range in unix nanoseconds. If nil, defaults to no lower bound. Only positions with close_time >= start_time are returned. Does not apply to partially closed positions (they have no close time and will be excluded when this field is set)
- `end_time` · lite `et` · string (int64 unix ns) · optional, default `now()` — End of the close-time range in unix nanoseconds. If nil, defaults to now. Only positions with close_time \<= end_time are returned. Does not apply to partially closed positions (they have no close time and will be excluded when this field is set)
- `kind` · lite `k` · enum Kind[] · optional, default `all` — The kind filter to apply. If nil, this defaults to all kinds. Otherwise, only positions matching the filter will be returned
  Values: `PERPETUAL`, `FUTURE`, `CALL`, `PUT`, `SPOT_SWAP`, `STABLE_PERP` ([details](../enums.md#kind)).
- `base` · lite `b` · string[] · optional, default `all` — The base filter to apply. If nil, this defaults to all bases. Otherwise, only positions matching the filter will be returned
- `quote` · lite `q` · string[] · optional, default `all` — The quote filter to apply. If nil, this defaults to all quotes. Otherwise, only positions matching the filter will be returned
- `limit` · lite `l` · integer (uint32) · optional, default `500` — The limit to query for. Defaults to 500; Max 1000. Applies to fully closed positions only; limit excludes any matching partially-closed positions
- `cursor` · lite `c` · string · optional — The cursor to indicate when to start the next page query from. Partially closed positions are only returned on the initial page (when cursor is unset)
- `status` · lite `s` · enum PositionCloseStatus[] · optional, default `all` — The status filter to apply. If nil, this defaults to all statuses. Otherwise, only positions matching the filter will be returned
  Values: `CLOSED`, `LIQUIDATED`, `SETTLED`, `PARTIALLY_CLOSED`, `SPLIT_CLOSED` ([details](../enums.md#positionclosestatus)).
- `is_long` · lite `il` · bool · optional, default `false` — Set to true to filter for long positions. If both is_long and is_short are false (default), positions of both directions are returned
- `is_short` · lite `is` · bool · optional, default `false` — Set to true to filter for short positions. If both is_long and is_short are false (default), positions of both directions are returned
- `margin_type` · lite `mt` · enum PositionMarginType[] · optional, default `all` — The margin type filter to apply. If nil, this defaults to all margin types. Otherwise, only positions matching the filter will be returned
  Values: `ISOLATED`, `CROSS` ([details](../enums.md#positionmargintype)).

## Response

Body type `ApiPositionHistoryResponse` (the same object is the JSON-RPC `result` over WebSocket).

- `result` · lite `r` · object ApiPositionHistory[] · required — Position lifecycle records matching the request filters. Partially closed positions appear first (most recently opened first), followed by fully closed positions (most recently closed first)
- `result[].sub_account_id` · lite `sa` · string (uint64) · required — Sub-account ID which held this position
- `result[].instrument` · lite `i` · string (instrument name) · required — Asset this position was for
- `result[].open_time` · lite `ot` · string (int64 unix ns) · required — Timestamp of first trade that opened this lifecycle
- `result[].status` · lite `s` · enum PositionCloseStatus · required
  Values: `CLOSED`, `LIQUIDATED`, `SETTLED`, `PARTIALLY_CLOSED`, `SPLIT_CLOSED` ([details](../enums.md#positionclosestatus)).
- `result[].is_long` · lite `il` · bool · required — True if the closed position was long
- `result[].margin_type` · lite `mt` · enum PositionMarginType · required
  Values: `ISOLATED`, `CROSS` ([details](../enums.md#positionmargintype)).
- `result[].close_time` · lite `ct` · string (int64 unix ns) · optional — Timestamp when the position lifecycle ended. Omitted when status is `PARTIALLY_CLOSED`
- `result[].entry_price` · lite `ep` · string · required — Average entry price at 9 decimals
- `result[].exit_price` · lite `ep1` · string · required — Average exit price at 9 decimals
- `result[].position_close_mark_price` · lite `pc` · string · optional — Mark price at close. Omitted when status is `PARTIALLY_CLOSED`
- `result[].realized_pnl` · lite `rp` · string · required — Cumulative realized PnL in quote currency
- `result[].cumulative_fee` · lite `cf` · string · required — Cumulative fees in quote currency
- `result[].cumulative_realized_funding_payment` · lite `cr` · string · required — Cumulative realized funding payment in quote currency
- `result[].closed_volume_base` · lite `cv` · string · required — Sum of abs(reducingTradeSize) across all reducing trades
- `result[].closed_volume_quote` · lite `cv1` · string · required — Sum of abs(reducingTradeSize) * tradePrice across all reducing trades
- `result[].max_open_interest_base` · lite `mo` · string · required — Max absolute position size reached during lifecycle
- `result[].max_open_interest_quote` · lite `mo1` · string · required — Max abs(size) * entryVWAP reached during lifecycle
- `result[].cumulative_initial_margin` · lite `ci` · string · required — Sum of markPrice * abs(tradeSize) / leverage for position-increasing trades
- `result[].max_initial_margin` · lite `mi` · string · required — High-water mark of cumulativeInitialMargin during lifecycle
- `result[].leverage` · lite `l` · string · required — Leverage at time of close. When status is `PARTIALLY_CLOSED`, this is the current leverage
- `result[].unrealized_pnl` · lite `up` · string · optional — The unrealized PnL of the position, in the quote asset. Decimal string, e.g. "12.5".
  `unrealized_pnl = (mark_price - entry_price) * size` where `size` is signed (negative for short positions)
  Only present when status is `PARTIALLY_CLOSED`
- `result[].split_ratio_from` · lite `sr` · integer (int32) · optional — Stock split 'from' ratio (pre-split units); the position size scaled by to/from
  Only present when status is `SPLIT_CLOSED`. Example: `1` for a 1:4 split
- `result[].split_ratio_to` · lite `sr1` · integer (int32) · optional — Stock split 'to' ratio (post-split units)
  Only present when status is `SPLIT_CLOSED`. Example: `4` for a 1:4 split
- `result[].split_remaining_size` · lite `sr2` · string · optional — Signed position size. Decimal string, e.g. "0.01"; up to the instrument's base_decimals. immediately before the split
  Only present when status is `SPLIT_CLOSED`
- `next` · lite `n` · string · required — The cursor to indicate when to start the query from

## Errors

Errors come back as `{"code", "message", "status"}` (lite `{"c", "m", "s"}`) with the HTTP status. Codes this call can return, as code, name (HTTP status) ([all error codes](../errors.md)):

`1000` Unauthorized (401) · `1001` Forbidden (403) · `1002` InternalServer (500) · `1003` InvalidRequest (400) · `1006` RateLimit (429, retryable) · `1008` IPNotWhitelisted (401) · `1014` GatewayTimeout (504, retryable) · `1015` ServiceUnavailable (503, retryable) · `3007` NotApplicableForSpotInstrument (400)

## Example

```python
import os

from grvt import Grvt

with Grvt(
    env=os.environ.get("GRVT_ENV", "testnet"),
    api_key=os.environ["GRVT_API_KEY"],
) as client:
    resp = client.position_history(
        sub_account_id=os.environ["GRVT_SUB_ACCOUNT_ID"],
        limit=500,
    )
    print(resp)
```

Raw HTTP (set `GRVT_DOMAIN`, see [Environments](../../guides/environments.md); `GRVT_COOKIE` and `GRVT_ACCOUNT_ID` come from [login](../../guides/authentication.md)):

With full field names:

```sh
curl -sS "https://trades.$GRVT_DOMAIN/full/v1/position_history" \
  -H "Content-Type: application/json" \
  -H "Cookie: gravity=$GRVT_COOKIE" \
  -H "X-Grvt-Account-Id: $GRVT_ACCOUNT_ID" \
  --data @- <<EOF
{
  "sub_account_id": "$GRVT_SUB_ACCOUNT_ID",
  "limit": 500
}
EOF
```

With lite field names:

```sh
curl -sS "https://trades.$GRVT_DOMAIN/lite/v1/position_history" \
  -H "Content-Type: application/json" \
  -H "Cookie: gravity=$GRVT_COOKIE" \
  -H "X-Grvt-Account-Id: $GRVT_ACCOUNT_ID" \
  --data @- <<EOF
{
  "sa": "$GRVT_SUB_ACCOUNT_ID",
  "l": 500
}
EOF
```
