Position history
List a sub-account's closed and partly closed positions.
On this page
Write a small program that calls `position.position_history` on Grvt {environment} {using}.
Target environment: {environment} (chain ID {chainId}). Recorded signing cases keep their original environment.
{executionGuidance}
Read this first: {origin}/bundle/reference/rpc/position_history.md (~61 KB, plain Markdown: this page plus the AGENTS.md rules, Authentication and sessions, Errors and retries, Python SDK, Environments).
Fetch other pages from the index at {origin}/llms.txt only as you need them. {origin}/downloads/contract.json is a machine-readable contract for tools, not for reading.
Rules: follow AGENTS.md; do not send requests without the user asking you to; read credentials from environment variables; never retry writes automatically; show me the code and how to run it. 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_timeis unset (partially closed positions have no close time)end_timeis unset (partially closed positions have no close time)cursoris unset (they are only returned on the initial page)statusis nil or includesPARTIALLY_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. Ifend_timeis not set, we start from the most recent data. - The lookup is limited to
limitrecords. If more data is requested, the response will contain anextcursor for you to query the next page. - If a
cursoris provided, it will be used to fetch results from that point onwards. - Pagination will continue until the
start_timeis reached. Ifstart_timeis not set, pagination will continue as far back as our data retention policy allows.
| SDK | client. |
|---|---|
| HTTP | POST https:/, or / for lite names (the SDKs' default) |
| WebSocket | method v1/position_history on wss:/, or /ws/lite for lite names |
| Auth | Session required (Authentication) |
| Retries | Read: the SDKs retry retryable errors (rules) |
| Contract id | position. (RPCPositionHistoryV1) |
Request
Body type ApiPositionHistoryRequest. Fields the server fills are left out.
- The sub-account ID to request for
- 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 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)
- The kind filter to apply. If nil, this defaults to all kinds. Otherwise, only positions matching the filter will be returned
6 values
- The base filter to apply. If nil, this defaults to all bases. Otherwise, only positions matching the filter will be returned
- The quote filter to apply. If nil, this defaults to all quotes. Otherwise, only positions matching the filter will be returned
- The limit to query for. Defaults to 500; Max 1000. Applies to fully closed positions only; limit excludes any matching partially-closed positions
- 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)
- The status filter to apply. If nil, this defaults to all statuses. Otherwise, only positions matching the filter will be returned
5 values
- Set to true to filter for long positions. If both is_long and is_short are false (default), positions of both directions are returned
- Set to true to filter for short positions. If both is_long and is_short are false (default), positions of both directions are returned
- 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
Response
Body type ApiPositionHistoryResponse (the same object is the JSON-RPC result over WebSocket).
- 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 ID which held this position
- Asset this position was for
- Timestamp of first trade that opened this lifecycle
- True if the closed position was long
- Timestamp when the position lifecycle ended. Omitted when status is
PARTIALLY_CLOSED - Average entry price at 9 decimals
- Average exit price at 9 decimals
- Mark price at close. Omitted when status is
PARTIALLY_CLOSED - Cumulative realized PnL in quote currency
- Cumulative fees in quote currency
- Cumulative realized funding payment in quote currency
- Sum of abs(reducingTradeSize) across all reducing trades
- Sum of abs(reducingTradeSize) * tradePrice across all reducing trades
- Max absolute position size reached during lifecycle
- Max abs(size) * entryVWAP reached during lifecycle
- Sum of markPrice * abs(tradeSize) / leverage for position-increasing trades
- High-water mark of cumulativeInitialMargin during lifecycle
- Leverage at time of close. When status is
PARTIALLY_CLOSED, this is the current leverage - The unrealized PnL of the position, in the quote asset. Decimal string, e.g. "12.5".
unrealized_pnl = (mark_price - entry_price) * sizewheresizeis signed (negative for short positions) Only present when status isPARTIALLY_CLOSED - Stock split 'from' ratio (pre-split units); the position size scaled by to/from Only present when status is
SPLIT_CLOSED. Example:1for a 1:4 split - Stock split 'to' ratio (post-split units) Only present when status is
SPLIT_CLOSED. Example:4for a 1:4 split - 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
- 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
}
EOFWith 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