Kraken WebSocket API Documentation
The Kraken WebSocket API provides a real-time data stream for cryptocurrency trading, offering users the ability to receive updates on market data, trades, and account information. This documentation will guide you through the various aspects of using the Kraken WebSocket API, including connecting to the WebSocket, subscribing to channels, and interpreting the data received.
Connecting to the WebSocket
To begin using the Kraken WebSocket API, you need to establish a WebSocket connection. This can be done using various libraries and programming languages, such as JavaScript's WebSocket object, Python's websockets
library, or others.
The WebSocket URL for Kraken is:
arduinowss://ws.kraken.com
Subscribing to Channels
Once connected, you can subscribe to various channels to receive different types of data. The primary channels include:
- Ticker: Provides real-time price updates for a given asset pair.
- Trades: Provides information about recent trades for an asset pair.
- Book: Provides order book updates for an asset pair.
- Spread: Provides information about the spread between the best bid and ask prices for an asset pair.
- OHLC: Provides historical OHLC (Open, High, Low, Close) data for an asset pair.
To subscribe to a channel, you need to send a subscription request to the WebSocket server. The general format of a subscription message is:
json{ "event": "subscribe", "channel": "channel_name", "pair": ["asset_pair"] }
For example, to subscribe to the ticker channel for the BTC/USD pair, you would send:
json{ "event": "subscribe", "channel": "ticker", "pair": ["XBT/USD"] }
Data Format
Each channel provides data in a specific format. Below are examples of the data formats for different channels:
Ticker Channel:
json{ "channel": "ticker", "pair": "XBT/USD", "last_trade_price": "23456.78", "high": "24000.00", "low": "22000.00", "volume": "100.0", "timestamp": 1629823200 }
Trades Channel:
json{ "channel": "trades", "pair": "XBT/USD", "trades": [ { "price": "23456.78", "volume": "0.1", "time": 1629823200, "side": "buy" }, { "price": "23457.00", "volume": "0.2", "time": 1629823300, "side": "sell" } ] }
Book Channel:
json{ "channel": "book", "pair": "XBT/USD", "book": { "bids": [ ["23450.00", "1.0", 1629823200], ["23445.00", "2.0", 1629823300] ], "asks": [ ["23460.00", "1.5", 1629823200], ["23465.00", "1.0", 1629823300] ] } }
Spread Channel:
json{ "channel": "spread", "pair": "XBT/USD", "spread": [ ["0.50", 1629823200], ["0.45", 1629823300] ] }
OHLC Channel:
json{ "channel": "ohlc", "pair": "XBT/USD", "ohlc": [ { "time": 1629823200, "open": "23400.00", "high": "23500.00", "low": "23300.00", "close": "23450.00", "volume": "100.0" }, { "time": 1629823300, "open": "23450.00", "high": "23600.00", "low": "23400.00", "close": "23500.00", "volume": "150.0" } ] }
Handling Errors
The Kraken WebSocket API may send error messages in response to invalid subscription requests or other issues. Error messages typically include an "error" field with details about the problem.
Example Error Message:
json{ "event": "error", "errorMessage": "Invalid channel" }
Closing the Connection
When you are finished using the WebSocket, you should close the connection gracefully to ensure all resources are properly released. This can be done by sending a close frame or simply closing the WebSocket client.
Conclusion
The Kraken WebSocket API is a powerful tool for accessing real-time cryptocurrency data. By following this documentation, you should be able to connect to the WebSocket, subscribe to various channels, and handle the data and errors appropriately.
Hot Comments
No Comments Yet