Loading...

[PLUS] 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.
This is a premium post. Create Plus+ Account to view the live, working codebase for this article.




Notice: Information contained herein is not and should not be construed as an offer, solicitation, or recommendation to buy or sell securities. The information has been obtained from sources we believe to be reliable; however no guarantee is made or implied with respect to its accuracy, timeliness, or completeness. Author does not own the any crypto currency discussed. The information and content are subject to change without notice. CryptoDataDownload and its affiliates do not provide investment, tax, legal or accounting advice.

This material has been prepared for informational purposes only and is the opinion of the author, and is not intended to provide, and should not be relied on for, investment, tax, legal, accounting advice. You should consult your own investment, tax, legal and accounting advisors before engaging in any transaction. All content published by CryptoDataDownload is not an endorsement whatsoever. CryptoDataDownload was not compensated to submit this article. Please also visit our Privacy policy; disclaimer; and terms and conditions page for further information.