Codementor Events

Stop being a slave to the chart. The easiest way to track your Bitcoin profits.

Published Feb 01, 2019
Stop being a slave to the chart. The easiest way to track your Bitcoin profits.

We often find ourselves checking the bitcoin price multiple times a day, waiting for the next peak or valley. We all want to make some profit from the market volatility.

Often times this becomes distracting and time consuming, but not anymore. We will be building a script which automatically checks the price at your favorite exchange, compares it to your starting price and notifies you of the profit you are making.

Quick Start Guide

If you just want to run the script without digging into it you can get the final code from here. Then just copy and paste it into a file with “.py” extension and run it via terminal:

$ python bitcoin-profits.py

The script will prompt you to enter some data.

  1. Your exchange: the exchange you want to check at, this is the exchange where you plan to sell the bitcoin eventually, or just your favorite one.
  2. Bitcoin starting price: The price at which you bought your coins.
  3. Bitcoin amount: The amount of bitcoin you bought at that price.
  4. Your currency (default USD): The fiat currency in which you are trading.
  5. Check every (seconds): The script performs the calculation in an infinite loop, so this parameter specifies how much to wait between subsequent executions. You can choose from 4 options for our API.
  6. Choose to be notified when a certain limit is reached: 1) Percent limit, 2) Amount limit. Here you specify when you want to be notified of the change in price in Bitcoin. If you choose the first option (1), then you will be asked to enter the amount of percent. Say you enter 10, this means that when the price of Bitcoin changes 10% from your starting price, you will receive a notification. The second option allows you to specify the exact amount, here if you enter 10 you will be notified when the Bitcoin price deviates 10 US dollars form the starting price (assuming US dollars is your currency).

Full Developer Guide

Step 1: Generating the BitcoinAverage API key pair

To be able to generate a key pair you must have an account at BitcoinAverage. Choose one here.

After you create and verify your account you can generate the API key pair here.

Now just copy the public and secret keys into the starting code snippet for our script bellow.

Step 2: Function for generating the Authentication signature for the Bitcoin Average API.

This function will use your secret and public key to generate a signature that will be sent with each HTTP request that you make later on.

The algorithm for signature creation is very simple. First concatenate the current timestamp (in UTC) with the public key with a “.”(dot) between them.

Then encrypt the resulting string with our secret key and the SHA256 algorithm which is already implemented in almost all programming languages.

Finally get the encrypted value and concatenate it, again with a “.”(dot) to the previous plain string, the one that contained the timestamp and the public key.

For example the final signature would look something like this:

1493816419.YjBjZDFlNWMxZDk1NDNlZThlYWY5ZDlhODA4MjhlN2Q.92b28898b6081e65b2735de57aae28d23b7332c26cbe5d87ef1ad1bfad393cf1

Step 3: Making the Http API call to the BitcoinAverage API

This is the main function that accepts all of the parameters entered by the user and forwards them to the calculate_profits function which we’ll write next.

But before we start coding make sure you have the awesome requests http library installed. Get it with: pip install requests.

Let’s checkout the response we get from our API real quick.

As you see from this response you can discover all the available trading pairs at your exchange and you can extend this script to perform the profits check calculation on more than one trading pair.

For now we will only use the BTCUSD values from the response. Let’s cover what these values mean: ask, bid, last and volume.

  • Last: the last price at which Bitcoin was sold or bought
  • Bid: the best (highest) bid. The highest price someone is willing to buy Bitcoin at your exchange right now.
  • Ask: the best (lowest) ask. The lowest price someone is willing to sell Bitcoin at your exchange right now.
  • Volume: the total number of Bitcoin bought and sold at this exchange in the last 24 hours.

From these values we are only interested in the “bid” value because if we were to sell our coins right now, that is the price we would get for them. so after we extract the bid, we just pass all the values to the calculate_profits function.

Step 4: Algorithm for checking our profits

What we are doing here is comparing the starting price with the latest bid price we got from the API.

If the script is setup to check in percent, we are doing that, otherwise we are checking for the absolute difference in dollars between the two prices.

If the latest price has deviated from the starting price more than a certain amount then we are constructing a helpful message that we will present to the end user.

Step 5: Running the script in an infinite loop and displaying desktop notifications on Linux and Mac

First we’re getting all the data we need from the user and then using infinite looping (while True:) we are executing the main function from before.
If this function happens to return us some answer, that means there is something to show to the user.
Depending on his choosing either Linux or Mac desktop notification will be generated. If he hasn’t chosen notifications, or is a Windows user, the message will be printed in the terminal.

Linux desktop notifications

I have only tried this on Ubuntu Linux, so these might not work on other Linux distros. The library for generating notifications is called notify-send and it’s very simple to use. In our example above I am running it like this.

notify-send 'Bitcoin Profits' result

This will show the result as the body of the notification and “Bitcoin Profits” as the title.

Mac desktop notifications

For Mac we’re using the osascript command which lets us run AppleScript commands from inside the terminal. The command for showing a notification is as follows:

osascript -e 'display notification "You have made 10% profit" with title "Bitcoin profits" '

Full code

The full code for the script can be found here

Discover and read more posts from Darko Kolev
get started
post commentsBe the first to share your opinion
Show more replies