In this tutorial, we will show you how to get the balance of the portfolio using the Kraken API. We assume that you have a Kraken account and you have access and a secret key to the Kraken API
Get the Balance of Each Asset
Let’s dive directly into the coding part and get the balance of each asset.
import requests import pandas as pd import time import os import urllib.parse import hashlib import hmac import base64 import re # Read Kraken API key and secret stored in environment variables api_url = "https://api.kraken.com" api_key = 'xxx' api_sec = 'yyy' def get_kraken_signature(urlpath, data, secret): postdata = urllib.parse.urlencode(data) encoded = (str(data['nonce']) + postdata).encode() message = urlpath.encode() + hashlib.sha256(encoded).digest() mac = hmac.new(base64.b64decode(secret), message, hashlib.sha512) sigdigest = base64.b64encode(mac.digest()) return sigdigest.decode() # Attaches auth headers and returns results of a POST request def kraken_request(uri_path, data, api_key, api_sec): headers = {} headers['API-Key'] = api_key # get_kraken_signature() as defined in the 'Authentication' section headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec) req = requests.post((api_url + uri_path), headers=headers, data=data) return req
The above code snippet is necessary for the next API calls. Now we are ready to get the balance of each asset.
# Attaches auth headers and returns results of a POST request def kraken_request(uri_path, data, api_key, api_sec): headers = {} headers['API-Key'] = api_key # get_kraken_signature() as defined in the 'Authentication' section headers['API-Sign'] = get_kraken_signature(uri_path, data, api_sec) req = requests.post((api_url + uri_path), headers=headers, data=data) return req # Construct the request and print the result resp = kraken_request('/0/private/Balance', { "nonce": str(int(1000*time.time())) }, api_key, api_sec) resp = resp.json()['result'] print(resp)
And we get:
{'ADA.S': '65.19155200', 'KAVA.S': '10.12947700', 'LUNA.S': '2.16474600', 'KAVA': '0.00000000', 'FLOW': '0.0000005900', 'AVAX': '1.0000000000', 'TRX': '0.00000000', 'FIL': '0.9000000000', 'XXBT': '0.0120190800', 'LUNA': '0.00000565', 'TRX.S': '500.43458500', 'ADA': '0.00000000', 'XETH': '1.1020075000', 'ATOM.S': '8.03591300', 'KSM': '0.0000000000', 'MINA': '0.0000000000', 'EUR.M': '144.9550', 'MINA.S': '30.1546203900', 'KSM.S': '0.7069214200', 'ZEUR': '50.0097', 'XZEC': '1.0000000000', 'FLOW.S': '3.1173987499', 'DOT.S': '23.9283502600', 'AAVE': '0.3515185600', 'BAT': '70.0000000000', 'ATOM': '0.00000000', 'SOL.S': '1.0027377300', 'SOL': '0.0000000000', 'DOT': '0.0000000000', 'UST': '0.19395300'}
As we can see, there are some cryptos that appear to be 0 and some others that they have the suffix .S
. This means that these Cryptos are staking in Kraken. Thus, it will be convenient to merge the staking and non-staking cryptos as follows:
my_cryptos = {} for crypto in resp.keys(): if crypto=='EUR.M': my_cryptos[crypto.replace(".M","")] = float(my_cryptos.get(crypto.replace(".M",""),0)) + float(resp[crypto]) else: my_cryptos[crypto.replace(".S","")] = float(my_cryptos.get(crypto.replace(".S",""),0)) + float(resp[crypto]) print(my_cryptos)
And we get:
{'ADA': 65.191552, 'KAVA': 10.129477, 'LUNA': 2.16475165, 'FLOW': 3.1173993399, 'AVAX': 1.0, 'TRX': 500.434585, 'FIL': 0.9, 'ATOM': 8.035913, 'KSM': 0.70692142, 'MINA': 30.15462039, 'EUR': 144.955, 'ZEUR': 50.0097, 'XZEC': 1.0, 'DOT': 23.92835026, 'AAVE': 0.35151856, 'BAT': 70.0, 'SOL': 1.00273773, 'UST': 0.193953}
Get the Balance of Each Asset in Euros
Now can show the value of each asset in €.
euro_value = {} for crypto in my_cryptos.keys(): if crypto[0] == 'X' and len(crypto)>3: tmp_ticker = crypto+'ZEUR' else: tmp_ticker = crypto+'EUR' if crypto == 'ZEUR' or crypto == 'EUR': euro_value[crypto] = float(my_cryptos[crypto])*1 else: resp = requests.get(f'https://api.kraken.com/0/public/Ticker?pair={tmp_ticker}'.format(tmp_ticker)) euro_value[crypto] = float(my_cryptos[crypto])*float(resp.json()['result'][tmp_ticker]['a'][0]) print(euro_value)
{'DOT': 472.72902168836, 'MINA': 75.9414917976, 'FIL': 21.0744, 'FLOW': 20.0684508546753, 'SOL': 114.41983901489999, 'XZEC': 174.898, 'LUNA': 209.976088436, 'ATOM': 222.0341276046, 'KSM': 124.00407786, 'ADA': 70.73331213522602, 'UST': 0.137013315, 'ZEUR': 50.0085, 'KAVA': 41.716544589, 'EUR': 250.0824, 'AVAX': 89.56, 'AAVE': 70.4302586816}
Get the Total Value of the Portfolio
We can get the total of the portfolio simply by running:
f'The value of the portfolio is {sum(euro_value.values())}'
'The value of the portfolio is 1844.8007626705555'
Want some Free Cryptos?
You can get $25 in Bitcoin by investing $100 in Nexo