Making a Python script to get Desktop notifications on Cryptocurrency prices

There are people who stare at the exchange markets like a hawk. We all know one person like that.

These days it's similar for cryptocurrency exchange markets.

So, I decided to build a simple Python script to give desktop notification on my Ubuntu system in case a cryptocurrency crosses a limit.

Let's dive into the code.

But before we begin, you're gonna need API key and API secret from Binance. So head over to https://www.binance.com/ and get your keys.

Set up

First, create a virtual environment, we all know it's a good practice. I have used Python3.6 for this project. After creation, activate the environment.

$ virtualenv venv -p python3.6
$ source venv/bin/activate
(venv) $ python -V
Python 3.6.5

Now we will install the project dependencies.

(venv) $ pip install python-binance notify2 schedule

Why have I installed these packages?

  • python-binance

For accessing the Binance API using Python

  • notify2

To show native desktop notifications.

  • schedule

It's a dead-simple library that let's schedule and repeats things as per your wish.

config

Our application is going to need a config file. Why? Well, this is how it knows which markets to monitor and what are the price limits that you want to be notified about.

{
    "markets":[
        {
            "name":"ETHBTC",
            "active":true,
            "max":0.0806,
            "min":0.08035
        },
        {
            "name":"LTCBTC",
            "active":false,
            "max":0.016850,
            "min":0.016828
        }
    ]
}
  • name

As the "name" suggests is the name of the market you want concerned about.

  • active

In case you want to disable this market fro being monitored.

  • max, min

The maximum and minimum price limit, if the market crosses this you want to be notified.

Code

Importing all the necessary packages.

import os
import json
import time
import schedule
import notify2

from binance.client import Client

Now we have to connect to the Binance API

binance_client = Client("YOUR BINANCE KEY HERE", "YOUR BINANCE SECRET HERE")

And we have to initiate the notify2 too.

notify2.init("Watch Crypto")

Now time for a big chunk of code.

def job():

    config = None

    with open("config.json") as file:
        config = json.load(file)

    for market in config.get("markets"):

        if market["active"] is False:
            continue

        symbol = market["name"]
        max_price = market["max"]
        min_price = market["min"]
        ticker = binance_client.get_symbol_ticker(symbol=symbol)
        price = float(ticker.get("price"))

        if price > max_price or price < min_price:
            notification = notify2.Notification(summary="{} has crossed {}".format(symbol, price))
            notification.show()

    print("notified")

You guessed it right, this is it, this is what this script revolves around.

To start we read the config file ( the one we mentioned before).

Now in the for loop, we first check if the market is active or not. If not then we get out of this loop using the continue statement.

Otherwise, we get the price of the market using the get_symbol_ticker() function provided by python-binance.

Next, we check if the price has crossed our limits of max and min prices. If yes, then it sends a notification like this.

Rest of the code

schedule.every().minute.do(job)

print("Initiated")

while True:
    schedule.run_pending()
    time.sleep(1)

Here, as you can see we are running our job function with a minute interval.

That's it, folks.

Find the code in its completeness here. My GitHub gist

I know the script is fairly simple, but this was made to serve as a starting point or to spark ideas in your minds. Further things that we can add here is, to have a dashboard, from where we can make changes and see all the reports.

If you run into any issues or any have feedback, do contact me.

Twitter: @anshulc95 GitHub: @anshulc95

Related article

AUTHOR

Thank you for writing interesting articles. I am looking forward to sharing your knowledge.

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?