4 min read · May 29, 2023
--
· Table of Contents
∘ Introduction
∘ Prerequisites
∘ Setting Up the Environment
∘ Retrieving Crypto Data
∘ Storing the Data
∘ Conclusion
Introduction
This article explains the basic steps to collect crypto data for free using Cryptowatch API and Python. Cryptowatch is a popular platform that provides real-time market data, charting tools, and insights for various cryptocurrencies. It allows traders and enthusiasts to monitor price movements, track trading volumes, and analyze market trends across multiple exchanges.
Whether you’re a beginner developer or a tech enthusiast, this step-by-step guide will help you get started with fetching crypto data effectively. Alternatively, you can check the complete code on my GitHub page.
Prerequisites
Before we begin, make sure you have the following prerequisites in place:
- Python installed on your machine
- An active API key from Cryptowatch which you can obtain for FREE by following this guide.
- Basic knowledge of Python programming
Setting Up the Environment
To retrieve crypto data from Cryptowatch, we’ll be using the cryptowatch
Python library. Let's start by installing the library using pip:
Once installed, we can import the necessary libraries and set our API key:
Make sure to replace "YOUR_API_KEY"
with your actual API key obtained from Cryptowatch. If you do not wish to obtain the API key from Cryptowatch you can make an anonymous call with a certain rate limit.
Retrieving Crypto Data
With the environment set up, we can now proceed to retrieve the crypto data. We’ll be fetching data for specific assets from different exchanges. In this example, we’ll focus on two popular coins listed on Binance — BTC and ETH, and two coins traded on Bitfinex — XRD and SMR.
In the exchange_dict
, we define the assets we want to fetch data for. For each exchange, we specify a list of assets and the currency in which we want the data. You can change the list of assets that you wish to collect data for. Make sure that sure to verify the following:
- Exchange(s) where the asset is traded. You can check it per coin on a website such as coinranking.com.
- Whether Cryptowatch supports this exchange.
Next, we’ll iterate over the exchange_dict
and fetch the data for each asset. We’ll be using the cw.markets.get()
function provided by the cryptowatch
library to request the data in JSON format. We’ll make use of the try-except
clause to catch the error in case the data on an asset is unavailable.
In the above code snippet, we make an API call using cw.markets.get()
by specifying the exchange, asset, and currency. The response from the API is a JSON object that contains various details about the asset, including its price, volume, and percentage change.
We extract the required data from the JSON response by accessing the corresponding keys. The price
key holds information such as the last price, high price, and low price. The volume
key provides the trading volume, while the volume_quote
key represents the volume in the quoted currency. The change
key within price
gives us the percentage and absolute change in price.
Storing the Data
As an additional step to your analysis, you might want to store the retrieved crypto data. For this step, we’ll utilize a SQLite database. SQLite is a lightweight and easy-to-use database engine that doesn’t require a separate server process. We’ll create a table named crypto_data
with columns representing different data attributes.
In the code above, we establish a connection to the SQLite database named crypto_data.db
. If the table crypto_data
doesn't exist, we create it with the required columns.
To make use of the database functionality, we should modify the for-loop above as follows:
We prepare the data as a tuple, including the current date, asset, price information, percentage change, and volume details. The INSERT INTO
statement is used to insert the data into the crypto_data
table, and conn.commit()
ensures that the changes are saved in the database. Finally, we close the database connection with conn.close()
.
Conclusion
In this article, we explored how to retrieve crypto data from Cryptowatch using Python. By leveraging the cryptowatch
library, we were able to fetch real-time data for specific assets from different exchanges. We also learned how to store the retrieved data in a SQLite database for further analysis and monitoring.
Retrieving crypto data is a fundamental step for building applications, conducting research, or making informed investment decisions in the cryptocurrency space. With the knowledge gained from this article, you can now begin exploring the vast possibilities of working with crypto data in Python.
Remember to stay updated with the latest developments in the Cryptowatch API and consult their documentation for any changes or additional functionalities.
Happy coding and happy crypto data.