Our WebSocket API v1 supports trading via the addOrder and cancelOrder endpoints. The addOrder endpoint is used for placing new orders and the cancelOrder endpoint is used for cancelling existing (open) orders.
Both addOrder and cancelOrder are private endpoints, which means that they can only be accessed via an authenticated WebSocket connection.
An initial connection should be made to the authenticated WebSocket URL wss://ws-auth.kraken.com/ , which can then be kept open indefinitely while orders are placed and cancelled. A single WebSocket connection is designed to support multiple requests, so it is not necessary (or recommended) to connect/disconnect for each call to the trading endpoints.
WebSocket authentication
A WebSocket authentication token can be retrieved via the REST API GetWebSocketsToken endpoint.
Once a token has been retrieved, a connection can be made to the private WebSocket URL wss://ws-auth.kraken.com/ and the addOrder and cancelOrder endpoints can then be used indefinitely (the token will not expire while it is being used to place and cancel orders).
Note that the API key that is used to retrieve the token must have both the Modify Orders and Cancel/Close Orders permissions, otherwise a permission denied error will be received from the WebSocket API.
Placing an order
Once an authenticated WebSocket connection has been established, an order can be placed by sending a message like the following:
- {"event":"addOrder", "token":"nT9n23/7oLdq+bNHNMNJ4VvF1hiHhjTnPOaCMiB1GJ0", "pair":"XBT/USD", "type":"buy", "ordertype":"limit", "price":"9857", "volume":"0.002", "leverage":"5", "userref":"8874657"}
A response will then be received indicating the status of the request (whether the order was accepted or rejected). For an order that was accepted and placed successfully, the response will indicate a status of ok and be similar to the following:
- {"descr":"buy 0.00200000 XBTUSD @ limit 9857.0 with 5:1 leverage","event":"addOrderStatus","status":"ok","txid":"OPOUJF-BWKCL-FG5DQL"}
For an order that was rejected and not placed, the response will indicate a status of error, along with an error message indicating the reason for the rejection (invalid arguments, insufficient funds, etc.), such as the following rejection for an order that did not meet the minimum order size:
- {"errorMessage":"EOrder:Order minimum not met","event":"addOrderStatus","status":"error"}
Cancelling an order
Once an authenticated WebSocket connection has been established (or preferably an existing connection reused), an order can be cancelled by sending a message like the following:
- {"event":"cancelOrder", "token":"s74Ajl83it0dNNKcF072NJ5wDmLTGIceaCbV96eWbFg", "txid":["OJZ3DI-IU6QC-ME7TVE"]}
A response will then be received indicating the status of the request (whether the cancellation was accepted or rejected). For an order that was cancelled successfully, the response will indicate a status of ok and be similar to the following:
- {"event":"cancelOrderStatus","status":"ok"}
For a cancellation request that was rejected, the response will indicate a status of error, along with an error message indicating the reason for the rejection (invalid order ID, etc.), such as the following rejection for a request to cancel an order that had already been cancelled:
- {"errorMessage":"EOrder:Unknown order","event":"cancelOrderStatus","status":"error"}
Note that unlike the REST API CancelOrder endpoint, it is possible to specify multiple order IDs in a single WebSocket cancellation message. The order IDs to be cancelled should be included as separate entries within the txid array, as follows:
- {"event":"cancelOrder", "token":"c4JjTNHSurKmWDsnrGaCxIuCOa4FurCZK2ppktvT2Yo", "txid":["OLVCRZ-DNKJI-VRTZKY","O67X6E-PBGSR-GU23R5","OECCDC-T2R4V-52CR7T"]}