> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hotstuff.trade/llms.txt
> Use this file to discover all available pages before exploring further.

# positions

Subscribe to real-time user position updates. Account-specific channel.

## Example Request

```json theme={null}
{
  "jsonrpc": "2.0",
  "id": "1",
  "method": "subscribe",
  "params": {
    "channel": "positions",
    "user": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
  }
}
```

## Example Response

```json theme={null}
{
  "jsonrpc": "2.0",
  "method": "event",
  "params": {
    "channel": "positions@0x42C183edba036906447372a7c81Eb89D0B9f2175",
    "data": {
      "account": "0x42C183edba036906447372a7c81Eb89D0B9f2175",
      "instrument_id": 10,
      "instrument": "TSLA-PERP",
      "position_type": "oneWay",
      "old_position": {
        "size": 0.833,
        "entry_price": 420
      },
      "position_side_updated": "BOTH",
      "realized_pnl": 0,
      "legs": [
        {
          "side": "BOTH",
          "size": 0.833,
          "entry_price": 420,
          "leverage": {
            "type": "isolated",
            "value": 10,
            "margin": "53.477383"
          },
          "position_value": 349.86
        }
      ],
      "block_timestamp": 1770853898179
    }
  }
}
```

## WebSocket Client Examples

### Python

```python theme={null}
import websocket
import json

def on_message(ws, message):
    print(f"Received: {message}")

def on_open(ws):
    subscribe_msg = {
        "jsonrpc": "2.0",
        "id": "1",
        "method": "subscribe",
        "params": {
            "channel": "positions",
            "user": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
        }
    }
    ws.send(json.dumps(subscribe_msg))

ws = websocket.WebSocketApp("wss://api.hotstuff.trade/ws",
                          on_message=on_message,
                          on_open=on_open)
ws.run_forever()
```

### JavaScript

```javascript theme={null}
const ws = new WebSocket("wss://api.hotstuff.trade/ws");

ws.onopen = function () {
  const subscribeMsg = {
    jsonrpc: "2.0",
    id: "1",
    method: "subscribe",
    params: {
      channel: "positions",
      user: "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
    },
  };
  ws.send(JSON.stringify(subscribeMsg));
};

ws.onmessage = function (event) {
  console.log("Received:", event.data);
};
```


## AsyncAPI

````yaml asyncapi/ws-subscriptions.json positions
id: positions
title: Positions
description: ''
servers:
  - id: production
    protocol: wss
    host: api.hotstuff.trade/ws
    bindings: []
    variables: []
