CryptoCurrency Trading on HitBTC with a Python Bot - REST API

in #python6 years ago

This first part of our python trade bot journey is to learn how to interface with HitBTC using the REST-API as mentioned in my first post . The goal here will be to fire off a request to their servers every 5 seconds to retrieve the last traded price for EOS, the highest bid value, and the lowest ask value. We will display these values to screen continuously until the user hits Ctrl + C to terminate the script.

Let's walk through the code ( When I reference line numbers I am not including empty lines )

Thankfully, most of the hard work has already been done for us by the python-dev team's, we just need to import all the libraries that will help us get the job done.

The first five lines import prewritten modules that include functions and classes, these have been tried, tested and proven to work (at least up to this writing). The biggest hitter here is the request module that deals with all the HTTP complexities and lets us send POST and GET requests in just a few simple steps, in our case we just need to create a session object and call the get() method.

Line 7 (as with anything that starts with a # is a comment, so explains itself

Line 8 is an empty dictionary that we will use and allocate later on in the code. This will store the return value of our session.get() function.

Line 9 creates a variable called webSession which is a name I choose and we instantiate requests.session object to handle the REST API calls. The documentation for the request model can be found here

Line 11 Starts a perpetual loop and will not stop until an interrupt or exception happens. As mentioned, we can send an interrupt by hitting Ctrl +C.

Line 13 clears the screen just to keep things looking nice.

Line 15 is where we fire off our POST request to HotBTC and store the return value in our json dictionary. Notice the .json() at the end of get() function, if we did not include this, the return value would simple be an HTTP 200 OK reponse. We are not looking for the HTTP code, we want payload.

Bear in mond that we have zero error checking built in here which is a terrible idea but at this stage we can ignore that. I assure you we will include it as we progress.

Now that we have the json returned to us we need to access it. Here is what we get back from HitBTC and saved to the json variable :-

{'ask': '18.40590', 'open': '16.82014', 'high': '18.57000', 'volumeQuote': '7333139.5256709', 'timestamp': '2018-05-08T03:37:12.601Z', 'last': '18.40407', 'volume': '411866.65', 'bid': '18.37939', 'symbol': 'EOSUSD', 'low': '16.75981'}

The above string is in a very specific format:​-


attribute​ : value

We only want a few of these values, and as this is now stored as a dictionary, we simply pass the key value into our json variable to retrieve the key we want.

Lines 17,18,19 and 20 are simply print statements including our json value and the key we want, ie we need the last price paid, so the key is last (have have put the attribute/value pair in bold above.

Code


import requests
import environment
import os
import sys
import time

# Create an empty dictionary to store the returned JSON
json = {};

# Create a session object
webSession = requests.session();

# Loop forever until Ctl^c is hit
while True:
    # Clear the screen
    os.system('clear')

    # Fire off the Rest API call and save the response as json to a variable
    json = webSession.get("https://api.hitbtc.com/api/2/public/ticker/EOSUSD").json()

    # Print out just the key values we want
    print("Symbol      : %s" % (json['symbol']))
    print("Last Price  : %s" % (json['last']))
    print("Highest Bid : %s" % (json['bid']))
    print("Lowest Ask  : %s" % (json['ask']))

    # Sleep for 5 seconds then loop again
    time.sleep(5)

Output

Symbol : EOSUSD
Last Price : 17.98524
Highest Bid : 17.95442
Lowest Ask : 17.99608

Next up, using HitBTC's streaming API using WebSockets for faster results.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.032
BTC 61662.20
ETH 3059.04
USDT 1.00
SBD 3.84