Crypto News

How to get notifications on new coins listed on Binance with Python

Binance seems to be listing several new cryptocurrencies each week. As a means of taking advantage from the significant spike in the price of new coin listings on Binance, I have been testing an open source crypto trading bot in Python that detects new coins the moment they’re listed and attempts to quickly place an order for quick gains. 

The tool is still in test mode, however I’ve recently added a new bit of functionality to it that will allow you to receive e-mail notifications when Binance makes the announcement on their page. This will give you precious time to actually buy the coin externally before it gets listed on Binance.

This article will cover how this tool actually works and how you can implement it yourself.

Check out this video if you want to find out more about how this tool can be used alongside the crypto trading algorithm that I’ve been working on. If this is the kind of content you were hoping for, please subscribe!

Ok, now that you know how this links to the bigger strategy, let’s get down to coding!

Install Python and dependencies

If you’re just starting out, the first thing you want to do is install Python on your machine. Simply head over to https://www.python.org/downloads and find a suitable version for your OS.

Now, in order to be able to scrape the Binance Announcement page, and get information about new coin listings ahead of time, we need a scraping tool. We’re going to use Selenium – but first, we need to be able to install Selenium or any other Python packages, and for that we need to install Pip.

Open up your cmd or terminal, and type the following command in:

Alternatively, if you’re using a Mac or Linux system, you may use this:

Once pip is installed, we can start installing Selenium and pyaml:

Now do the same with pyaml – this will enable us to read and write yml files (more on that in a bit).

Create a directory and start coding

With the preparation work out of the way, we can now create a new directory where our Python scraper can live. We’re also going to create a few different Python files, in order to keep our script nice and neat:

  • store_listing.py – helps store new coin symbols from Binance announcements locally
  • config.yml – we need to store our e-mail credentials somewhere.
  • load_config.py
  • send_notification.py – will contain the script to send us an e-mail when a new Binance announcement is made
  • new_listings_scraper.py – the script for scraping the Binance Annoucement page

Store listings in a json file

We’re going to need two simple functions in order to be able to store the symbol of the last coin announced by Binance. def store_listing() will create a simple json file, that will store anything we pass in under the symbol argument. In our case, the symbol of the newly announced listing.

Just as a side note though, this function can be re-used in any number of ways in different scripts, to store information locally, so you might want to make a  note of that for when you need to write and save something in Json format.

The second function will find and load an existing json file. We’ll use this to replace the current values in our json file with new ones as future Binance listings get announced. Again – this function can be used for anything.

Send e-mail notifications with Python and smtplib

The next step is to create a script that will send an e-mail notification. Don’t worry if you can’t see how this all ties together, I promise it will all make sense in the end.

Let’s start by creating a config.yml file. Here is where you would store your email address and password. Can be any e-mail address, so you may create a burner one if you’re not comfortable doing this on your main one.

The next step is to create a simple load_config.py file that will load our credentials from config.yml. Note that we’ve imported the yaml module that we installed earlier via pip.

Ok now for the actual sending of the e-mail notification, I promise.

Let’s create a file called send_notification.py. In the new file let’s import the smtplib and ssl. We’re also going to import our load_config file that we created above.

Next, we assign the load_config function to a variable called config. This will contain the login details stored in config.yml.

For the actual send – create a function called send notification that takes in one argument (coin).

We’re going to use port 465 and the gmail smtp. Note that Gmail might restrict you from logging in via Python, you may have to enable less secure login from the Gmail settings in order to get this to work.

sent_from and to is our e-mail address as stored in the config file. 

Subject – you will notice that we’ve wrapped the subject in f-strings and we’re passing the {coin} variable in. This will contain the symbol listed by Binance, once we include this function in our main scraper file.

In the e-mail body we’ll include a link to the Binance Announcements page as well as a google search for the new symbol so that we can easily find out where we can buy it from ahead of it being listed on Binance.

Finally, we’ll use a try and except block in order to trigger an e-mail. We’re now ready to create a Binance scraper in Python using selenium.

Creating the Binance announcements scraper to detect new symbols

The first thing you need to do is download chromedriver. Chromedriver is user by Selenium in order to scrape web pages. Make sure that you download Chromedriver v94, as v95 won’t work with selenium. If you’re having trouble with this, read the error log and download the appropriate version as required. Download here.

Now we’re ready to crate our new_listings_scraper.py file and import some requirements:

Here we’ve imported several functions from the  selenium library, along with a binary_path for chromedriver, to automatically get the correct path for our driver. Note, you may need to pip install chromedriver_py. We’re also importing os.path and json.

In addition to this, we’re also importing the files that we created previously: store_listing, load_config and send_notification.

Now for the actual scraping of the Binance Announcements page:

Firstly, we assign Options() to chrome_options and we then add_argument(“–headless”). This will hide the actual browser when scraping so you won’t see it on your screen. If you want to see how the scraping happens simply #comment out this line.

We then assign the binary path for our driver, and we give it the URL we want it to scrape by using the driver.get(“URL”) method.

 The next step is to create a function that does the scraping. We want to only get the last announcement, and we can do this by targeting the id of the first element with selenium by using latest_announcement = driver.find_element(By.ID, ‘link-0-0-p1’). 

As we’re only interested in new coin listings, we’re going to create a brief list called exclusions where we will be excluding any announcements that contain the words futures, margin or adds – feel free to edit this as you see fit.

Next we need to enumerate for every character in our latest announcement string. Using index won’t work since index returns the first position where an element is located. For example letter a will be used multiple times in our string, yet its index will always be the position where a is first used. Once the enum list is ready, it’s time to work some filter magic.

We could just return the entire string “Binance adds…” but, for multiple reasons, we only want the symbol of the new coin to be added. By just getting the symbol, we can automate a system that automatically feeds this symbol to trading algorithm that places the order for us in Binance. 

This is where we’re filtering through letters and trying to select only uppercase characters that are close together, and are followed by a space or a “)”. This is how that logic looks like:

uppers = ”.join(item[1] for item in enum if item[1].isupper() and (enum[enum.index(item)+1][1].isupper() or enum[enum.index(item)+1][1]==’ ‘ or enum[enum.index(item)+1][1]==’)’) )

If you start seeing a weird sequence of symbols in your notifications so make sure to add more exclusions for announcements that do not strictly mention a coin listing.

Next we need to define two more functions. One that will store the symbol in a local json file called new_listing.json and another function that will run all the necessary functions – let this be main()

The main function will get the last coins, and store the new listings, while the store_new_listing function calls the send_notification() function and will send ourselves an e-mail when appropriate.

Now let’s run this:

Did you enjoy this article? Here’s what to do next:

Consider donating at the widget next to the top of the page or via the brave browser. Your donations help keep this content free and open source.
If  you’re looking for a custom-built cryptocurrency trading algorithm, shoot me a message and let’s talk
Subscribe to the newsletter for more awesome content!

The post How to get notifications on new coins listed on Binance with Python appeared first on Automated Cryptocurrency Trading.

Read more: https://www.cryptomaton.org/2021/10/03/how-to-get-notifications-on-new-coins-listed-on-binance-with-python/

Text source: Automated Cryptocurrency Trading

Disclaimer: Financial information and news are not financial advice, read the disclaimer.
Buy & sell Crypto in minutes

Join BINANCE!

The world's largest crypto exchange

You're just steps away from receiving your reward.

The most complete Crypto News Center.

Search Stories:

Latest top stories