I have to admit I’m quite excited about this bot and I can’t wait to test it out. The bot that I’m about to walk you through is able to analyse the changes in price across all coins on Binance and place trades on the most volatile ones. In addition to that, this Binance trading algorithm will also keep track of all the coins bought and sell them according to your specified Stop Loss and Take Profit.
- a Binance account
- a GitHub account
- Binance Testnet & MainnetAPI keys
- a few python libraries
- some knowledge of Python
Making a start
First things first, if you don’t have a Binance account, or would like to create a separate account to run the bot on, go ahead and create one. You can use my referral link to get 5% off your trading fees. With a Binance account created, we’re going to Generate some API Keys. We’ll start with the mainnet keys and then the testnet keys.
To get your mainnet keys, navigate over your account, and click on API management.
On the API management page, give a label to your API key and click create API.
Once your key and secret have been generated make sure to save these, as you will only be shown the secret once. You’ll need to regenerate your keys if you haven’t saved the secret.
Now let’s give it the permissions that we require to run the bot on it. We’re only going to do Spot trading, so we only need to select Enable Reading and Enable Spot & Margin Trading.
Head over to testnet.binance.vision in order to create a testnet account.
Log In with GitHub and authenticate with your GitHub account. If you don’t have a GitHub account, simpy head over to GitHub and create one first. Once you’re logged in with your GitHub, click on Generate HMAC_SHA256 Key.
Give your key a description and click Generate.
Now it’s important that you SAVE your API Key and Secret displayed as for security reasons, this is the only time you will be able to see them. Simply regenerate the keys if you haven’t saved them.
Coding the Binance trading bot
UPDATE: continue following the guide below to learn how to configure the bot, but please refer to the GitHub repo for the latest version of the script.
With the Binance account and keys out of the way it’s time for the exciting part – coding our trading bot in Python! Let’s start by clearly defining the parameters of this bot:
- The bot will listen to changes in price accross all Coins on Binance*
- By default we’re only picking USDT pairs
- We’re excluding Margin (like BTCDOWNUSDT) and Fiat pairs
- The bot checks if the any coin has gone up by more than 3% in the last 5 minutes
- The bot will buy 100 USDT of the most volatile coins on Binance
- The bot will sell at 6% profit or 3% stop loss
These are all easily configurable, and I encourage you to play with these parameters around.
Making a start on the code
The first thing we want to do is import the modules that we need for the script. Quick sidenote here, python-binance needs to be installed via pip by using the pip install python-binance command. If you don’t have pip on your machine, this other guide I wrote, contains some more info on how to install pip on your machine.
Configuring the Binance Client
We now need to store the Binance API keys in our script. In order to be able to easily switch between Test and Mainnet, there is a short conditional statement that will feed the Binance Client the correct keys for out bot depending on the value inside the TESTNET variable. Set this to True if you want to use the testnet, or (at your own risk) set this to False to use the Mainnet. Using the mainnet you will trade using real money from your account so be particularly careful here if you choose to do so.
Another thing to note here is the syntax for your api keys. In the code above, I am using the os.getenv method in order to call API keys that I have stored on my machine. You don’t have to do this if you’re not planning to make your code public, so simply re-write the syntax for your keys as api_key_test = “YOUR_API_KEY”
Defining user inputs
It’s time to define our user inputs – or the parameters on which our Binance trading bot will place our trades. You can modify these to change the parameters of the algorithm in order to test different strategies, by default we going with the following:
- PAIR_WITH defines the coin (or fiat) that each crypto is paired to. I have only tested this USDT, as most coins are paired to it.
- QUANTITY represents the size of your trade, by default in USDT. Be extra careful with the QUANTITY if you change PAIR_WITH to BNB for example.
- FIATS is a list of fiat currencies and margin symbols that I’m excluding. Anything added here will be excluded from the coins output and won’t be traded with.
- TIME_DIFFERENCE by default we’re checking the difference in price for each coin on Binance in the last 5 minutes, you can change this value for different results. This also dictates how often each code iteration executes.
- CHANGE_IN_PRICE the threshold at which the bot will decide to buy a coin. By default, if a coin has moved by more than 3% in the last 5 minutes, we consider that a strong buying signal.
- STOP LOSS and TAKE PROFIT define in % how to sell the coins that the Binance algorithm bought.
Load Binance bot portfolio
The next step is check if the bot has placed any trades already, and if so to load the bot portfolio. By default, the bot will save each trade in a json file in the same directory as the script, so that we can keep track of trades and sell when TP or SL are reached
Get the current price for each coin listed on Binance
It’s time for our algorithm to read the price for each coin. The get_price() function will the return the price of each coin that meets our criteria.
Wait and fetch the price again
The next function will wait according to the time defined in the TIME_DIFFERENCE variable and will return any coin that has moved by more than the CHANGE_IN_PRICE – by default 3%.
Convert the volume from USDT to each of coins returned
The next step is to convert our QUANTITY of 100USDT (default) into the respective quantity for each coin that we’re about to buy. Because Binance is a bit particular with the format of the volume, our trading bot needs to know the step size for each coin. For example BTC supports a step size of 6 decimal points while XRP only supports one. So if we want to buy XRP, we need to make sure that the volume formatting is correct. Trying to buy 1.000 XRP would fail, while 1.0 would be executed.
Place the trade
Now that we have the volatile coins from Binance as well as the correct format for the volume it’s time to let our Binance trading bot to place some trades for us.
You nearly did it so hand in there! The next step is to update our portfolio by saving the details of each trade into the json file that we’re checking for at the beginning of each iteration. By default, we’re saving the symbol, orderId, timestamp, buying price and volume for each coin.
You can add more info if you wish by looking at the format of client.get_all_orders(symbol=coin, limit=1) and add other information you deem necessary.
Execute sell orders
This function (the last one too!) checks if any of the coins we own in our bot portfolio should be sold due to SL or TP being reached. Note what we’re using our Spot account so we can only sell what we own. We cannot place sell orders if a coin is going down and we don’t own it. But I would be curious to see how the Binance trading bot performs on a Futures account.
Putting it all together
The last piece in our code brings all the functions together and makes the code to keep on executing until you stop it.
That’s it. You have a fully functioning Binance trading bot that detects the most volatile coins on Binance, buys and sells according to each coin’s performance.