Building a cryptocurrency dashboard using Plotly and Binance API | by Mayank Vadsola | Towards Data Science

As of now, according to CoinMarketCap, the global cryptocurrency market is worth more than $1.5T. Recent approval from a couple of banks and credit card companies to include cryptocurrencies as one of their financial products indicates a bright future for the crypto market. In the last few years, more people started trading cryptocurrencies, and there are many exchanges worldwide that support different cryptocurrencies. In this article, we will focus on the Binance exchange, which is the world’s number one exchange today. It supports almost all cryptocurrencies and available in many countries.
In this article, we will discuss how to access data from Binance API and create a dashboard with Plotly.
Here’s what we’ll cover:
How to set up Binance API
How to get data using Binance API
Building a dashboard with Plotly
Binance provides two kinds of API access: 1) Actual Binance API and 2) Test Binance API.
Actual API provides direct access to your actual account, and any trades made with this API will be reflected into your actual account. That means we need to be careful while using this API.
First, you need to sign up with Binance (if you don’t have an account) at https://www.binance.com/en/register?ref=AG3W30LV (This link includes my referral code and will give you a 10% discount on trading fees. More details can be found at https://www.binance.com/en/activity/referral)
After registering, you will be asked (or you can do that from security settings) to set up two-factor authentication for additional security. I will recommend choosing Google Authenticator.
Once 2FA is set up, you can go to the API management tab under settings. You will be asked to provide a label for your API keys (this will be useful when you have multiple keys associated with a single account).
Click on create API after providing a label for your API keys. You will be asked to authenticate one more time, and then you will be able to see your “API Key” and “Secret Key.” This is the only time you will see it, so copy your keys to some safe place. By default, the keys will be given the following access, which can be changed.
We will be using both actual API and test API keys to understand how to work with them. For this, we don’t want to mess up our actual account, so we will change the actual API keys permission to read-only.
We will be saving our actual API keys into secret.cfg file as mentioned below (Remember, never share or publish your secret.cfg file)
Test Binance API provides you with the same feel as you interact with the actual API. I would recommend starting with this (for trading) until you are assured that your application works fine.
First, you need to log in at https://testnet.binance.vision/ (For now, login is only supported with GitHub)
After logging in, click on “Generate HMAC_SHA256 Key” and you will be again asked to provide a label for keys. After providing the label, click on generate, and you should be able to see your API keys. Copy them to some safe place. Also, read all details on the main page about test API.
Now, we will modify secret.cfg file to include test API keys as mentioned below
We have successfully set up both actual and test API keys and saved them to secret.cfg file. In the next section, we will focus on getting data using these API keys.
Binance doesn’t provide a python library for interacting with the API, but there is one very famous third-party library called python-binance , which we will be using to interact with the API.
To install python-binance library
$ pip install python-binance
By default, you will get some balance in your test account in the form of different cryptocurrencies, and we will use test API for this section (as I don’t want to share my account info). Also, python-binance doesn’t have access to test API, so we need to change the endpoint URL.
Here is the code to get the test account info
As you can see from the above output, it displays important things such as accountType, balances, permissions, etc.
Now, let’s get the ETH balance.
You can do many things with the python-binance library and detailed documentation can be found here.
As per my observation, test API doesn’t provide real historical data; instead, it provides dummy data. So to access real data, we will be using actual API and actual API keys.
Getting ETH price from the earliest date available (on Binance) to the present day
The above output represents the following parameters as mentioned on Binance API doc
Converting the output to a data frame and saving it as a CSV file
We can use the Binance WebSocket to stream real-time data. Here’s how you do it
Now, to stop streaming data and to close WebSocket
It looks like we have figured out different ways of getting data and information. There are plenty of other things you can do with python-binance library and Binance API. I would encourage you to take a look at this and this. We can now move to build a Plotly dashboard in the next section.
In this section, we will build a dashboard with Plotly that will track our portfolio of the test account in real-time and change total account value based on live streaming data.
Here is how our final dashboard will look like (don’t mind the aesthetics, as you can change it later)
As you can see, we have included the following features in the dashboard.
Indicator - Total portfolio value in USDT
Indicator - Total portfolio value in BTC
Indicator - BNB/USDT conversion
Pie chart - Portfolio distribution (in USDT)
Bar graph - Tokens distribution
Let’s look at the code.
First, we will import all required libraries.
Second, we will read our keys, establish the connection, and get account info.
Third, we will define some functions for taking care of streaming data and calculating metrics based on live data.
Forth, we will start streaming data.
Fifth, we will define our dashboard layout, graphs and host it
That’s it, and you should be able to track your test account portfolio. You can easily set it up for an actual account using actual keys without changing the endpoint URL.
This brings us to the end of this article. You can access all code in this GitHub repo. Feel free to star or bookmark for your future reference.
Recently, I started writing a series on Algorithmic Trading with Python and Machine Learning. You can find the first article below.
pub.towardsai.net
Follow me for more articles. Feel free to connect and contact me on LinkedIn. Thank you!
Note from Towards Data Science’s editors: While we allow independent authors to publish articles in accordance with our rules and guidelines, we do not endorse each author’s contribution. You should not rely on an author’s works without seeking professional advice. See our Reader Terms for details.