Calculate Kucoin Balance in your Currency
With so many different coins available it can be hard to keep track of your portfolio.
Why not use python to make it simple for you. In this post I’ll step through downloading your balances on Kucoin and then calculating our total balance in USD and other currencies supported by Kucoin.
At the time of writing Kucoin supports these currencies
USD, EUR, AUD, CAD, CHF, CNY, GBP, JPY, NZD, BGN, BRL, CZK, DKK, HKD, HRK, HUF, IDR, ILS, INR, KRW, MXN, MYR, NOK, PHP, PLN, RON, RUB, SEK, SGD, THB, TRY, ZAR
This example requires an account on Kucoin as it utilises private API calls, you will need an API key and secret go to your API settings to generate one, and enable it.
You will also need to have the python-kucoin library installed.
Fetching our coin balances
Kucoin provides an endpoint to fetch all balances, so let’s try it.
Now we have a list of balance objects in the format
coinType
is the code of the coinbalance
is the total available balance on the exchangefreezeBalance
is the amount tied up in active tradesbalanceStr
is a string representation of the balancefreezeBalanceStr
is a string representation of the frozen balance
So to get our total balance we need to add the balance
and freezeBalance
values for each coin.
If we inspect the list we see that it has returned some coins that we don’t have any balance at all, e.g
We will be able to filter these out by testing for balanceStr == '0.0' and freezeBalanceStr == '0.0'
To get a unique list of the coins we do have we can use python list comprehension.
Fetching value in Fiat
Kucoin has a helpful function to fetch Fiat rates for a list of coins which is exactly what we need.
It requires a comma separated list of coins as a parameter
There are some forked coins on Kucoin such as GOD (at time of writing) that don’t have a fiat value, and you won’t even find on Coin Market Cap yet.
The get_currencies
call does not return them so we need to be aware of that.
Calculate our total in Fiat
Now that we have the balance of each coin and the fiat value of the coin we can finally determine a total.
And there you have it, your balance in your currency.
Bonus
Well seeing as this turned out to just use the Kucoin endpoints I’ve added a get_total_balance
function
to python-kucoin to do this for you.
What next
Why not send your daily balance breakdown to yourself via email, or maybe a quick snapshot your own Telegram bot.
Another source of prices is Coin Market Cap which returns some extra information in the ticker, such as percentage changes per hour, day or week price in BTC and even coin market cap rank. I will look at an example of this for the Binance exchange soon.
Further extensions could include
- a total balance in BTC, well it turns out we can reuse our code from the last example.
- overall percentage change per hour, day or week.
- weighted value of our coin market cap rank.