Developers

Position history

List a sub-account's closed and partly closed positions.

On this page

URLs and generated code samples on this page: Testnet (chain ID 326). Other code on this page doesn't change: it stays on testnet until you switch it in your code. Tutorials, signing cases and live tools always use testnet.

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.
SDKclient.position_history(...)
HTTPPOST https://trades.testnet.grvt.io/full/v1/position_history, or /lite/v1/position_history for lite names (the SDKs' default)
WebSocketmethod v1/position_history on wss://trades.testnet.grvt.io/ws/full, or /ws/lite for lite names
AuthSession required (Authentication)
RetriesRead: the SDKs retry retryable errors (rules)
Contract idposition.position_history (RPCPositionHistoryV1)

Request

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

  • sub_account_idlite name sastring (uint64)required#
    The sub-account ID to request for
  • start_timelite name ststring (int64 unix ns)optionaldefault 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_timelite name etstring (int64 unix ns)optionaldefault 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)
  • kindlite name karray<enum Kind>optionaldefault all#
    The kind filter to apply. If nil, this defaults to all kinds. Otherwise, only positions matching the filter will be returned
    6 values
    PERPETUAL, FUTURE, CALL, PUT, SPOT_SWAP, STABLE_PERP (details).
  • baselite name barray<string>optionaldefault all#
    The base filter to apply. If nil, this defaults to all bases. Otherwise, only positions matching the filter will be returned
  • quotelite name qarray<string>optionaldefault all#
    The quote filter to apply. If nil, this defaults to all quotes. Otherwise, only positions matching the filter will be returned
  • limitlite name linteger (uint32)optionaldefault 500#
    The limit to query for. Defaults to 500; Max 1000. Applies to fully closed positions only; limit excludes any matching partially-closed positions
  • cursorlite name cstringoptional#
    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)
  • statuslite name sarray<enum PositionCloseStatus>optionaldefault all#
    The status filter to apply. If nil, this defaults to all statuses. Otherwise, only positions matching the filter will be returned
    5 values
    CLOSED, LIQUIDATED, SETTLED, PARTIALLY_CLOSED, SPLIT_CLOSED (details).
  • is_longlite name ilbooloptionaldefault 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_shortlite name isbooloptionaldefault 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_typelite name mtarray<enum PositionMarginType>optionaldefault all#
    The margin type filter to apply. If nil, this defaults to all margin types. Otherwise, only positions matching the filter will be returned
    2 values
    ISOLATED, CROSS (details).

Response

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

  • resultlite name rarray<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)
    24 fields of ApiPositionHistory
    • sub_account_idlite name sastring (uint64)required#
      Sub-account ID which held this position
    • instrumentlite name istring (instrument name)required#
      Asset this position was for
    • open_timelite name otstring (int64 unix ns)required#
      Timestamp of first trade that opened this lifecycle
    • statuslite name senum PositionCloseStatusrequired#
    • is_longlite name ilboolrequired#
      True if the closed position was long
    • margin_typelite name mtenum PositionMarginTyperequired#
    • close_timelite name ctstring (int64 unix ns)optional#
      Timestamp when the position lifecycle ended. Omitted when status is PARTIALLY_CLOSED
    • entry_pricelite name epstringrequired#
      Average entry price at 9 decimals
    • exit_pricelite name ep1stringrequired#
      Average exit price at 9 decimals
    • position_close_mark_pricelite name pcstringoptional#
      Mark price at close. Omitted when status is PARTIALLY_CLOSED
    • realized_pnllite name rpstringrequired#
      Cumulative realized PnL in quote currency
    • cumulative_feelite name cfstringrequired#
      Cumulative fees in quote currency
    • cumulative_realized_funding_paymentlite name crstringrequired#
      Cumulative realized funding payment in quote currency
    • closed_volume_baselite name cvstringrequired#
      Sum of abs(reducingTradeSize) across all reducing trades
    • closed_volume_quotelite name cv1stringrequired#
      Sum of abs(reducingTradeSize) * tradePrice across all reducing trades
    • max_open_interest_baselite name mostringrequired#
      Max absolute position size reached during lifecycle
    • max_open_interest_quotelite name mo1stringrequired#
      Max abs(size) * entryVWAP reached during lifecycle
    • cumulative_initial_marginlite name cistringrequired#
      Sum of markPrice * abs(tradeSize) / leverage for position-increasing trades
    • max_initial_marginlite name mistringrequired#
      High-water mark of cumulativeInitialMargin during lifecycle
    • leveragelite name lstringrequired#
      Leverage at time of close. When status is PARTIALLY_CLOSED, this is the current leverage
    • unrealized_pnllite name upstringoptional#
      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
    • split_ratio_fromlite name srinteger (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
    • split_ratio_tolite name sr1integer (int32)optional#
      Stock split 'to' ratio (post-split units) Only present when status is SPLIT_CLOSED. Example: 4 for a 1:4 split
    • split_remaining_sizelite name sr2stringoptional#
      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
  • nextlite name nstringrequired#
    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 (all error codes):

3007NotApplicableForSpotInstrument400
API is not applicable for spot instruments
Plus 8 errors any call can return
1000Unauthorized401
You need to authenticate prior to using this functionality
1001Forbidden403
You are not authorized to access this functionality
1002InternalServer500
Internal Server Error
1003InvalidRequest400
Request could not be processed due to malformed syntax
1006RateLimit429retryable
You have surpassed the allocated rate limit for your tier
1008IPNotWhitelisted401
Your IP has not been whitelisted for access
1014GatewayTimeout504retryable
Upstream data service timed out, please try again
1015ServiceUnavailable503retryable
Upstream data service is temporarily unavailable, please try again

Example

Example code
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; GRVT_COOKIE and GRVT_ACCOUNT_ID come from login):

With full field names:

curl -sS "https://trades.testnet.grvt.io/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:

curl -sS "https://trades.testnet.grvt.io/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
Build request
esc

↑ ↓ move↵ open⇧↵ open .mdTry an error code like 2002

Keyboard shortcuts

⌘ K
Search the docs
/
Search
l
Next code language (Python → TypeScript → Go → Rust → curl → WebSocket)
j / k
Next / previous page
t
Light / dark theme
?
This help
Esc
Close dialogs and drawers