Data Scientists can be involved in “algorithmic trading” projects. Currently, since we are entering the Web3 era, we come across terms such as Blockchains, Smart Contracts, DeFi, DApps, DAO, Yield Farming and so on. All these things that we mentioned above are based on blockchain technology and cryptocurrencies. As a result, it seems that cryptocurrencies do not serve only as a medium to transfer money, but also as a promising revolutionary technology with many applications. Many strong players like Microsoft, IMB etc have started investing in Web3 and this interest impacts the cryptocurrency market. More investors and funds enter the cryptocurrency market and there is a higher demand for Data Scientists to run technical analysis, statistical analysis, time series model, algorithmic trading and so on. As we know, every analysis starts with the data and in this tutorial, we will show you how to get the cryptocurrency data from Kraken API using Python for free without needing to create an account.
How to Get the Trading Pairs
The first thing that we need to do is to get the available trading pairs and then filter them by keeping the ones that we want to consider in our analysis. Let’s say that we want to consider all the cryptocurrencies over Euro:
import pandas as pd import requests resp = requests.get('https://api.kraken.com/0/public/AssetPairs') resp = resp.json() euro_pairs = [] for pair in resp['result']: if pair.endswith('EUR'): euro_pairs.append(pair) euro_pairs
We get 125 crypto tickers len(euro_pairs)
:
['1INCHEUR',
'AAVEEUR',
'ACAEUR',
'ADAEUR',
'AIREUR',
'AKTEUR',
'ALGOEUR',
'ALICEEUR',
'ANKREUR',
'ANTEUR',
'APEEUR',
'ASTREUR',
'ATLASEUR',
'ATOMEUR',
'AUDIOEUR',
'AVAXEUR',
...
'YFIEUR',
'YGGEUR',
'ZRXEUR']
We can get an output of a specific asset pair by entering the pair variable in the URL. Let’s get the asset pair ADA/EURO.
resp = requests.get('https://api.kraken.com/0/public/AssetPairs?pair=ADAEUR') print(resp.json())
Output:
{'error': [],
'result': {'ADAEUR': {'altname': 'ADAEUR',
'wsname': 'ADA/EUR',
'aclass_base': 'currency',
'base': 'ADA',
'aclass_quote': 'currency',
'quote': 'ZEUR',
'lot': 'unit',
'pair_decimals': 6,
'lot_decimals': 8,
'lot_multiplier': 1,
'leverage_buy': [2, 3],
'leverage_sell': [2, 3],
'fees': [[0, 0.26],
[50000, 0.24],
[100000, 0.22],
[250000, 0.2],
[500000, 0.18],
[1000000, 0.16],
[2500000, 0.14],
[5000000, 0.12],
[10000000, 0.1]],
'fees_maker': [[0, 0.16],
[50000, 0.14],
[100000, 0.12],
[250000, 0.1],
[500000, 0.08],
[1000000, 0.06],
[2500000, 0.04],
[5000000, 0.02],
[10000000, 0.0]],
'fee_volume_currency': 'ZUSD',
'margin_call': 80,
'margin_stop': 40,
'ordermin': '2.5'}}}
We can add more pairs to the same call by adding the required pairs in the URL separated by a comma. For example, if we want the ADA/EURO and AAVE/EUR:
resp = requests.get('https://api.kraken.com/0/public/AssetPairs?pair=ADAEUR,AAVEEUR') print(resp.json())
Output:
{'error': [], 'result': {'AAVEEUR': {'altname': 'AAVEEUR', 'wsname': 'AAVE/EUR', 'aclass_base': 'currency', 'base': 'AAVE', 'aclass_quote': 'currency', 'quote': 'ZEUR', 'lot': 'unit', 'pair_decimals': 2, 'lot_decimals': 8, 'lot_multiplier': 1, 'leverage_buy': [2, 3], 'leverage_sell': [2, 3], 'fees': [[0, 0.26], [50000, 0.24], [100000, 0.22], [250000, 0.2], [500000, 0.18], [1000000, 0.16], [2500000, 0.14], [5000000, 0.12], [10000000, 0.1]], 'fees_maker': [[0, 0.16], [50000, 0.14], [100000, 0.12], [250000, 0.1], [500000, 0.08], [1000000, 0.06], [2500000, 0.04], [5000000, 0.02], [10000000, 0.0]], 'fee_volume_currency': 'ZUSD', 'margin_call': 80, 'margin_stop': 40, 'ordermin': '0.02'}, 'ADAEUR': {'altname': 'ADAEUR', 'wsname': 'ADA/EUR', 'aclass_base': 'currency', 'base': 'ADA', 'aclass_quote': 'currency', 'quote': 'ZEUR', 'lot': 'unit', 'pair_decimals': 6, 'lot_decimals': 8, 'lot_multiplier': 1, 'leverage_buy': [2, 3], 'leverage_sell': [2, 3], 'fees': [[0, 0.26], [50000, 0.24], [100000, 0.22], [250000, 0.2], [500000, 0.18], [1000000, 0.16], [2500000, 0.14], [5000000, 0.12], [10000000, 0.1]], 'fees_maker': [[0, 0.16], [50000, 0.14], [100000, 0.12], [250000, 0.1], [500000, 0.08], [1000000, 0.06], [2500000, 0.04], [5000000, 0.02], [10000000, 0.0]], 'fee_volume_currency': 'ZUSD', 'margin_call': 80, 'margin_stop': 40, 'ordermin': '2.5'}}}
How to Get all the Assets
We can get a list of all the available assets by running:
resp = requests.get('https://api.kraken.com/0/public/Assets') resp = resp.json() assets = [] for asset in resp['result']: assets.append(asset) assets
We get 156 assets:
['1INCH',
'AAVE',
'ACA',
'ADA',
'ADA.S',
'AIR',
'AKT',
'ALGO',
'ALGO.S',
'ALICE',
'ANKR',
...
'ZJPY',
'ZRX',
'ZUSD']
How to Get the Current Price of a Ticker
We can get the current market prices for an individual currency pair, and includes all the following information:
- most recently traded price
- most recently traded volume
- best (highest) bid price
- volume available at best bid price
- best (lowest) ask price
- volume available at best ask price
- additional trading information (high/low of day, number of trades, total volume of day, etc.)
Note: Today’s prices start at midnight UTC
Let’s get the current ticker data of the ADA/EUR
resp = requests.get('https://api.kraken.com/0/public/Ticker?pair=ADAEUR') resp.json()
Output:
{'error': [],
'result': {'ADAEUR': {'a': ['0.794935', '850', '850.000'],
'b': ['0.794734', '850', '850.000'],
'c': ['0.794997', '100.00000000'],
'v': ['1057413.14777778', '2846333.33727160'],
'p': ['0.785368', '0.776113'],
't': [1607, 3526],
'l': ['0.769807', '0.755176'],
'h': ['0.800000', '0.800000'],
'o': '0.770461'}}}
How to Get OHLC Data
Now, we will show you how to get the OHLC data of a cryptocurrency. In the URL, you can define the interval in minutes and the starting period in Unix timestamp. Let’s say that we want to get the hourly data of ADA/EUR from 2022-19-03 up to now. Notice that the API returns the following statistics:
- Unix Timestamp
- Open Price
- High Price
- Low Price
- Close Price
- Volume Weighted Average Price (vwap)
- Volume
- Number of transactions
resp = requests.get('https://api.kraken.com/0/public/OHLC?pair=ADAEUR&since=1647625329&interval=60') df = pd.DataFrame(resp.json()['result']['ADAEUR']) df.columns = ['unixtimestap', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count'] df
If we want to get a quick plot:
import matplotlib.pyplot as plt %matplotlib inline df.close.astype(float).plot(figsize=(12,8))
If we want to build a pipeline to add more cryptocurrencies with a for loop, we can work as follows. For simplicity, let’s consider only two cryptocurrencies, the ADA and the AAVEX over Euro.
df = pd.DataFrame() my_pairs = ['ADAEUR','AAVEEUR'] for pair in my_pairs: tmp_url = f'https://api.kraken.com/0/public/OHLC?pair={pair}&since=1647625329&interval=60'.format(pair) resp = requests.get(tmp_url) tmp_df = pd.DataFrame(resp.json()['result'][pair]) tmp_df['asset'] = pair df = pd.concat([df,tmp_df]) df.columns = ['unixtimestap', 'open', 'high', 'low', 'close', 'vwap', 'volume', 'count', 'asset']
Final Thoughts
As Predictive Hacks, we never claimed that we can predict the market and more specifically the cryptocurrency market, which is so volatile. However, we can assess the risks, the implied volatility, ideas of how to build optimized portfolios, how to run technical analysis and many other things. We believe that there will be a high demand for cryptocurrency analysis and this is a good opportunity for the Data Scientists since there will be many investors that would like to apply algorithmic trading, dashboards, analysis, reports and so on. So, I do not know if you can make money by investing in the cryptocurrency market but I strongly believe that you can make money from the cryptocurrency demand working as Data Scientist. Stay tuned for the next tutorials!
Want some Free Cryptos?
You can get $25 in Bitcoin by investing $100 in Nexo
2 thoughts on “How to Get Cryptocurrency Data from Kraken API in Python”
How can you distinguish between cryptocurrency and traditional currencies from the data you are fetching???? They are all labeled “currency”!
What are you talking about?