Using Python 3 to Retrieve Your Cryptocurrency Balances from Binance Part 3

in #python7 years ago (edited)

This is part 3 in our discussion on using Python 3 to retrieve our cryptocurrency balances from Binance. In the first iteration of this script we had to manually insert the name of the coin into the bclient.get_asset_balance() api function. It was a quick and simple task but not very useful.
In part 2 we took another approach to getting our balances from our Binance accounts using the same api function as in part 1. In this iteration of the python script we learned how to read in a list of coin symbols from a text file and retrieve our balances for several coins rather than just one as was the case in the part 1 example. This method was better but it still needed some tweaking because if our list wasn’t updated prior to running the script we would either not get all of our balances or we would see balances of zero for coins we no longer owned.
We need an efficient method for getting our balances out of Binance without having to manually inserting a coin symbol or maintaining a list in a text file of coins we currently have balances. Part 3 in our journey will solve this problem for us.

Requirements:
Python 3 :
Download here: https://www.python.org/downloads/

Binance API Key and Security Key:
Instructions: https://support.binance.com/hc/en-us/articles/360002502072-How-to-create-API

Binance API: Downloads and Reference:
https://github.com/binance-exchange/binance-official-api-docs

You can create your Python scripts with any simple text editor such as “Notepad (Windows”), TextEdit ( Mac OS X), or any number GUI or CL editors for (Linux). Or you can use applications such as JUPYTER or ATOM and others. Other than these tidbits of information this article will not discuss basic concepts of using Python, editors or API’s.

This script introduces the get_account() api function. The script will also use some Python functions to convert data into usable numeric data and make the data displayed easier to read.

get_account(): This function returns an api response that is a list with dictionary data inside it.

{
"makerCommission": 15,
"takerCommission": 15,
"buyerCommission": 0,
"sellerCommission": 0,
"canTrade": true,
"canWithdraw": true,
"canDeposit": true,
"balances": [
{
"asset": "BTC",
"free": "4723846.89208129",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "4763368.68006011",
"locked": "0.00000000"
}
]
}

We want just the balances so let’s do this!

Just to recap we need to pass our private API_KEY and API_SECRETKEY

Create a folder in your documents folder and call it “BinanceScripts” here you will save all the files you create in this tutorial.

Create a personal.PY file. The personal.PY file will contain three lines. This file is not meant for sharing with anyone! DO NOT SHARE! The file will contain your API Key and your Secret Key you generated from this site:

https://support.binance.com/hc/en-us/articles/360002502072-How-to-create-API

***** Start : Example personal.py file *******
Screen Shot 2018-04-25 at 9.06.56 AM.png

***** End: Example personal.py file ******

Save this file as personal.py.

We’ll do the following:
Create a new script and call it acctBalances.py.
Import our personal.py information
Import the binance.client
Store get_account() results in a variable.
Execute a for loop to evaluate account balances and display the data in a format that is easy to read.

***** Start: acctBalances.py *****
Screen Shot 2018-04-27 at 10.33.29 AM.png
***** End: acctBalances.py *****

That’s it. I hope you’ve found this tutorial easy to follow and useful in your pursuit in Python programming and in the world of cryptocurrency management!

Sort:  

Hey @jagedgem, great post! I enjoyed your content. Keep up the good work! It's always nice to see good content here on Steemit! Cheers :)