address: /
parameters: []
bindings: []
operations:
  - &ref_9
    id: positions_sendSubscribe
    title: Positions_send subscribe
    type: receive
    messages:
      - &ref_11
        id: a_subscribeRequest
        contentType: application/json
        payload:
          - name: Subscription request
            description: Send subscribe/unsubscribe request
            type: object
            properties:
              - name: jsonrpc
                type: string
                description: JSON-RPC version
                examples: &ref_0
                  - '2.0'
                required: true
              - name: id
                type: integer
                description: Client request identifier
                examples: &ref_1
                  - 1
                required: true
              - name: method
                type: string
                description: Subscription action
                enumValues:
                  - subscribe
                  - unsubscribe
                examples: &ref_2
                  - subscribe
                required: true
              - name: params
                type: object
                description: Channel subscription parameters
                required: true
                properties:
                  - name: channel
                    type: string
                    description: Must be `positions`
                    examples: &ref_3
                      - positions
                    required: true
                  - name: user
                    type: string
                    description: 'Example: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
                    examples: &ref_4
                      - '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
                    required: true
        headers: []
        jsonPayloadSchema:
          type: object
          properties:
            jsonrpc:
              type: string
              default: '2.0'
              examples: *ref_0
              description: JSON-RPC version
              x-parser-schema-id: <anonymous-schema-242>
            id:
              type: integer
              default: 1
              examples: *ref_1
              description: Client request identifier
              x-parser-schema-id: <anonymous-schema-243>
            method:
              type: string
              enum:
                - subscribe
                - unsubscribe
              default: subscribe
              examples: *ref_2
              description: Subscription action
              x-parser-schema-id: <anonymous-schema-244>
            params:
              type: object
              properties:
                channel:
                  type: string
                  description: Must be `positions`
                  default: positions
                  examples: *ref_3
                  x-parser-schema-id: <anonymous-schema-246>
                user:
                  type: string
                  description: 'Example: 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
                  default: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
                  examples: *ref_4
                  x-parser-schema-id: <anonymous-schema-247>
              required:
                - channel
                - user
              description: Channel subscription parameters
              x-parser-schema-id: <anonymous-schema-245>
          required:
            - jsonrpc
            - id
            - method
            - params
          x-parser-schema-id: <anonymous-schema-241>
        title: Subscription request
        description: Send subscribe/unsubscribe request
        example: |-
          {
            "jsonrpc": "2.0",
            "id": 1,
            "method": "subscribe",
            "params": {
              "channel": "positions",
              "user": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            }
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: a_subscribeRequest
    bindings: []
    extensions: &ref_8
      - id: x-parser-unique-object-id
        value: positions
  - &ref_10
    id: positions_receiveEvent
    title: Positions_receive event
    type: send
    messages:
      - &ref_12
        id: event
        contentType: application/json
        payload:
          - name: Channel event
            description: Server-pushed JSON-RPC event
            type: object
            properties:
              - name: jsonrpc
                type: string
                description: JSON-RPC version, must be `2.0`
                examples: &ref_5
                  - '2.0'
                required: false
              - name: method
                type: string
                description: '`event`'
                examples: &ref_6
                  - event
                required: false
              - name: params
                type: object
                required: false
                properties:
                  - name: channel
                    type: string
                    description: '`positions@<user>`'
                    examples: &ref_7
                      - positions@0x42C183edba036906447372a7c81Eb89D0B9f2175
                    required: false
                  - name: data
                    type: object
                    required: false
                    properties:
                      - name: account
                        type: string
                        required: false
                      - name: instrument_id
                        type: integer
                        required: false
                      - name: instrument
                        type: string
                        description: instrument name
                        required: false
                      - name: position_type
                        type: string
                        description: position type, one of `oneWay` or `hedge`
                        enumValues:
                          - oneWay
                          - hedge
                        required: false
                      - name: old_position
                        type: object
                        description: old position size and entry price
                        required: false
                        properties:
                          - name: size
                            type: number
                            required: true
                          - name: entry_price
                            type: number
                            required: true
                      - name: position_side_updated
                        type: string
                        description: position side updated
                        enumValues:
                          - BOTH
                          - LONG
                          - SHORT
                        required: false
                      - name: realized_pnl
                        type: integer
                        description: realized PnL
                        required: false
                      - name: legs
                        type: array
                        description: legs of the position
                        required: false
                        properties:
                          - name: side
                            type: string
                            description: >-
                              side of the position, one of `BOTH`, `LONG`, or
                              `SHORT`
                            enumValues:
                              - BOTH
                              - LONG
                              - SHORT
                            required: false
                          - name: size
                            type: number
                            description: position size
                            required: false
                          - name: entry_price
                            type: number
                            description: entry price
                            required: false
                          - name: leverage
                            type: object
                            required: false
                            properties:
                              - name: type
                                type: string
                                enumValues:
                                  - isolated
                                  - cross
                                required: true
                              - name: value
                                type: integer
                                description: leverage value
                                required: true
                              - name: margin
                                type: string
                                description: only present for isolated margin positions
                                required: true
                          - name: position_value
                            type: number
                            required: false
                      - name: block_timestamp
                        type: integer
                        required: false
        headers: []
        jsonPayloadSchema:
          type: object
          properties:
            jsonrpc:
              type: string
              description: JSON-RPC version, must be `2.0`
              examples: *ref_5
              x-parser-schema-id: <anonymous-schema-249>
            method:
              type: string
              description: '`event`'
              examples: *ref_6
              x-parser-schema-id: <anonymous-schema-250>
            params:
              type: object
              properties:
                channel:
                  type: string
                  description: '`positions@<user>`'
                  examples: *ref_7
                  x-parser-schema-id: <anonymous-schema-252>
                data:
                  type: object
                  properties:
                    account:
                      type: string
                      x-parser-schema-id: <anonymous-schema-254>
                    instrument_id:
                      type: integer
                      x-parser-schema-id: <anonymous-schema-255>
                    instrument:
                      type: string
                      description: instrument name
                      x-parser-schema-id: <anonymous-schema-256>
                    position_type:
                      type: string
                      enum:
                        - oneWay
                        - hedge
                      description: position type, one of `oneWay` or `hedge`
                      x-parser-schema-id: <anonymous-schema-257>
                    old_position:
                      type: object
                      description: old position size and entry price
                      properties:
                        size:
                          type: number
                          x-parser-schema-id: <anonymous-schema-259>
                        entry_price:
                          type: number
                          x-parser-schema-id: <anonymous-schema-260>
                      required:
                        - size
                        - entry_price
                      x-parser-schema-id: <anonymous-schema-258>
                    position_side_updated:
                      type: string
                      enum:
                        - BOTH
                        - LONG
                        - SHORT
                      description: position side updated
                      x-parser-schema-id: <anonymous-schema-261>
                    realized_pnl:
                      type: integer
                      description: realized PnL
                      x-parser-schema-id: <anonymous-schema-262>
                    legs:
                      type: array
                      description: legs of the position
                      items:
                        type: object
                        properties:
                          side:
                            type: string
                            enum:
                              - BOTH
                              - LONG
                              - SHORT
                            description: >-
                              side of the position, one of `BOTH`, `LONG`, or
                              `SHORT`
                            x-parser-schema-id: <anonymous-schema-265>
                          size:
                            type: number
                            description: position size
                            x-parser-schema-id: <anonymous-schema-266>
                          entry_price:
                            type: number
                            description: entry price
                            x-parser-schema-id: <anonymous-schema-267>
                          leverage:
                            type: object
                            properties:
                              type:
                                type: string
                                enum:
                                  - isolated
                                  - cross
                                x-parser-schema-id: <anonymous-schema-269>
                              value:
                                type: integer
                                description: leverage value
                                x-parser-schema-id: <anonymous-schema-270>
                              margin:
                                type: string
                                description: only present for isolated margin positions
                                x-parser-schema-id: <anonymous-schema-271>
                            required:
                              - type
                              - value
                              - margin
                            x-parser-schema-id: <anonymous-schema-268>
                          position_value:
                            type: number
                            x-parser-schema-id: <anonymous-schema-272>
                        x-parser-schema-id: <anonymous-schema-264>
                      x-parser-schema-id: <anonymous-schema-263>
                    block_timestamp:
                      type: integer
                      x-parser-schema-id: <anonymous-schema-273>
                  x-parser-schema-id: <anonymous-schema-253>
              x-parser-schema-id: <anonymous-schema-251>
          x-parser-schema-id: <anonymous-schema-248>
        title: Channel event
        description: Server-pushed JSON-RPC event
        example: |-
          {
            "jsonrpc": "2.0",
            "method": "event",
            "params": {
              "channel": "positions@0x42C183edba036906447372a7c81Eb89D0B9f2175",
              "data": {
                "account": "0x42C183edba036906447372a7c81Eb89D0B9f2175",
                "instrument_id": 10,
                "instrument": "TSLA-PERP",
                "position_type": "oneWay",
                "old_position": {
                  "size": 0.833,
                  "entry_price": 420
                },
                "position_side_updated": "BOTH",
                "realized_pnl": 0,
                "legs": [
                  {
                    "side": "BOTH",
                    "size": 0.833,
                    "entry_price": 420,
                    "leverage": {
                      "type": "isolated",
                      "value": 10,
                      "margin": "53.477383"
                    },
                    "position_value": 349.86
                  }
                ],
                "block_timestamp": 1770853898179
              }
            }
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: event
    bindings: []
    extensions: *ref_8
sendOperations:
  - *ref_9
receiveOperations:
  - *ref_10
sendMessages:
  - *ref_11
receiveMessages:
  - *ref_12
extensions:
  - id: x-parser-unique-object-id
    value: positions
securitySchemes: []

````

This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.