Plus+
Python
Connecting to Kraken API via Websocket to Capture Tick Data in Real Time using Python

Most serious algorithmic trading developers need tick data to process and backtest trading strategies. Due to the sheer size of the data hose, it would not make a lot of sense to try to write these files to Excel. We will expand on an article where we stored Binance OHLC data into a database and ultimately do the same here with our data capture.
There is a fundamental data capture difference in our methodology in this article than in previous code examples. Most of our examples to date are pulling historical data, and therefore can rely on a REST API to deliver responses to specific requests. But to do this in real time would mean that you need to make requests to the REST API nearly continuously! This is where the websocket connection to the Kraken API service comes in. The biggest difference is that the websocket will push the data to you (in comparison to you pulling the data from the REST API). We will store 7 columns into our database: 1) Unix 2) Date 3) Trade Price 4) Volume 5) Side 6) Order Type 7) Pair Name
Of course, every line of code is fully commented so that all levels of Python coders can pick up and modify, enhance or understand the code right away. Be warned though: this set of code will be a little more advanced than some of the previous scripts we have made available. We will use Threading to open websocket connections to the endpoint for BTC/USD. We will receive messages and store them in a Queue until we capture a pre-defined amount, and then save those to the SQLite database. You can think of the "Queue'" as a place where we store pieces of a dataframe until it reaches a certain size. Again, we think using a database in this instance is most efficient due to the size of the data and then ease of utilizing it via SQL later on. This code is immediately executable and you will start capturing data right away.
There is a fundamental data capture difference in our methodology in this article than in previous code examples. Most of our examples to date are pulling historical data, and therefore can rely on a REST API to deliver responses to specific requests. But to do this in real time would mean that you need to make requests to the REST API nearly continuously! This is where the websocket connection to the Kraken API service comes in. The biggest difference is that the websocket will push the data to you (in comparison to you pulling the data from the REST API). We will store 7 columns into our database: 1) Unix 2) Date 3) Trade Price 4) Volume 5) Side 6) Order Type 7) Pair Name
Of course, every line of code is fully commented so that all levels of Python coders can pick up and modify, enhance or understand the code right away. Be warned though: this set of code will be a little more advanced than some of the previous scripts we have made available. We will use Threading to open websocket connections to the endpoint for BTC/USD. We will receive messages and store them in a Queue until we capture a pre-defined amount, and then save those to the SQLite database. You can think of the "Queue'" as a place where we store pieces of a dataframe until it reaches a certain size. Again, we think using a database in this instance is most efficient due to the size of the data and then ease of utilizing it via SQL later on. This code is immediately executable and you will start capturing data right away.
Plus+ membership required
Create a Plus+ account to view the live codebase.
This article has a fully working, notebook-ready code block. Sign up for Plus+ to access it and the rest of the 20+ premium articles.
Subscribe to Plus+ Browse Plus+ articles