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

# Token position

> 订阅单个钱包内某一代币仓位的实时 PnL。该房间会派发指定代币的 `tradeUpdate` 与 `balanceUpdate` 消息。



## AsyncAPI

````yaml cn/datastream/pnl.json tokenPosition
id: tokenPosition
title: Token position
description: 订阅单个钱包内某一代币仓位的实时 PnL。该房间会派发指定代币的 `tradeUpdate` 与 `balanceUpdate` 消息。
servers:
  - id: production
    protocol: wss
    host: datastream.solanatracker.io
    bindings: []
    variables:
      - id: apiKey
        description: Data API 订阅中的唯一 API 密钥
        allowedValues: []
        examples: []
address: /
parameters: []
bindings: []
operations:
  - &ref_1
    id: subscribeToPnlTokenPosition
    title: Subscribe to pnl token position
    description: Subscribe to token position PnL
    type: receive
    messages:
      - &ref_6
        id: joinPnlTokenPosition
        payload:
          - name: joinPnlTokenPosition
            description: Subscribe to Token Position PnL
            type: object
            properties:
              - name: type
                type: string
                description: join
                required: true
              - name: room
                type: string
                description: PnL token position room (pnl:{walletAddress}:{tokenAddress})
                required: true
        headers: []
        jsonPayloadSchema:
          type: object
          required:
            - type
            - room
          properties:
            type:
              type: string
              const: join
              default: join
              x-parser-schema-id: <anonymous-schema-2>
            room:
              type: string
              pattern: ^pnl:[A-Za-z0-9]{32,44}:[A-Za-z0-9]{32,44}$
              description: PnL token position room (pnl:{walletAddress}:{tokenAddress})
              default: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
              x-parser-schema-id: <anonymous-schema-3>
          examples:
            - type: join
              room: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
          x-parser-schema-id: <anonymous-schema-1>
        title: Join pnl token position
        description: Subscribe to Token Position PnL
        example: |-
          {
            "type": "join",
            "room": "pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: joinPnlTokenPosition
          - id: x-parser-message-name
            value: joinPnlTokenPosition
    bindings: []
    extensions: &ref_0
      - id: x-displayName
        value: Token Position PnL
      - id: x-codeSamples
        value: &ref_11
          - lang: typescript
            label: SDK
            source: >
              import { Datastream } from '@solana-tracker/data-api';


              const dataStream = new Datastream({
                wsUrl: 'wss://datastream.solanatracker.io/YOUR_API_KEY',
              });


              await dataStream.connect();


              const sub =
              dataStream.subscribe.pnl.position('FbMxP3GVq8TQ36nbYgx4NP9iygMpwAwFWJwW81ioCiSF',
              '6p6xgHyF7AeE6TZkSmFsko444wqoP15icUSqi2jfGiPN').on((data) => {
                console.log(data);
              });


              // sub.unsubscribe();
      - id: x-parser-unique-object-id
        value: tokenPosition
  - &ref_3
    id: receivePnlTokenPositionConfirmation
    title: Receive pnl token position confirmation
    description: Receive token position PnL confirmation
    type: send
    messages:
      - &ref_8
        id: pnlTokenPositionJoined
        payload:
          - name: pnlTokenPositionJoined
            description: Token position PnL subscription confirmed
            type: object
            properties:
              - name: type
                type: string
                description: joined
                required: false
              - name: room
                type: string
                required: false
        headers: []
        jsonPayloadSchema:
          type: object
          properties:
            type:
              type: string
              const: joined
              x-parser-schema-id: <anonymous-schema-5>
            room:
              type: string
              x-parser-schema-id: <anonymous-schema-6>
          examples:
            - type: joined
              room: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
          x-parser-schema-id: <anonymous-schema-4>
        title: Pnl token position joined
        description: Token position PnL subscription confirmed
        example: |-
          {
            "type": "joined",
            "room": "pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: pnlTokenPositionJoined
          - id: x-parser-message-name
            value: pnlTokenPositionJoined
    bindings: []
    extensions: *ref_0
  - &ref_4
    id: receivePnlTradeUpdate
    title: Receive pnl trade update
    description: Receive trade update for token position
    type: send
    messages:
      - &ref_9
        id: pnlTradeUpdateMessage
        payload:
          - name: pnlTradeUpdateMessage
            description: Trade update notification
            type: object
            properties:
              - name: type
                type: string
                description: message
                required: false
              - name: room
                type: string
                description: The PnL room identifier (pnl:{wallet}:{token})
                required: false
              - name: data
                type: object
                description: >-
                  Emitted when a buy or sell trade is executed for the tracked
                  token position. Contains the full trade history summary
                  including cost basis, realized and unrealized PnL, and running
                  totals.
                required: false
                properties:
                  - name: type
                    type: string
                    description: Message type identifier
                    required: false
                  - name: wallet
                    type: string
                    description: The Solana wallet address being tracked
                    required: false
                  - name: token
                    type: string
                    description: The token mint address for this position
                    required: false
                  - name: averageBuyAmountUsd
                    type: number
                    description: Average USD amount per buy transaction
                    required: false
                  - name: averageSellAmountUsd
                    type: number
                    description: Average USD amount per sell transaction
                    required: false
                  - name: avgCostPerToken
                    type: number
                    description: Weighted average cost per token across all buys
                    required: false
                  - name: buyCount
                    type: integer
                    description: Total number of buy transactions
                    required: false
                  - name: currentBalance
                    type: number
                    description: Current token balance held by the wallet
                    required: false
                  - name: currentPrice
                    type: number
                    description: Current market price of the token in USD
                    required: false
                  - name: currentValue
                    type: number
                    description: >-
                      Current USD value of the position (currentBalance ×
                      currentPrice)
                    required: false
                  - name: firstBuyTime
                    type: integer
                    description: Unix timestamp (ms) of the first buy transaction
                    required: false
                  - name: firstTradeTime
                    type: integer
                    description: Unix timestamp (ms) of the first trade (buy or sell)
                    required: false
                  - name: holdingCostBasis
                    type: number
                    description: Total USD cost basis of tokens currently held
                    required: false
                  - name: lastBuyTime
                    type: integer
                    description: Unix timestamp (ms) of the most recent buy transaction
                    required: false
                  - name: lastTradeTime
                    type: integer
                    description: Unix timestamp (ms) of the most recent trade
                    required: false
                  - name: proceeds
                    type: number
                    description: Total USD proceeds from all sell transactions
                    required: false
                  - name: realizedPnl
                    type: number
                    description: >-
                      Realized profit or loss from completed (sold) trades in
                      USD
                    required: false
                  - name: sellCount
                    type: integer
                    description: Total number of sell transactions
                    required: false
                  - name: soldCostBasis
                    type: number
                    description: Total USD cost basis of tokens that have been sold
                    required: false
                  - name: totalBought
                    type: number
                    description: Total number of tokens purchased across all buys
                    required: false
                  - name: totalBuyUsd
                    type: number
                    description: Total USD spent on buy transactions
                    required: false
                  - name: totalSellUsd
                    type: number
                    description: Total USD received from sell transactions
                    required: false
                  - name: totalSold
                    type: number
                    description: Total number of tokens sold across all sells
                    required: false
                  - name: totalTransactions
                    type: integer
                    description: Total number of transactions (buys + sells)
                    required: false
                  - name: unrealizedPnl
                    type: number
                    description: >-
                      Unrealized profit or loss on the current position in USD
                      (currentValue − holdingCostBasis)
                    required: false
        headers: []
        jsonPayloadSchema:
          type: object
          properties:
            type:
              type: string
              const: message
              x-parser-schema-id: <anonymous-schema-8>
            room:
              type: string
              description: The PnL room identifier (pnl:{wallet}:{token})
              x-parser-schema-id: <anonymous-schema-9>
            data:
              type: object
              description: >-
                Emitted when a buy or sell trade is executed for the tracked
                token position. Contains the full trade history summary
                including cost basis, realized and unrealized PnL, and running
                totals.
              properties:
                type:
                  type: string
                  const: tradeUpdate
                  description: Message type identifier
                  x-parser-schema-id: <anonymous-schema-10>
                wallet:
                  type: string
                  description: The Solana wallet address being tracked
                  x-parser-schema-id: <anonymous-schema-11>
                token:
                  type: string
                  description: The token mint address for this position
                  x-parser-schema-id: <anonymous-schema-12>
                averageBuyAmountUsd:
                  type: number
                  description: Average USD amount per buy transaction
                  x-parser-schema-id: <anonymous-schema-13>
                averageSellAmountUsd:
                  type: number
                  description: Average USD amount per sell transaction
                  x-parser-schema-id: <anonymous-schema-14>
                avgCostPerToken:
                  type: number
                  description: Weighted average cost per token across all buys
                  x-parser-schema-id: <anonymous-schema-15>
                buyCount:
                  type: integer
                  description: Total number of buy transactions
                  x-parser-schema-id: <anonymous-schema-16>
                currentBalance:
                  type: number
                  description: Current token balance held by the wallet
                  x-parser-schema-id: <anonymous-schema-17>
                currentPrice:
                  type: number
                  description: Current market price of the token in USD
                  x-parser-schema-id: <anonymous-schema-18>
                currentValue:
                  type: number
                  description: >-
                    Current USD value of the position (currentBalance ×
                    currentPrice)
                  x-parser-schema-id: <anonymous-schema-19>
                firstBuyTime:
                  type: integer
                  description: Unix timestamp (ms) of the first buy transaction
                  x-parser-schema-id: <anonymous-schema-20>
                firstTradeTime:
                  type: integer
                  description: Unix timestamp (ms) of the first trade (buy or sell)
                  x-parser-schema-id: <anonymous-schema-21>
                holdingCostBasis:
                  type: number
                  description: Total USD cost basis of tokens currently held
                  x-parser-schema-id: <anonymous-schema-22>
                lastBuyTime:
                  type: integer
                  description: Unix timestamp (ms) of the most recent buy transaction
                  x-parser-schema-id: <anonymous-schema-23>
                lastTradeTime:
                  type: integer
                  description: Unix timestamp (ms) of the most recent trade
                  x-parser-schema-id: <anonymous-schema-24>
                proceeds:
                  type: number
                  description: Total USD proceeds from all sell transactions
                  x-parser-schema-id: <anonymous-schema-25>
                realizedPnl:
                  type: number
                  description: Realized profit or loss from completed (sold) trades in USD
                  x-parser-schema-id: <anonymous-schema-26>
                sellCount:
                  type: integer
                  description: Total number of sell transactions
                  x-parser-schema-id: <anonymous-schema-27>
                soldCostBasis:
                  type: number
                  description: Total USD cost basis of tokens that have been sold
                  x-parser-schema-id: <anonymous-schema-28>
                totalBought:
                  type: number
                  description: Total number of tokens purchased across all buys
                  x-parser-schema-id: <anonymous-schema-29>
                totalBuyUsd:
                  type: number
                  description: Total USD spent on buy transactions
                  x-parser-schema-id: <anonymous-schema-30>
                totalSellUsd:
                  type: number
                  description: Total USD received from sell transactions
                  x-parser-schema-id: <anonymous-schema-31>
                totalSold:
                  type: number
                  description: Total number of tokens sold across all sells
                  x-parser-schema-id: <anonymous-schema-32>
                totalTransactions:
                  type: integer
                  description: Total number of transactions (buys + sells)
                  x-parser-schema-id: <anonymous-schema-33>
                unrealizedPnl:
                  type: number
                  description: >-
                    Unrealized profit or loss on the current position in USD
                    (currentValue − holdingCostBasis)
                  x-parser-schema-id: <anonymous-schema-34>
              x-parser-schema-id: TradeUpdate
          examples:
            - type: message
              data:
                type: tradeUpdate
                wallet: DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61
                token: A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
                averageBuyAmountUsd: 0.008573769236435
                averageSellAmountUsd: 0
                avgCostPerToken: 0.00008177548295
                buyCount: 1
                currentBalance: 104.845229
                currentPrice: 0.00009110065178426498
                currentValue: 0.009551468698371
                firstBuyTime: 1776706534041
                firstTradeTime: 1776706534041
                holdingCostBasis: 0.008573769236435
                lastBuyTime: 1776706534041
                lastTradeTime: 1776706534041
                proceeds: 0
                realizedPnl: 0
                sellCount: 0
                soldCostBasis: 0
                totalBought: 104.845229
                totalBuyUsd: 0.008573769236435
                totalSellUsd: 0
                totalSold: 0
                totalTransactions: 1
                unrealizedPnl: 0.000977699461936
              room: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
          x-parser-schema-id: <anonymous-schema-7>
        title: Pnl trade update message
        description: Trade update notification
        example: |-
          {
            "type": "message",
            "data": {
              "type": "tradeUpdate",
              "wallet": "DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61",
              "token": "A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump",
              "averageBuyAmountUsd": 0.008573769236435,
              "averageSellAmountUsd": 0,
              "avgCostPerToken": 0.00008177548295,
              "buyCount": 1,
              "currentBalance": 104.845229,
              "currentPrice": 0.00009110065178426498,
              "currentValue": 0.009551468698371,
              "firstBuyTime": 1776706534041,
              "firstTradeTime": 1776706534041,
              "holdingCostBasis": 0.008573769236435,
              "lastBuyTime": 1776706534041,
              "lastTradeTime": 1776706534041,
              "proceeds": 0,
              "realizedPnl": 0,
              "sellCount": 0,
              "soldCostBasis": 0,
              "totalBought": 104.845229,
              "totalBuyUsd": 0.008573769236435,
              "totalSellUsd": 0,
              "totalSold": 0,
              "totalTransactions": 1,
              "unrealizedPnl": 0.000977699461936
            },
            "room": "pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: pnlTradeUpdateMessage
          - id: x-parser-message-name
            value: pnlTradeUpdateMessage
    bindings: []
    extensions: *ref_0
  - &ref_5
    id: receivePnlBalanceUpdate
    title: Receive pnl balance update
    description: Receive balance update for token position
    type: send
    messages:
      - &ref_10
        id: pnlBalanceUpdateMessage
        payload:
          - name: pnlBalanceUpdateMessage
            description: Balance update notification
            type: object
            properties:
              - name: type
                type: string
                description: message
                required: false
              - name: room
                type: string
                description: The PnL room identifier (pnl:{wallet}:{token})
                required: false
              - name: data
                type: object
                description: >-
                  Emitted when the position value changes due to token price
                  movement without a new trade. Contains current position
                  valuation and unrealized PnL.
                required: false
                properties:
                  - name: type
                    type: string
                    description: Message type identifier
                    required: false
                  - name: wallet
                    type: string
                    description: The Solana wallet address being tracked
                    required: false
                  - name: token
                    type: string
                    description: The token mint address for this position
                    required: false
                  - name: avgCostPerToken
                    type: number
                    description: Weighted average cost per token across all buys
                    required: false
                  - name: currentBalance
                    type: number
                    description: Current token balance held by the wallet
                    required: false
                  - name: currentPrice
                    type: number
                    description: Current market price of the token in USD
                    required: false
                  - name: currentValue
                    type: number
                    description: >-
                      Current USD value of the position (currentBalance ×
                      currentPrice)
                    required: false
                  - name: holdingCostBasis
                    type: number
                    description: Total USD cost basis of tokens currently held
                    required: false
                  - name: unrealizedPnl
                    type: number
                    description: >-
                      Unrealized profit or loss on the current position in USD
                      (currentValue − holdingCostBasis)
                    required: false
        headers: []
        jsonPayloadSchema:
          type: object
          properties:
            type:
              type: string
              const: message
              x-parser-schema-id: <anonymous-schema-36>
            room:
              type: string
              description: The PnL room identifier (pnl:{wallet}:{token})
              x-parser-schema-id: <anonymous-schema-37>
            data:
              type: object
              description: >-
                Emitted when the position value changes due to token price
                movement without a new trade. Contains current position
                valuation and unrealized PnL.
              properties:
                type:
                  type: string
                  const: balanceUpdate
                  description: Message type identifier
                  x-parser-schema-id: <anonymous-schema-38>
                wallet:
                  type: string
                  description: The Solana wallet address being tracked
                  x-parser-schema-id: <anonymous-schema-39>
                token:
                  type: string
                  description: The token mint address for this position
                  x-parser-schema-id: <anonymous-schema-40>
                avgCostPerToken:
                  type: number
                  description: Weighted average cost per token across all buys
                  x-parser-schema-id: <anonymous-schema-41>
                currentBalance:
                  type: number
                  description: Current token balance held by the wallet
                  x-parser-schema-id: <anonymous-schema-42>
                currentPrice:
                  type: number
                  description: Current market price of the token in USD
                  x-parser-schema-id: <anonymous-schema-43>
                currentValue:
                  type: number
                  description: >-
                    Current USD value of the position (currentBalance ×
                    currentPrice)
                  x-parser-schema-id: <anonymous-schema-44>
                holdingCostBasis:
                  type: number
                  description: Total USD cost basis of tokens currently held
                  x-parser-schema-id: <anonymous-schema-45>
                unrealizedPnl:
                  type: number
                  description: >-
                    Unrealized profit or loss on the current position in USD
                    (currentValue − holdingCostBasis)
                  x-parser-schema-id: <anonymous-schema-46>
              x-parser-schema-id: BalanceUpdatePnl
          examples:
            - type: message
              data:
                type: balanceUpdate
                wallet: DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61
                token: A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
                avgCostPerToken: 0.00008351931949024704
                currentBalance: 205.389603
                currentPrice: 0.00009110065178426498
                currentValue: 0.018711126703011425
                holdingCostBasis: 0.017153999872932
                unrealizedPnl: 0.0015571268300794243
              room: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
          x-parser-schema-id: <anonymous-schema-35>
        title: Pnl balance update message
        description: Balance update notification
        example: |-
          {
            "type": "message",
            "data": {
              "type": "balanceUpdate",
              "wallet": "DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61",
              "token": "A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump",
              "avgCostPerToken": 0.00008351931949024704,
              "currentBalance": 205.389603,
              "currentPrice": 0.00009110065178426498,
              "currentValue": 0.018711126703011425,
              "holdingCostBasis": 0.017153999872932,
              "unrealizedPnl": 0.0015571268300794243
            },
            "room": "pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: pnlBalanceUpdateMessage
          - id: x-parser-message-name
            value: pnlBalanceUpdateMessage
    bindings: []
    extensions: *ref_0
  - &ref_2
    id: unsubscribeFromPnlTokenPosition
    title: Unsubscribe from pnl token position
    description: Unsubscribe from token position PnL
    type: receive
    messages:
      - &ref_7
        id: leavePnlTokenPosition
        payload:
          - name: leavePnlTokenPosition
            description: Unsubscribe from token position PnL
            type: object
            properties:
              - name: type
                type: string
                description: leave
                required: true
              - name: room
                type: string
                required: true
        headers: []
        jsonPayloadSchema:
          type: object
          required:
            - type
            - room
          properties:
            type:
              type: string
              const: leave
              default: leave
              x-parser-schema-id: <anonymous-schema-48>
            room:
              type: string
              default: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
              x-parser-schema-id: <anonymous-schema-49>
          examples:
            - type: leave
              room: >-
                pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump
          x-parser-schema-id: <anonymous-schema-47>
        title: Leave pnl token position
        description: Unsubscribe from token position PnL
        example: |-
          {
            "type": "leave",
            "room": "pnl:DJqkHSmx9XosFpNqdi2DevtNgaf52oow4pFpJECv6D61:A7FbnhhkY2R6hZhkT2oexNNFQyGG7cBZJLWmC32spump"
          }
        bindings: []
        extensions:
          - id: x-parser-unique-object-id
            value: leavePnlTokenPosition
          - id: x-parser-message-name
            value: leavePnlTokenPosition
    bindings: []
    extensions: *ref_0
sendOperations:
  - *ref_1
  - *ref_2
receiveOperations:
  - *ref_3
  - *ref_4
  - *ref_5
sendMessages:
  - *ref_6
  - *ref_7
receiveMessages:
  - *ref_8
  - *ref_9
  - *ref_10
extensions:
  - id: x-displayName
    value: Token Position PnL
  - id: x-codeSamples
    value: *ref_11
  - id: x-parser-unique-object-id
    value: tokenPosition
securitySchemes:
  - id: apiKeyAuth
    name: apiKey
    type: httpApiKey
    description: >-
      API key for authentication. Include your Datastream key in the connection
      URL: wss://datastream.solanatracker.io/{DATASTREAM_KEY}
    in: header
    extensions: []

````