Improving the Binance news trading algorithm

Last week we covered how to code Binance trading bot that analyses news sentiment and makes buying decisions based on how positive the news headlines are for the top 100 cryptocurrency feeds. The code has been open-sourced on GitHub and is available for everyone to use or to contribute with improvements. I’m happy to say that the bot has been positively received by many of you and some even contributed to improving the functionality of the bot.

This article builds upon the original set-up of the bot, so if your Binance bot is not set up yet, I would recommend starting with the last week’s article as  if offers a step-by-step introduction into all the requirements needed to set the bot up as well as how to write and execute the code. Or if you don’t need a guide feel free to jump directly into the Github code for the  Binance News Trading bot .

Before going into the added functionality I would to announce that cryptomaton.org is now listed on the top 200 cryptocurrency feeds on Feedspot . So thank you for reading and sharing this blog!

Implementing the changes

Added functionality

Without further ado, let’s cover some of the improvements that were suggested and made by the GitHub community:

  • The bot is now much quicker.
    • It used to take around 107 seconds to pull and analyse today’s top Cryptocurrency headlines, this has now been brought down to only 7 seconds by using async functions and async http requests.
  • Ability to place sell orders when the sentiment is negative has been added as well. 
    • Previously, you would only be able to Buy if the sentiment is positive but now both positions are supported
  • The bot will only analyse today’s news headlines
    • Previously, the algorithm would pull the last headline some could be older than a day
  • The bot will remember coins bought and won’t buy more until it sold some
    • An issue within the code logic would lead the bot to keep placing trades once it detected a fitting signal. This functionality ensure that each coin is only bought once while the code is running.
  • Easy changing between the testnet and mainnet

Updating the code

If you’re currently running the version from the previous blog post, there are a few code changes that are needed.

Importing additional modules

In addition to the modules imported for the base version, you will also need to import the following:

Make sure you install aiohttp via pip if you don’t have this library on your computer. The previous blog explains how to install the pip installer if you don’t have that on  your machine either, so check that out if you’re just starting with Python.

Switching between testnet and mainnet

The Binance testnet allows to to safely test the configuration of your bot without the risk of losing any real money. In order to make it easier to change to the mainnet, the testnet = True variable has been added in. Just change this to False if you want to use the mainnet. You will also need to provide both testnet and mainnet API keys in order for this to work. 

Create additional user inputs

In order to place trades based on negative sentiment you need to give it a value between 0 and -1. HOURS_PAST define how old the articles should be/

Memorise coins bought

Add the following code to memorise the coins that were bought since the algorithm was executed. Note that stopping and re-starting the algorithm will currently cause the bot to forget the coins bought.

Fix the lot size for each coin

In the Binance API you have different levels of accuracy for how exact a quantity is when placing a trade. For example, Bitcoin supports an accuracy (or step size as it’s called by Binance) of 6 decimal points, while Ripple only has 1. As the our Binance news bot can work with multiple coins, it’s important to know the correct step size for each.

Improving parsing time

The parsing time can be improved by making the parsing functions async and by using aiohttp.

Changes to the buy function

The buy function will now check if we already bought a coin based on news sentiment, and it will stop buying immediately if that’s true.

Adding a sell function

The sell function is very similar to the buy function, only the signals are reversed. The main difference is how we handle the amount. IN the sell function there is a variable called amount_to_sell, which essentially sells 99.5% of the owned crypto that was bought via the bot. We only sell 99.5% in order to allow Binance to take its commission.

Adjust the execution of the code

The last update considers the execution of the code, so that the logged information looks bit more user friendly.

 

That’s it, if you have now successfully  improved the functionality of you Binance News Trading bot.

Did you find this article useful? Subscribe to my newsletter for more awesome crypto but building guides and resources.

Want to support me more? You can contribute by donating  here , or via the Brave Browser. Thank you!

Leave a Reply

Your email address will not be published.