README
¶
Kraken Provider
Overview
The Kraken provider is used to fetch the ticker price from the Kraken websocket API.
General Considerations
- TLS with SNI (Server Name Indication) is required in order to establish a Kraken WebSockets API connection.
- All messages sent and received via WebSockets are encoded in JSON format
- All decimal fields (including timestamps) are quoted to preserve precision.
- Timestamps should not be considered unique and not be considered as aliases for transaction IDs. Also, the granularity of timestamps is not representative of transaction rates.
- Please use REST API endpoint AssetPairs to fetch the list of pairs which can be subscribed via WebSockets API. For example, field 'wsname' gives the supported pairs name which can be used to subscribe.
- Recommended reconnection behaviour is to (1) attempt reconnection instantly up to a handful of times if the websocket is dropped randomly during normal operation but (2) after maintenance or extended downtime, attempt to reconnect no more quickly than once every 5 seconds. There is no advantage to reconnecting more rapidly after maintenance during cancel_only mode.
To check all available pairs, you can use the following REST API call:
curl "https://api.kraken.com/0/public/Assets"
Documentation
¶
Index ¶
- Constants
- Variables
- func NewWebSocketDataHandler(logger *zap.Logger, ws config.WebSocketConfig) (types.PriceWebSocketDataHandler, error)
- type BaseMessage
- type Channel
- type Event
- type HeartbeatResponseMessage
- type Status
- type SubscribeRequestMessage
- type SubscribeResponseMessage
- type Subscription
- type SystemStatusResponseMessage
- type TickerData
- type TickerResponseMessage
- type WebSocketHandler
- func (h *WebSocketHandler) Copy() types.PriceWebSocketDataHandler
- func (h *WebSocketHandler) CreateMessages(tickers []types.ProviderTicker) ([]handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) HandleMessage(message []byte) (types.PriceResponse, []handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)
- func (h *WebSocketHandler) NewSubscribeRequestMessage(instruments []string) ([]handlers.WebsocketEncodedMessage, error)
Constants ¶
const ( // ExpectedTickerResponseMessageLength is the expected length of the ticker // response message. ExpectedTickerResponseMessageLength = 4 // ChannelIDIndex is the index of the channel ID in the ticker response // message. ChannelIDIndex = iota - 1 // TickerDataIndex is the index of the ticker data in the ticker response // message. TickerDataIndex // ChannelNameIndex is the index of the channel name in the ticker response // message. ChannelNameIndex // PairIndex is the index of the asset pair in the ticker response message. PairIndex )
const ( // TodayPriceIndex is the index of the today's price in the ticker's // VolumeWeightedAveragePrice array. TodayPriceIndex = 0 // ExpectedVolumeWeightedAveragePriceLength is the expected length of the ticker's // VolumeWeightedAveragePrice array. ExpectedVolumeWeightedAveragePriceLength = 2 )
const ( // Name is the name of the Kraken provider. Name = "kraken_ws" // URL is the production websocket URL for Kraken. URL = "wss://ws.kraken.com" // URL_BETA is the demo websocket URL for Kraken. URL_BETA = "wss://beta-ws.kraken.com" )
Variables ¶
var DefaultWebSocketConfig = config.WebSocketConfig{ Name: Name, Enabled: true, MaxBufferSize: 1000, ReconnectionTimeout: 10 * time.Second, PostConnectionTimeout: config.DefaultPostConnectionTimeout, Endpoints: []config.Endpoint{{URL: URL}}, ReadBufferSize: config.DefaultReadBufferSize, WriteBufferSize: config.DefaultWriteBufferSize, HandshakeTimeout: config.DefaultHandshakeTimeout, EnableCompression: config.DefaultEnableCompression, ReadTimeout: config.DefaultReadTimeout, WriteTimeout: config.DefaultWriteTimeout, PingInterval: config.DefaultPingInterval, WriteInterval: config.DefaultWriteInterval, MaxReadErrorCount: config.DefaultMaxReadErrorCount, MaxSubscriptionsPerConnection: config.DefaultMaxSubscriptionsPerConnection, MaxSubscriptionsPerBatch: config.DefaultMaxSubscriptionsPerBatch, }
DefaultWebSocketConfig is the default configuration for the Kraken Websocket.
Functions ¶
func NewWebSocketDataHandler ¶
func NewWebSocketDataHandler( logger *zap.Logger, ws config.WebSocketConfig, ) (types.PriceWebSocketDataHandler, error)
NewWebSocketDataHandler returns a new Kraken PriceWebSocketDataHandler.
Types ¶
type BaseMessage ¶
type BaseMessage struct { // Event is the event name that is sent to the client. Event string `json:"event"` }
BaseMessage is the template used to determine the type of message that is received from the server.
type Channel ¶
type Channel string
Channel correspond to the various channels that are available for subscription.
const ( // TickerChannel is the channel name for the ticker channel. // // https://docs.kraken.com/websockets/#message-ticker TickerChannel Channel = "ticker" )
type Event ¶
type Event string
Event correspond to the various message types that are sent to the client.
const ( // SystemStatusEvent is the event name that is sent to the client when the // connection is first established. The status notifies the client of any // outages or planned maintenance. // // https://docs.kraken.com/websockets/#message-systemStatus SystemStatusEvent Event = "systemStatus" // HeartbeatEvent is the event name that is sent to the client periodically // to ensure that the connection is still alive. // // https://docs.kraken.com/websockets/#message-heartbeat HeartbeatEvent Event = "heartbeat" // SubscriptionStatusEvent is the event name that is used to send a subscribe // message to the server. // // https://docs.kraken.com/websockets/#message-subscribe SubscriptionStatusEvent Event = "subscriptionStatus" // SubscribeEvent is the event name that is used to send a subscribe message // to the server. // // https://docs.kraken.com/websockets/#message-subscribe SubscribeEvent Event = "subscribe" )
type HeartbeatResponseMessage ¶
type HeartbeatResponseMessage struct { // Event is the event name that is sent to the client periodically to ensure // that the connection is still alive. Event string `json:"event"` }
HeartbeatResponseMessage is the message that is sent to the client periodically to ensure that the connection is still alive. In particular, the server will send a heartbeat if no subscription traffic is received within a 60-second period.
{ "event": "heartbeat", }
type Status ¶
type Status string
Status correspond to the various status types that are sent to the client.
const ( // OnlineStatus is the status that is sent to the client when the connection // is first established. The status notifies the client that the connection // is online. OnlineStatus Status = "online" // MaintenanceStatus is the status that is sent to the client when the // connection is first established. The status notifies the client that the // connection is online, but that there is ongoing maintenance. MaintenanceStatus Status = "maintenance" // SubscribedStatus is the status that is sent to the client when the server // has received the subscription request. SubscribedStatus Status = "subscribed" // ErrorStatus is the status that is sent to the client when the server has // received the subscription request. ErrorStatus Status = "error" )
type SubscribeRequestMessage ¶
type SubscribeRequestMessage struct { // Event is the event name that is sent to the server to subscribe to a // channel. Event string `json:"event"` // Pair is the asset pair to subscribe to. Pair []string `json:"pair"` // Subscription is the subscription details. Subscription Subscription `json:"subscription"` }
SubscribeRequestMessage is the message that is sent to the server to subscribe to a channel.
{ "event": "subscribe", "pair": [ "XBT/USD", "XBT/EUR" ], "subscription": { "name": "ticker" } }
type SubscribeResponseMessage ¶
type SubscribeResponseMessage struct { // ChannelID is the channel ID. ChannelID uint64 `json:"channelID"` // ChannelName is the channel name. ChannelName string `json:"channelName"` // Event is the event name that is sent to the client when the server has // received the subscription request. Event string `json:"event"` // Pair is the asset pair that was subscribed to. Pair string `json:"pair"` // Status is the status that is sent to the client when the server has // received the subscription request. Status string `json:"status"` // Subscription is the subscription details. Subscription Subscription `json:"subscription"` // ErrorMessage is the error message that is sent to the client when the // server has received the subscription request. ErrorMessage string `json:"errorMessage"` }
SubscribeResponseMessage is the message that is sent to the client when the server has received the subscription request.
Good response:
{ "channelID": 10001, "channelName": "ticker", "event": "subscriptionStatus", "pair": "XBT/EUR", "status": "subscribed", "subscription": { "name": "ticker" } }
Bad response:
{ "errorMessage": "Subscription depth not supported", "event": "subscriptionStatus", "pair": "XBT/USD", "status": "error", "subscription": { "depth": 42, "name": "book" } }
ref: https://docs.kraken.com/websockets/#message-subscriptionStatus
type Subscription ¶
type Subscription struct { // Name is the name of the subscription. Name string `json:"name"` }
Subscription is the subscription details.
type SystemStatusResponseMessage ¶
type SystemStatusResponseMessage struct { // ConnectionID is the unique identifier for the connection. ConnectionID uint64 `json:"connectionID"` // Event is the event name that is sent to the client when the connection is // first established. Event string `json:"event"` // Status is the status that is sent to the client when the connection is // first established. Status string `json:"status"` // Version is the version of the API. Version string `json:"version"` }
SystemStatusResponseMessage is the message that is sent to the client when the connection is first established.
{ "connectionID": 8628615390848610000, "event": "systemStatus", "status": "online", "version": "1.0.0" }
ref: https://docs.kraken.com/websockets/#message-systemStatus
type TickerData ¶
type TickerData struct { // VolumeWeightedAveragePrice is the volume weighted average price. VolumeWeightedAveragePrice []string `json:"p"` }
TickerData is the ticker data.
type TickerResponseMessage ¶
type TickerResponseMessage struct { // ChannelID is the channel ID. ChannelID int // TickerData is the ticker data corresponding to the asset pair. TickerData TickerData // ChannelName is the channel name. ChannelName string // Pair is the asset pair that was subscribed to. Pair string }
TickerResponseMessage is the message that is sent to the client when the server has a price update for the subscribed asset pair. This is specific to the ticker subscription.
[ 0, // ChannelID { "a": [ // Ask array "5525.40000", // Best ask price 1, // Whole lot volume "1.000" // Lot volume ], "b": [ // Bid array "5525.10000", // Best bid price 1, // Whole lot volume "1.000" // Lot volume ], "c": [ // Close array "5525.10000", // Price "0.00398963" // Lot volume ], "h": [ // High price array "5783.00000", // Today "5783.00000" // Last 24 hours ], "l": [ // Low price array "5505.00000", // Today "5505.00000" // Last 24 hours ], "o": [ // Open price array "5760.70000", // Today "5763.40000" // Last 24 hours ], "p": [ // Volume weighted average price array <- This is the value we want "5631.44067", // Value Today "5653.78939" // Value 24h ], "t": [ // Number of trades array 11493, // Today 16267 // Last 24 hours ], "v": [ // Volume array "2634.11501494", // Value Today "3591.17907851" // Value 24h ] }, "ticker", // Channel name "XBT/USD" // Asset pair
]
ref: https://docs.kraken.com/websockets/#message-ticker
func DecodeTickerResponseMessage ¶
func DecodeTickerResponseMessage(message []byte) (TickerResponseMessage, error)
DecodeTickerResponseMessage decodes a ticker response message.
type WebSocketHandler ¶
type WebSocketHandler struct {
// contains filtered or unexported fields
}
WebSocketDataHandler implements the WebSocketDataHandler interface. This is used to handle messages received from the Kraken websocket API.
func (*WebSocketHandler) Copy ¶
func (h *WebSocketHandler) Copy() types.PriceWebSocketDataHandler
Copy is used to create a copy of the WebSocketHandler.
func (*WebSocketHandler) CreateMessages ¶
func (h *WebSocketHandler) CreateMessages( tickers []types.ProviderTicker, ) ([]handlers.WebsocketEncodedMessage, error)
CreateMessages is used to create a message to send to the data provider. This is used to subscribe to the given tickers. This is called when the connection to the data provider is first established.
func (*WebSocketHandler) HandleMessage ¶
func (h *WebSocketHandler) HandleMessage( message []byte, ) (types.PriceResponse, []handlers.WebsocketEncodedMessage, error)
HandleMessage is used to handle a message received from the data provider. There are two types of messages that are handled by this function:
- Price update messages. This is used to update the price of the given ticker. This is formatted as a JSON array.
- General response messages. This is used to check if the subscription request was successful, heartbeats, and system status updates.
func (*WebSocketHandler) HeartBeatMessages ¶
func (h *WebSocketHandler) HeartBeatMessages() ([]handlers.WebsocketEncodedMessage, error)
HeartBeatMessages is not used for Kraken.
func (*WebSocketHandler) NewSubscribeRequestMessage ¶
func (h *WebSocketHandler) NewSubscribeRequestMessage( instruments []string, ) ([]handlers.WebsocketEncodedMessage, error)
NewSubscribeRequestMessage returns a new SubscribeRequestMessage with the given asset pairs.